Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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" } }

like image 943
Sutarpat Thummanon Avatar asked Jun 07 '26 00:06

Sutarpat Thummanon


1 Answers

The problem is that you are using nanoid Ver.4.0.0 . It seems that a new feature in V.4 (support for ESM) is a braking change.

The full documentation is in this link to issue#365 in the nanoid GitHub repo.

The comment that helped me and I base my solution on was from @salyndev0

To fix the problem follow these steps:

  • Uninstall nanoid: npm uninstall nanoid

  • Install Version 3 supporting all 3.x.x: npm install nanoid@^3.0.0

like image 77
Yariv Avatar answered Jun 08 '26 12:06

Yariv