主页 > 手机  > 

使用yarn的时候,遇到Error[ERR_REQUIRE_ESM]:require()ofESModule怎么


晚上回到家,我打开自己的项目,执行:

cd HexoPress git pull --rebase yarn install yarn dev

拉取在公司 push 的代码,然后更新依赖,最后开始今晚的开发时候,意外发生了,竟然报错了,明明在公司时候还好好的。到底怎么回事?

/Users/charles/Projects/HexoPress/node_modules/concurrently/node_modules/cliui/build/index.cjs:291 const stringWidth = require('string-width'); ^ Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/charles/Projects/HexoPress/node_modules/string-width/index.js from /Users/charles/Projects/HexoPress/node_modules/concurrently/node_modules/cliui/build/index.cjs not supported. Instead change the require of index.js in /Users/charles/Projects/HexoPress/node_modules/concurrently/node_modules/cliui/build/index.cjs to a dynamic import() which is available in all CommonJS modules. at Object.<anonymous> (/Users/charles/Projects/HexoPress/node_modules/concurrently/node_modules/cliui/build/index.cjs:291:21) at Object.<anonymous> (/Users/charles/Projects/HexoPress/node_modules/concurrently/node_modules/yargs/build/index.cjs:1:60678) at Object.<anonymous> (/Users/charles/Projects/HexoPress/node_modules/concurrently/node_modules/yargs/index.cjs:5:30) at Object.<anonymous> (/Users/charles/Projects/HexoPress/node_modules/concurrently/dist/bin/concurrently.js:30:33) { code: 'ERR_REQUIRE_ESM' } Node.js v20.8.1 error Command failed with exit code 1.

放狗在网上一搜,我发现这个问题并非我一个人遇到,竟然相当普遍。

其实我在公司,都还好好的,怎么可能回家就不对了呢?表面上看,这个问题的原因是在一个 Common JS 的代码里,用 require() 去引用了一个 ES Module 的类,这么操作是不支持的。推荐的解决办法是,将 require() 换成 import()。不用想,这当然是无稽之谈,很显然,出问题的代码根本就不是我写的,怎么可能去改呢?

是我引用的依赖,它的依赖,我的依赖我都不可能改,更别说依赖的依赖了。

比对整合了多篇帖子的内容后,我发现大家遇到这个错误是在不同的库,比如我这个场景是 concurrently 引用 string-width 的时候,网上还有很多人是引用 ts-node 的时候,别的也有。不过,不同场景遇到同一个错误,确实一点奇怪。终于,我发现,共性是大家都用了包管理器 yarn,有人说,将 yarn 升级到 3 就可以解决了。

我定睛一看,我 Node 版本是 20+,但是用的 yarn 竟然是 v1.22,为什么会用了一个这么老的 yarn 版本呢?这次我是真的乏力了,第一,我搞不清到底 yarn 有什么 bug 导致了这个问题,第二我搞不清为什么我会用这么旧一个 yarn 的版本。

解决方法很简单,就是升级 yarn,但是怎么升级呢?首先,你要确保你的 Node 环境的版本在 16.10+,然后:

yarn set version stable yarn install

在项目里执行上面的命令,然后,你会发现升级完毕了。再次尝试,果然问题解决了。今天我执行完毕后,yarn 已经升级到了 v4.1.0 版本了。现在是 2024 年。

所以,如果你遇到了跟我一样的问题,可以先检查一下 yarn 的版本号是否过老?我前面也有文章提过,推荐初学者使用 npm 作为包管理器,至少我自己使用 npm 的时候,从没遇到过这个问题。确实是兼容性最好的包管理器。

标签:

使用yarn的时候,遇到Error[ERR_REQUIRE_ESM]:require()ofESModule怎么由讯客互联手机栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“使用yarn的时候,遇到Error[ERR_REQUIRE_ESM]:require()ofESModule怎么