我有一个非常简单的 react material-ui 项目。
我最近添加了 jsconfig.json
到顶层文件夹
{
"compilerOptions": {
"target": "es6",
"checkJs": true,
"jsx": "react"
},
"exclude": [
"node_modules",
"data",
"docs",
".cache",
"dist"
]
}
哪个工作正常。但是 VSCode 发现我想删除一些错误:
Cannot find module '@material-ui/core/Button'.
Cannot find module '@material-ui/core/styles'.
Cannot find module 'easy-peasy'.
导入工作正常,但我宁愿不只是禁用 ts-check。所有这些导入都在 ./node-modules 树中(包括 easy-peasy)。
(顺便说一句,代码都是 JavaScript 而不是 TS)。
import { action, createStore, StoreProvider, useStore, useActions, thunk } from 'easy-peasy';
import Button from '@material-ui/core/Button';
import { withStyles } from '@material-ui/core/styles';
import Grid from '@material-ui/core/Grid';
import TextField from '@material-ui/core/TextField';
最佳答案
您可能需要设置 "module": "commonjs"
这是我的做法:
jsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
"target": "es6",
"module": "commonjs",
"paths": {
"@components/*": ["./src/components/*"],
"@constants/*": ["./src/constants/*"],
"@helpers/*": ["./src/helpers/*"]
}
},
"include": [
"src/**/*"
]
查看我在 issue on Github Vscode 上得到的答案Yes if you are importing modules from
node_modules
, you generally need to set"module": "commonjs"
("moduleResolution": "node"
should also work I believe).commonjs
is the default setting, but"target": "es6"
overrides it to use"module": "es6"
unless you explicitly specify another"module"
settingThe module options are covered in a bit more detail here: Understanding "target" and "module" in tsconfig
关于visual-studio-code - VSCODE checkJs 找不到模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56586704/