node引用chalk插件 报错 ‘ERR_REQUIRE_ESM‘

这篇博客介绍了在使用Chalk库时遇到的错误,由于安装了高于v5的最新版本,导致不支持esModule。解决方案是将Chalk版本回降至4.1.0,可以通过yarn或npm进行安装。降级后问题得到解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引用报错

const chalk = require('chalk');

报错 如上图
因为安装的是最新版本的 ^chalk@5
而版本 5以上不支持 es Module
只要版本降为4版本即可,

yarn add chalk@4.1.0 || npm i chalk@4.1.0
Nothing to compile failed to get chainId, falling back on net_version... An unexpected error occurred: Error [ERR_REQUIRE_ESM]: require() of ES Module D:\学习\1. 前端React精品教程\练习\web3\crossChain-nft\node_modules\micro-eth-signer\typed-data.js from D:\学习\1. 前端React精品教程\练习\web3\crossChain-nft\node_modules\hardhat\internal\core\providers\accounts.js not supported. Instead change the require of typed-data.js in D:\学习\1. 前端React精品教程\练习\web3\crossChain-nft\node_modules\hardhat\internal\core\providers\accounts.js to a dynamic import() which is available in all CommonJS modules. at D:\学习\1. 前端React精品教程\练习\web3\crossChain-nft\node_modules\hardhat\internal\core\providers\accounts.js:45:79 at async LocalAccountsProvider.request (D:\学习\1. 前端React精品教程\练习\web3\crossChain-nft\node_modules\hardhat\internal\core\providers\accounts.js:45:31) at async ChainIdValidatorProvider._getChainIdFromEthNetVersion (D:\学习\1. 前端React精品教程\练习\web3\crossChain-nft\node_modules\hardhat\internal\core\providers\chainId.js:28:21) at async ChainIdValidatorProvider._getChainId (D:\学习\1. 前端React精品教程\练习\web3\crossChain-nft\node_modules\hardhat\internal\core\providers\chainId.js:16:33) at async ChainIdValidatorProvider.request (D:\学习\1. 前端React精品教程\练习\web3\crossChain-nft\node_modules\hardhat\internal\core\providers\chainId.js:45:35) { code: 'ERR_REQUIRE_ESM'
最新发布
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值