Uncaught ReferenceError: require is not defined = node.js install

I am trying to get node.js running. When I execute a REQUIRE, I get the error Uncaught ReferenceError: require is not defined


const fs = require('fs');  // file system module


     fs.writeFile("test.txt", "Hey there", function(err) {
        if(err) {
            return console.log(err);
        console.log("The file was saved!");

If I run straight node.js it works

If I run this on an HTML page I get 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 25, 2023

Hi there,

The require function is specific to Node.js and is used to import modules in a CommonJS environment. When you’re running JavaScript in a browser, this function isn’t defined, leading to the Uncaught ReferenceError: require is not defined error.

If you want to use Node.js modules like fs in a browser environment, you’ll need to use a tool like Browserify or Webpack, which can bundle your Node.js code into a format that’s suitable for the browser. These tools can’t handle some Node.js-specific modules like fs (since filesystem access is not available in browsers), so you’ll have to reconsider your approach if you need to use such modules.

Here’s a brief overview of how you might use Browserify to make your code browser-compatible:

  1. Install Browserify: If you haven’t installed Browserify yet, you can do so using npm:

    npm install -g browserify
  2. Create an Entry File: Move the JavaScript code you want to run in the browser to a separate file, like main.js.

  3. Modify Your Code: Remove or replace code that is specific to Node.js and won’t run in a browser. This includes the fs module usage in your example.

  4. Bundle Your Code: Run Browserify on your entry file to create a bundle:

    browserify main.js -o bundle.js
  5. Include the Bundle in Your HTML: You can now include bundle.js in your HTML file using a <script> tag:

    <script src="bundle.js"></script>

You need to keep in mind that Node.js modules like fs that depend on server-side features won’t be usable in a browser environment, even with Browserify or similar tools. You may need to find a client-side alternative or rearchitect your application to perform these operations on the server and communicate with the client using APIs or other methods.

Feel free to share more information about your use-case and I will be happy to advise you further!



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