首页 > 解决方案 > 错误 [ERR_REQUIRE_ESM] - Husky,lint-staged,eslint - nodeJS:

问题描述

尽管对此还有其他问题,但大多数人都没有回应,或者给出的回应对我不起作用。

对于它给出的显然 eslint 正在查看 node_modules,这是给定的错误:

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/kamoraes/Workspace/node_adc/node_modules/supports-color/index.js from /home/kamoraes/Workspace/node_adc/.git/hooks/commit-msg not supported.
Instead change the require of index.js in /home/kamoraes/Workspace/node_adc/.git/hooks/commit-msg to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/home/kamoraes/Workspace/node_adc/.git/hooks/commit-msg:8:23) {
  code: 'ERR_REQUIRE_ESM'
}


Node v16.13.0

问题是,该项目处于第一步,在同一节点和纱线,版本的另一台机器上快速重做项目,不要给出相同的错误。还请了我的一个朋友试试。没有给出错误。

另外,这个项目是一门课程,步骤相同,这是我现在的整个代码:

https://github.com/kaiqueAMoraes/clean-node-api

这个给定错误的最后一次提交是苦差事:eslintignore 6250e5bdea05cc2eb413c8a57a97e4bbe4bd5bb9

我已经添加了哈士奇,lint-staged

yarn add -D husky lint-staged

然后添加他们各自的配置文件

.huskyrc.json:

{
  "hooks": {
    "pre-commit": "lint-staged"
  }
}

.lintstagedrd.json:

{
  "*.ts": [
    "eslint 'src/**' --fix",
    "git add"
  ]
}

供参考:tsconfig:

{
  "compilerOptions": {
    "outDir" : "./dist",
    "module": "commonjs",
    "target": "es2019",
    "strictNullChecks": true,
    "esModuleInterop": true,
    "allowJs": true
  }
}

eslintrc:

{
  "extends": "standard-with-typescript",
  "parserOptions": {
    "project": "./tsconfig.json"
  }
}

gitignore 和 eslintignore 都忽略 node_modules 和 dist

标签: node.jseslinteslintrchusky

解决方案


推荐阅读