I am trying import a JSON file into a TypeScript script using await import(filepath)
but am receiving the following error running script with npx ts-node <script>
:
"CustomError: ERR_UNKNOWN_FILE_EXTENSION .json /path/to/file.json"
This is the code I use to import the file:
const serAcc: ServiceAccount = await import(GOOGLE_APPLICATION_CREDENTIALS, {assert: { type: "json" });
where GOOGLE_APPLICATION_CREDENTIALS
is a string.
These release notes have some information about adding an assert property to dynamic imports.
The following is my tsconfig.json:
{
"compilerOptions": {
"target": "es2017",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ESNext", "DOM"],
"moduleResolution": "Node",
"strict": true,
"sourceMap": true,
"resolveJsonModule": true,
"isolatedModules": true,
"esModuleInterop": true,
"noEmit": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"skipLibCheck": true
},
"ts-node": {
"esm": true
},
"include": ["src", "utility/test.ts", "utility/tests", "organize.js", "index.ts"]
}
This is my directory structure:
- tsconfig.json
- utility:
- storageScripts:
- uploadFile.ts
I am calling the file from the root directory with:
npx ts-node ./utility/storageScripts/uploadFile.ts
Is there something wrong with my tsconfig.json that is causing improper module resolution? I set my module Resolution to "Node", and resolveJsonModule and isolatedModule to true which I have seen helped others with similar issues.
I have also had issues with importing .ts files as well when running scripts with npx ts-node
, but if I specify that the files are .ts
files, there is no error:
// @ts-ignore
import { object } from "file.ts"
How can I import .json files to a TypeScript script using ts-node
Thanks