node使用ts编译json文件时:Cannot find module 'xxx.json'. Consider using '--resolveJson问题

375 阅读1分钟

引入方式:

import xxxx from "xxxx.json"
const xxxList: any[] = xxxx.data;

//json
{
    "data": [
    ]
}

报错信息:编译后

> tsc index.ts --outDir dist

xxx.ts:3:25 - error TS2732: Cannot find module 'xxxx.json'. Consider using '--resolveJsonModule' to import module with '.json' extension.


Found 1 error in xxxx.ts:3

解决方法:

//tsconfig.json中修改:
"compilerOptions": {
    "resolveJsonModule": true,
    "allowSyntheticDefaultImports": true,            
    "esModuleInterop": true,
}

重新编译即可

使用require引入无法解决此问题