0

I try to use require in JS but I get the following error Uncaught ReferenceError: require is not defined I have to files. main.js:

const count = 9; module.exports = count;

index.js:

const count = require('./index'); console.log(count);

How to use require?

1 Answers1

1

Require is a NodeJS thing. Are you trying this in NodeJS? Or in browser Javascript. I suspect the latter. In that case you have to include your files withing your HTML using <script src="./index.js"></script> and <script src="./main.js"></script> instead. Within NodeJS require should always be possible unless your installation is corrupted in a really weird way.

bdbdbd
  • 416
  • 5
  • 13