打字稿无法导入.json
我正在尝试使用等待Import(filepath)
将JSON文件导入打字稿脚本,但是正在接收以下错误,该错误使用npx ts-node< script>
:
"CustomError: ERR_UNKNOWN_FILE_EXTENSION .json /path/to/file.json"
这是我用来导入文件的代码:
const serAcc: ServiceAccount = await import(GOOGLE_APPLICATION_CREDENTIALS, {assert: { type: "json" });
其中google_application_credentials
是字符串。
这些发行说明主张动态导入的属性。
以下是我的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"]
}
这是我的目录结构:
- tsconfig.json
- utility:
- storageScripts:
- uploadFile.ts
我正在从根目录中调用文件:
npx ts-node ./utility/storageScripts/uploadFile.ts
我的tsconfig.json是否有问题,导致模块分辨率不正确?我将模块分辨率设置为“ node”,并将其解析和隔离模块置于True,我看到的这对他人有类似问题的帮助。
我在使用npx ts-node
运行脚本时也遇到了导入.ts文件的问题,但是如果我指定文件为.ts
文件,则没有错误:
// @ts-ignore
import { object } from "file.ts"
我如何使用TS节点将.json文件导入到打字稿
脚本
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论