Uncaught ReferenceError: require is not defined

I am tyring to get node.js working and when I call a ‘require’, I get the following error Uncaught ReferenceError: require is not defined

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
August 4, 2023

Hi there,

This error usually occurs when trying to use Node.js’s require() function in a context where it isn’t defined. This is common when you try to use require() in client-side JavaScript, which runs in a web browser.

Web browsers don’t support the require() function natively because it is part of Node.js’s module system (CommonJS), not part of standard JavaScript. To use modules in client-side JavaScript, you would generally use the ES6 import statement. However, this assumes that the script you’re importing is an ES6 module, and it must be served from a web server due to CORS (Cross-Origin Resource Sharing) policies in browsers.

If you’re trying to use a Node.js library in client-side code, you will often need to use a tool like Browserify or Webpack. These tools bundle your code and its dependencies into a single JavaScript file that can be included in your HTML.

If you are indeed trying to use Node.js and the error persists, it could be due to several reasons:

  1. The script is not running in a Node.js environment. Make sure you’re running your script with Node.js, e.g. node your_script.js in the command line.

  2. The module you’re trying to require is not installed. If you’re trying to require a third-party module, make sure it’s installed using npm install module_name.

  3. The path to the module is incorrect. If you’re trying to require a local file, make sure the path to the file is correct. For example, if you’re in the same directory as the file, the require statement would be require('./filename').

If none of these solutions work, feel free to share more details about your exact setup!



Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel