繁体   English   中英

Typescript 无法导入 json

[英]Typescript can't import json

我根本无法在 NodeJS v18 项目中导入 json 个文件。 我正在使用 TS 4.8.4 并且我在 tsconfig.json

"compilerOptions": {
  "esModuleInterop": true,
  "target": "esnext",
  "lib": ["esnext"],
  "module": "commonjs",
  "resolveJsonModule": true,
  "forceConsistentCasingInFileNames": true,
  "strict": true,
  "skipLibCheck": true
}

在 index.ts 我有:

import * as json from '../config.json';
console.log(json);

我已经在 stackoverflow 中看到了很多这样的答案,但我可以发现问题所在。 有什么建议么? 谢谢你。

编辑:我一直在兜圈子。 TS编译报错建议换模块。 我更改了模块而不是导入不再。 我找不到解决方案。

更新在 tsconfig 中将模块更改为 nodenext,json 文件的导入工作但其他 TS 文件的导入不再工作:错误 TS2835:相对导入路径需要显式文件扩展名

EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './myclass.js'?

我现在还需要改变什么吗? 谢谢你。

在导入中,您提到了“从 '../config.json' 导入 * 为 json;” 而不是尝试“从'./config.json'导入*作为json;” 或者您也可以尝试使用 json 的绝对路径来进行测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM