-
Why does "ReferenceError: require is not defined" appear in Node.js? |
Beta Was this translation helpful? Give feedback.
Answered by
TatyOko28
Feb 9, 2025
Replies: 1 comment
-
This error happens when you use require() in an ES module (.mjs) instead of a CommonJS module (.cjs). Solution 1: Use import instead of require (for ES Modules) import express from "express"; Solution 2: Use CommonJS by renaming your file to .cjs "type": "commonjs" |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Mercure28
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This error happens when you use require() in an ES module (.mjs) instead of a CommonJS module (.cjs).
Solution 1: Use import instead of require (for ES Modules)
Solution 2: Use CommonJS by renaming your file to .cjs
Or add this to package.json: