visual-studio-code - VSCODE checkJs 找不到模块

标签 visual-studio-code tsconfig

我有一个非常简单的 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" setting

The 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/

相关文章:

typescript - tsc-不编译别名路径

typescript :设置 "node_modules"目录路径

javascript - 为什么 Vite 会创建两个 TypeScript 配置文件 : tsconfig. json 和 tsconfig.node.json?

javascript - 如何在未定义类、仅传递给的模块中获取智能感知?

typescript - VSC 中的 ESLint 不适用于 .ts 和 .tsx 文件

python - VS代码无法导入已安装的模块

javascript - Angular 2 - TypeError undefined 不是数组包含的函数

angular - VS Code - 如何从 angular html 模板创建方法?

python - 如何判断 VS Code 使用的 Python 格式化程序的版本?

node.js - 'tsConfigPath' 选项已弃用,并将在下一个主要版本中删除。使用 'compilerOptions.typescript.configPath' 选项代替