commit 报错
来源:4-5 使用 lint-staged 配置 pre-commit
星海V5
2023-02-09
PS O:\www\wwwroot\leshan\leshan_mobile> git commit -m “feat: test”
leshan_mobile@0.0.0 pre-commit
lint-staged
→ No staged files match any configured task.
O:\www\wwwroot\leshan\leshan_mobile\node_modules\ts-node\dist\index.js:851
return old(m, filename);
^
Error [ERR_REQUIRE_ESM]: require() of ES Module O:\www\wwwroot\leshan\leshan_mobile\commitlint.config.js from O:\www\wwwroot\leshan\leshan_mobile\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 O:\www\wwwroot\leshan\leshan_mobile\package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).
at Object.require.extensions.<computed> [as .js] (O:\www\wwwroot\leshan\leshan_mobile\node_modules\ts-node\dist\index.js:851:20)
at module.exports (O:\www\wwwroot\leshan\leshan_mobile\node_modules\import-fresh\index.js:32:59)
at loadJs (O:\www\wwwroot\leshan\leshan_mobile\node_modules\cosmiconfig\dist\loaders.js:16:18)
at Explorer.loadFileContent (O:\www\wwwroot\leshan\leshan_mobile\node_modules\cosmiconfig\dist\Explorer.js:86:20)
at Explorer.createCosmiconfigResult (O:\www\wwwroot\leshan\leshan_mobile\node_modules\cosmiconfig\dist\Explorer.js:94:36)
at Explorer.loadSearchPlace (O:\www\wwwroot\leshan\leshan_mobile\node_modules\cosmiconfig\dist\Explorer.js:70:31)
at async Explorer.searchDirectory (O:\www\wwwroot\leshan\leshan_mobile\node_modules\cosmiconfig\dist\Explorer.js:55:27)
at async run (O:\www\wwwroot\leshan\leshan_mobile\node_modules\cosmiconfig\dist\Explorer.js:35:22)
at async cacheWrapper (O:\www\wwwroot\leshan\leshan_mobile\node_modules\cosmiconfig\dist\cacheWrapper.js:16:18)
at async Explorer.search (O:\www\wwwroot\leshan\leshan_mobile\node_modules\cosmiconfig\dist\Explorer.js:27:20)
at async loadConfig (O:\www\wwwroot\leshan\leshan_mobile\node_modules\@commitlint\load\lib\utils\load-config.js:40:19)
at async load (O:\www\wwwroot\leshan\leshan_mobile\node_modules\@commitlint\load\lib\load.js:19:20)
at async main (O:\www\wwwroot\leshan\leshan_mobile\node_modules\@commitlint\cli\lib\cli.js:194:20) {
code: ‘ERR_REQUIRE_ESM’,
filepath: ‘O:\www\wwwroot\leshan\leshan_mobile\commitlint.config.js’
}
husky - commit-msg hook exited with code 1 (error)
1回答
-
one_pieces
2023-02-09
这个问题跟 https://coding.imooc.com/learn/questiondetail/GjNdEXKDDV969rn4.html 一样哈~
应该是你的 package.json 里声明 "type": "module",这个配置的意思是项目下的所有 js 文件都使用 ES 模块。但目前 commitlint.config.js 使用的是 commonjs 模块方式,所以导致不兼容。
解决方式有两种,一种是将 commitlint.config.js 文件名改成 commitlint.config.cjs 声明这是一个 commonjs 模块。另一种是把 package.json 里的 type: module 去掉。
122023-05-16
相似问题