Skip to content

Node.js Error: "ReferenceError: require is not defined" #2014

Answered by TatyOko28
Mercure28 asked this question in Q&A
Discussion options

You must be logged in to vote

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
Or add this to package.json:

"type": "commonjs"

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Mercure28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants