【问题标题】:VSCODE checkJs not finding modulesVSCODE checkJs 找不到模块
【发布时间】:2019-06-13 18:35:46
【问题描述】:

我有一个非常简单的 react material-ui 项目。

我最近在顶级文件夹中添加了jsconfig.json

{
    "compilerOptions": {
        "target": "es6",
        "checkJs": true,
        "jsx": "react"
    },
    "exclude": [
        "node_modules",
        "data",
        "docs",
        ".cache",
        "dist"
    ]
}

哪个工作正常。但是 VSCode 发现了一些我想删除的错误:

找不到模块“@material-ui/core/Button”。

找不到模块“@material-ui/core/styles”。

找不到模块“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';

【问题讨论】:

    标签: visual-studio-code tsconfig


    【解决方案1】:

    你可能需要设置"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 上得到的答案

    是的,如果您从node_modules 导入模块,您通常需要设置"module": "commonjs"(我相信"moduleResolution": "node" 也应该可以工作)。 commonjs 是默认设置,但 "target": "es6" 会覆盖它以使用 "module": "es6",除非您明确指定另一个 "module" 设置

    这里更详细地介绍了模块选项:Understanding "target" and "module" in tsconfig

    【讨论】:

    • 优秀。 (但我现在要追查另一组错误:请参阅:github.com/ctrlplusb/easy-peasy/issues/221
    • 这肯定与类型有关。既然你没有使用 TS,你真的需要类型检查吗?
    • 嗯,我认为这是 VSCODE 的一个很好的特性,如果你在你的函数上使用 JSDOC,那么实际上(在 VSCODE 内部做打字稿解析引擎)你基本上得到了相同风格的静态类型检查。但是,如果有一种方法可以关闭类型检查并仍然进行其他语法检查 - 在这种情况下可能是合适的。
    • 这就是我正在做的。我得到的唯一类型检查是对 React 使用 propTypes。但是当我需要对某个对象进行一些智能感知来自动完成属性和方法时,我会使用 JSDoc 语法,它可能会成功。但我确实对某些 DOMElement 使用过一次或两次。对于库 API 对象,我猜它取决于某种类型的 d.ts 文件。
    【解决方案2】:

    块引用

    VSCODE settings

    转到File -> Preferences -> Settings 或on 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
    

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

    【讨论】:

      猜你喜欢
      • 2022-11-02
      • 2018-08-29
      • 2018-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-24
      • 2018-02-04
      • 2021-04-28
      相关资源
      最近更新 更多