require is not defined? Node.js
Solution 1
In the terminal, you are running the node application and it is running your script. That is a very different execution environment than directly running your script in the browser. While the Javascript language is largely the same (both V8 if you're running the Chrome browser), the rest of the execution environment such as libraries available are not the same.
node.js is a server-side Javascript execution environment that combines the V8 Javascript engine with a bunch of server-side libraries. require()
is one such feature that node.js adds to the environment. So, when you run node in the terminal, you are running an environment that contains require()
.
require()
is not a feature that is built into the browser. That is a specific feature of node.js, not of a browser. So, when you try to have the browser run your script, it does not have require()
.
There are ways to run some forms of node.js code in a browser (but not all). For example, you can get browser substitutes for require()
that work similarly (though not identically).
But, you won't be running a web server in your browser as that is not something the browser has the capability to do.
You may be interested in browserify which lets you use node-style modules in a browser using require()
statements.
Solution 2
This can now also happen in Node.js as of version 14.
It happens when you declare your package type as module in your package.json
. If you do this, certain CommonJS variables can't be used, including require
.
To fix this, remove "type": "module"
from your package.json
and make sure you don't have any files ending with .mjs
.
Solution 3
As Abel said, ES Modules in Node >= 14 no longer have require
by default.
If you want to add it, put this code at the top of your file:
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
Source: https://nodejs.org/api/modules.html#modules_module_createrequire_filename
Solution 4
Node.JS is a server-side technology, not a browser technology. Thus, Node-specific calls, like require()
, do not work in the browser.
See browserify or webpack if you wish to serve browser-specific modules from Node.
Solution 5
just remove "type":"module" from your package.json. I hope this will help you😊.
Comments
-
Richard Bustos over 2 years
Just started working with Node.js. In my
app/js
file, I am doing something like this:app.js
var http = require('http'); http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('Am I really running a server?!'); }).listen(8080, '127.0.0.1'); console.log('running server!');
When I'm in my terminal and run
node app.js
, the console spits out'running server!'
, but in my browser I get,Uncaught ReferenceError: require is not defined
.Can someone explain to me why in the terminal, it works correctly but in the browser, it doesn't?
I am using the node's
http-server
to serve my page. -
jfriend00 over 5 years@N73k - I won't take that as a serious question. If you want to read about node.js, what it is and what people use it for, that's a completely different topic and you can start by doing your own searches on the web and find all sorts of articles. Javascript is a real OOP language. Perhaps different than what you think you want, but that's only your opinion and there are many who do not share your opinion for a variety of reasons. This is not the place to debate or discuss that.
-
Youssef Boudaya over 5 yearsi have the same problem and installed both browserify and webpack but the problem persists !!
-
Rob Raisch over 5 yearsWithout knowing your particular configuration, I cannot help. Best to create a new question with your specific problem.
-
Ivan Olshansky about 5 yearsSo what new information is in your answer? It looks just as "me too"-comment.
-
Joshua Wade almost 4 yearsThis can happen with anything in your project folder. So if you have an unrelated build script written with
CommonJS
imports but aproject.json
with"type": "module"
, then the build script will break, even if it's not referenced in thepackage.json
. -
codedread almost 4 yearsYou can still use require in a module using createRequire
-
Mike Kormendy over 3 yearsHow can we add this in TypeScript so it adds this in our compiled JS after?
-
Milind over 3 yearsI am using nvm, so i switched from node 14 to node 10, and this error was fixed.
-
jfriend00 over 3 years@HasinduDahanayake - What is the point of that inflammatory statement? JS is a very real object oriented language. Everything in the language is an object with methods, with classes, with an ability to extend classes or objects. It is loosely typed, but that doesn't make it any less OO.
-
Tibor Udvari almost 3 yearsYou can also rename the file extension to
.cjs
-
Jeb50 over 2 yearsUsing Node 14, taking out
"type": "module"
from package.json results(node:7396) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs
. I don't have any .mjs -
Jeb50 over 2 yearsUsing Node 14, taking out
"type": "module"
from package.json results(node:7396) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs
. I don't have any .mjs -
Jasdev Singh Moun over 2 yearsit doesn't help.
-
Piotr Henryk Dabrowski over 2 yearsThat way you can't use
import
.