. Advertisement .
..3..
. Advertisement .
..4..
While writing JavaScript code, I received the “ReferenceError: require is not defined”.
......... ADVERTISEMENT .........
..8..
What is the cause of the problem and how to solve it quickly? I need the help of computer experts to fix this error. Thank you very much.
The cause:
There are likely one of two reasons for your “ReferenceError: require is not defined”:
-You attempted to use
require
in a browser setting.-Despite being in a Node.js environment, your project’s
package.json
file specifies “type”: “module.”Solution:
JavaScript in the browser does not allow the use of
require
, thus you will need to find a replacement or a technique to make that work, such as by utilizing Browserify.This example uses the syntax for the ES6 import and export module:
You can also use export default on anonymous functions in the
subtract.js
module shown above:Read more:
→ Uncaught referenceerror: require is not defined error. How to fix it