请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

使用husky进行commit约束的时候会报错

/Users/jaylen/Desktop/ele-h5/node_modules/ts-node/dist/index.js:851
            return old(m, filename);
                   ^
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/jaylen/Desktop/ele-h5/commitlint.config.js from /Users/jaylen/Desktop/ele-h5/node_modules/cosmiconfig/dist/loaders.js not supported.
commitlint.config.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename commitlint.config.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /Users/jaylen/Desktop/ele-h5/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at Object.require.extensions.<computed> [as .js] (/Users/jaylen/Desktop/ele-h5/node_modules/ts-node/dist/index.js:851:20)
    at module.exports (/Users/jaylen/Desktop/ele-h5/node_modules/import-fresh/index.js:32:59)
    at loadJs (/Users/jaylen/Desktop/ele-h5/node_modules/cosmiconfig/dist/loaders.js:16:18)
    at Explorer.loadFileContent (/Users/jaylen/Desktop/ele-h5/node_modules/cosmiconfig/dist/Explorer.js:86:20)
    at Explorer.createCosmiconfigResult (/Users/jaylen/Desktop/ele-h5/node_modules/cosmiconfig/dist/Explorer.js:94:36)
    at Explorer.loadSearchPlace (/Users/jaylen/Desktop/ele-h5/node_modules/cosmiconfig/dist/Explorer.js:70:31)
    at async Explorer.searchDirectory (/Users/jaylen/Desktop/ele-h5/node_modules/cosmiconfig/dist/Explorer.js:55:27)
    at async run (/Users/jaylen/Desktop/ele-h5/node_modules/cosmiconfig/dist/Explorer.js:35:22)
    at async cacheWrapper (/Users/jaylen/Desktop/ele-h5/node_modules/cosmiconfig/dist/cacheWrapper.js:16:18)
    at async Explorer.search (/Users/jaylen/Desktop/ele-h5/node_modules/cosmiconfig/dist/Explorer.js:27:20)
    at async loadConfig (/Users/jaylen/Desktop/ele-h5/node_modules/@commitlint/load/lib/utils/load-config.js:40:19)
    at async load (/Users/jaylen/Desktop/ele-h5/node_modules/@commitlint/load/lib/load.js:19:20)
    at async main (/Users/jaylen/Desktop/ele-h5/node_modules/@commitlint/cli/lib/cli.js:194:20) {
  code: 'ERR_REQUIRE_ESM',
  filepath: '/Users/jaylen/Desktop/ele-h5/commitlint.config.js'
}
husky - commit-msg hook exited with code 1 (error)

正在回答

1回答

同学你好,这个问题应该是你的 package.json 里声明 "type": "module",这个配置的意思是项目下的所有 js 文件都使用 ES 模块。但目前 commitlint.config.js 使用的是 commonjs 模块方式,所以导致不兼容。

解决方式有两种,一种是将 commitlint.config.js 文件名改成 commitlint.config.cjs 声明这是一个 commonjs 模块。另一种是把 package.json 里的 type: module 去掉。

1 回复 有任何疑惑可以回复我~
  • 提问者 jaylen666 #1
    好的,现在可以了,谢谢老师
    回复 有任何疑惑可以回复我~ 2023-02-03 21:16:12
  • one_pieces 回复 提问者 jaylen666 #2
    应该的哈~
    回复 有任何疑惑可以回复我~ 2023-02-03 21:36:00
  • 提问者 jaylen666 #3
    非常感谢!
    回复 有任何疑惑可以回复我~ 2023-02-22 21:58:01
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信