Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

require is not defined? Node.js

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.

like image 869
Richard Bustos Avatar asked Aug 11 '15 01:08

Richard Bustos


People also ask

What is the difference between require () and Node JS?

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 ().

What does require is not defined mean in JavaScript?

Sometimes, JavaScript may suddenly give you a require is not defined error like this: Uncaught ReferenceError: require is not defined. This usually happens because your JavaScript environment doesn’t understand how to handle the require () function reference.

Why is require () not built into the browser in Node JS?

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 ().

Why can't I use require () in JavaScript?

This is because require () does not exist in the browser/client-side JavaScript. Now you're going to have to make some choices about your client-side JavaScript script management. Use the <script> tag. Use a CommonJS implementation. It has synchronous dependencies like Node.js


2 Answers

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.

like image 199
Abel Avatar answered Sep 28 '22 01:09

Abel


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.

like image 32
jfriend00 Avatar answered Sep 28 '22 00:09

jfriend00