导入不存在的模块时如何获得警告

how can I get warnings when importing modules that don't exist

我在 VsCode 中使用 EsLint。

当我尝试导入一个不存在的模块时怎么会出现错误?

例如

import foo from './this-path-doesnt-exist'

应使用红色下划线。 这需要 eslint 插件吗?

如果您使用 eslint 作为 linter,您可以使用 eslint-plugin-import .

This plugin intends to support linting of ES2015+ (ES6+) import/export syntax, and prevent issues with misspelling of file paths and import names

除了建议的 eslint 插件之外,您还可以通过在文件顶部添加 // @ts-check 来在 VS Code 中启用对 JS 文件的语义检查:

// @ts-check
import foo from './this-path-doesnt-exist'

这还将启用文件中的许多其他检查,包括类型检查,因此它可能不适用于每个代码库,但它可以帮助发现许多常见的编程错误。

More info about ts-check

如果您还没有使用 linter,请按照以下步骤操作:

  1. npm install eslint --save-dev

  2. npm 安装 eslint-plugin-import --save-dev

然后你需要配置

extends:

  • eslint:recommended
  • plugin:import/errors
  • plugin:import/warnings

手动进入您的 .eslintrc.(yml|json|js) 文件。

如果您已经在使用 linter。跳过第一步安装 eslint。