打字稿无法导入.json

发布于 2025-02-10 04:19:57 字数 1849 浏览 1 评论 0原文

我正在尝试使用等待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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文