Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught ReferenceError: process is not defined

I am using node.js to create a web application. When I run the application (either by opening index.html on the browser or using the command "npm start" on the terminal) I get two errors:

Uncaught ReferenceError: process is not defined

Uncaught ReferenceError: require is not defined

I solved the "require is not defined" error by specifically including in my index.html head tag the link to this script, where the require function is defined. However, I cannot find something similar for the process function.

My question is doublefold:

  1. Why do built-in node.js modules need to be re-defined? Why are they not recognized as they are, that is "built-in modules"? Doesn't the term "built-in module" mean that a module need not be redefined externaly/second-handedly?

  2. Is there a way to solve this problem? My script is very simple, I am just trying to use a basic function of node.js, so I cannot figure out what errors I might have done.

If anyone has come about this problem and has found a way around it or a reason this happens, you would be of great help.

like image 381
Kantharis Avatar asked May 14 '15 13:05

Kantharis


People also ask

What does “process is not defined” mean?

» The Uncaught ReferenceError: process is not defined happens when when a non-existent (here: process) variable is referenced . Most likely the problematic code snippet looks like:

How do I fix process is not defined in react?

So to solve the Uncaught ReferenceError: process is not defined in React, open your terminal in your project’s root directory and update the version of your react-scripts package by running npm install react-scripts@latest and re-install your dependencies if necessary. How to preload a sound in Javascript?

Why am I getting an uncaught ReferenceError in create react app?

A recent change to Create React App, or more specifically react-scripts, has caused hot reloading to throw an error: Uncaught ReferenceError: process is not defined. This is due to its dependency, react-error-overlay, being referenced as ^6.0.9 while react-error-overlay ’s latest patch version 6.0.10 is a breaking change.

Why is “process” not defined in the browser in NodeJS?

In NodeJS, “process” is defined, but not in the browser. This is because NodeJS and the browser are different runtime environments. This error is common in many frontend projects. You might have encountered it a few times, and it might come from a line of code like this:


1 Answers

Node.js code must be run by the node process, not the browser (the code must run in the server).

To run the code, you must run the command:

node server.js 

And then you can access your server from a browser by typing "http://localhost:8080", for example. You must have a file server.js (or whatever) with the server code you want (in this case, creating a web server in port 8080).

You can follow this easy example, using express as http server module: http://expressjs.com/starter/hello-world.html

like image 81
greuze Avatar answered Sep 22 '22 21:09

greuze