[英]HOW TO FIx code : 'ERR_REQUIRE_ESM' const { nanoid } = require("nanoid");
how to fix it, I'm doing App chat.如何解决它,我正在做应用程序聊天。 Sorry if the language is difficult to read, I'm Thai.
对不起,如果语言难以阅读,我是泰国人。
PS C:\Users\ADMIN\Desktop\chat\server> node server.js
C:\Users\ADMIN\Desktop\chat\server\server.js:4
const { nanoid } = require("nanoid");
^
[ERR_REQUIRE_ESM]: require() of ES Module
C:\Users\ADMIN\Desktop\chat\server\node_modules\nanoid\index.js from
at Object.<anonymous> (C:\Users\ADMIN\Desktop\chat\server\server.js:4:20) {
code: 'ERR_REQUIRE_ESM'
{
"name": "chat",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"keywords": [], "author": "", "license": "ISC", "dependencies": { "express": "^4.18.1", "nanoid": "^4.0.0", "socket.io": "^4.5.1" } } “关键字”:[],“作者”:“”,“许可证”:“ISC”,“依赖项”:{ “表达”:“^4.18.1”,“nanoid”:“^4.0.0”,“ socket.io": "^4.5.1" } }
This means the library you're trying to require cannot be imported using the require
syntax.这意味着您尝试要求的库无法使用
require
语法导入。
I think the version of the nanoid
you're using needs to be imported using import
我认为您使用的
nanoid
版本需要使用import
You could try downgrading nanoid
or switching to the import
keyword.您可以尝试降级
nanoid
或切换到import
关键字。
The problem is that you are using nanoid Ver.4.0.0.问题是您使用的是 nanoid Ver.4.0.0。 It seems that a new feature in V.4 (support for ESM) is a braking change.
V.4 中的一个新特性(对 ESM 的支持)似乎是一个制动变化。
The full documentation is in thislink to issue#365 in the nanoid GitHub repo.完整文档位于 nanoid GitHub 存储库中 issue#365 的链接中。
The comment that helped me and I base my solution on was from@salyndev0帮助我和我的解决方案的评论来自@salyndev0
To fix the problem follow these steps:要解决此问题,请执行以下步骤:
Uninstall nanoid: npm uninstall nanoid
卸载 nanoid:
npm uninstall nanoid
Install Version 3 supporting all 3.xx: npm install nanoid@^3.0.0
安装支持所有 3.xx 的版本 3:
npm install nanoid@^3.0.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.