[英]require a json file results in an error: Cannot find module
In a nodej project open in VsCode with checkJs
enabled, when a json file is required like在nodej项目在VsCode开放
checkJs
启用,当需要一个JSON文件中像
const myFile = require('./my-file.json')
This makes an error [ts] Cannot find module
.这会导致错误
[ts] Cannot find module
。
How is it possible to remove the error warning?如何消除错误警告?
I tried to:我试过了:
add "resolveJsonModule": true
to the compilerOptions
in jsconfig.json
, but it does not work.添加
"resolveJsonModule": true
的compilerOptions
在jsconfig.json
,但它不工作。
create a typing.d.ts
file with this content:使用以下内容创建一个
typing.d.ts
文件:
declare module '*.json' { const value: any; export default value; }
declare module '*.json' { const value: any; export default value; }
But now, there is an error [ts] Type 'typeof import("*.json")' must have a '[Symbol.iterator]()' method that returns an iterator. [2488]
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]
[ts] Type 'typeof import("*.json")' must have a '[Symbol.iterator]()' method that returns an iterator. [2488]
I had a similar problem when trying to import json from a file in a typescript project.尝试从打字稿项目中的文件导入 json 时,我遇到了类似的问题。
I used我用了
import * as data from "module/path/filename.json"
instead of代替
const data = require("module/path/filename.json")
and it worked.它奏效了。
You should add你应该添加
"resolveJsonModule":true
as part of compilerOptions to tsconfig.json.作为 tsconfig.json 的compilerOptions 的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.