【发布时间】:2023-03-30 19:48:01
【问题描述】:
在启用了checkJs 的VsCode 中打开的nodej 项目中,当需要像
const myFile = require('./my-file.json')
这会产生错误[ts] Cannot find module。
如何消除错误警告?
我尝试过:
将
"resolveJsonModule": true添加到compilerOptions中的jsconfig.json,但它不起作用。-
使用此内容创建一个
typing.d.ts文件:declare module '*.json' { const value: any; export default value; }但是现在,出现错误[ts] Type 'typeof import("*.json")' must have a '[Symbol.iterator]()' method that returns an iterator. [2488]
【问题讨论】:
-
在进行 require 时不需要指定 json 扩展名。 const myFile = require('./my-file');
-
谢谢,但如果没有扩展名,也会出现同样的错误。出现错误是因为 json 文件不是
module。 (-> 它没有module.exports) -
检查这个答案,也许它可以帮助你:stackoverflow.com/questions/49996456/…
标签: javascript json typescript visual-studio-code checkjs