<!DOCTYPE html> <html> <head> <title>RequireJS on Browser</title> <script data-main="scripts/main" src="scripts/require.js"></script> </head> <body> <h1 id="header">This is body</h1> </body> </html>
Here the data-main attribute is used by RequireJS to load a specified js file inside the data-main attribute just after the require.js file is loaded. Here in the above example, main.js is loaded after require.js.
Your project structure should be as:
Uncaught ReferenceError: required is not defined
The require() function is available on NodeJS only but if you want it to be used on the browser you have to add the require() function on the browser by using the ReactJS library.
Uncaught ReferenceError in Node.js
To solve uncaught ReferenceError in Node.js:
- Change the module type in package.json form module to commonjs: “type”:”commonjs”.
- You can delete the whole “type”:”module” string from package.json.
- You can change require to import.
// const express = require('express'); import express from 'express';
That’s it for this tutorial.