VSCODE checkJs 找不到模块

VSCODE checkJs not finding modules

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

Blockquote

VSCODE settings

转到 File -> Preferences -> Settingson Mac Code -> Preferences -> Settings 选择工作区设置选项卡 将此代码添加到右侧显示的 settings.json 文件中:

// 将您的设置放在该文件中以覆盖默认设置和用户设置。

{
    "files.exclude": {
        "**/.git": true,         // this is a default value
        "**/.DS_Store": true,    // this is a default value

        "**/node_modules": true, // this excludes all folders 
                                 // named "node_modules" from 
                                 // the explore tree

        // alternative version
        "node_modules": true    // this excludes the folder 
                                // only from the root of
                                // your workspace 
    }
}

If you chose File -> Preferences -> User Settings

然后为当前用户全局配置排除文件夹。