This question already has answers here:
Client on Node.js: Uncaught ReferenceError: require is not defined
(11 answers)
Closed 3 years ago.
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?
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.
Related
This question already has answers here:
"Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6
(31 answers)
Closed 2 years ago.
I'm using the latest version of node.js and Chrome.
Node.js still relies on CommonJS to do modules (exports and imports).
The CommonJS page that was recommended to me actually says CommonJS is not suitable for client-side and to use ES modules instead. This makes me think I should use them for my websites that I'm creating. Is it possible to do this?
The errors I get when I try to are 'Uncaught SyntaxError: Unexpected token 'export' & unexpected token export' & 'Uncaught SyntaxError: Cannot use import statement outside a module.'
Here are the errors I get when I include type="module":
Write <script type="module"> instead of <script>.
This question already has answers here:
Client on Node.js: Uncaught ReferenceError: require is not defined
(11 answers)
Closed 2 years ago.
I need to have a colormap on my javascript page.
I am using Flask for server side (written in python).
I have a javascript file that requires colormap module (installed using "npm install colormap"), for rendering colormaps on the client side.
I tried to import it from my javascript file index.js
var colormap = import("colormap");
which I got the error:
index.js:2 Uncaught (in promise) TypeError: Failed to resolve module specifier 'colormap'
at index.js:2
and I also tried:
let colormap = require('colormap')
and I got the error require is not defined.
how can I solve it?
I am trying to import the js file with colormap directly into my html (so the javascript file will have reference), but I cant find what file should I import.
The NPM package colormap is not set up well for just importing into a client-side JS script.
If it was, you could just include it in a script tag as described here.
Because it is not, you will have to use Browserify to convert the JS into a file that you can then serve and use in your JS.
This question already has answers here:
Client on Node.js: Uncaught ReferenceError: require is not defined
(11 answers)
Closed 4 years ago.
Edit : Problem solved using webpack
For the needs of an API, I needed to import MD5 and moment. I downloaded the packages using the basic npm install but when I try to import it on my app.js using the code below :
const md5 = require ('./node_modules/md5/md5.js');
const moment = require ('./node_modules/moment/moment.js');
function getTimeStamp () {
return moment.utc ().format ('YYYYMMDDHHmmss');
}
let timestamp = getTimeStamp ();
function generateSignature (devId, method, authKey, timestamp) {
return md5 (`${devId}${method}${apiKey}${timestamp}`);
}
let signature = generateSignature (XXXX, "createsession", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", getTimeStamp ());
I get this message in the console :
Uncaught ReferenceError: require is not defined
I don't know what I'm doing wrong because I used the same method for another program and it worked perfectly...
Thanks in advance
You're probably seeing this error because require() does not exist in the browser/client-side JavaScript.
If you want to use require() in the browser, then you need to use something like require.js
RequireJS is a JavaScript file and module loader. It is optimized for
in-browser use, but it can be used in other JavaScript environments,
like Rhino and Node.
PS: I agree with cptwonton. Please refer to the mentioned post for an in-depth solution with the various options available.
try this:
const md5 = require ("md5");
const moment = require ("moment");
require isn't supported in a browser because Node and ES6 have their different module systems. Are you trying to call require in a browser? In that case I suggest you to setup Babel. But if you use node, then try reistalling nodejs.
This question already has answers here:
Importing an external module in TS causes "ReferenceError: require is not defined"
(2 answers)
Closed 6 years ago.
I read official doc for TypeScript, and I copy code from there.
I install commonjs and requerejs.
"devDependencies": {
...
"commonjs": "latest",
"requirejs": "latest"
}
But I get a error in the browser:
ReferenceError: require is not defined
It is index.html:
<!DOCTYPE html>
<body>
<script src="main.js"></script>
</body>
It is main.js after compiling:
"use strict";
var core_1 = require("./some_dir/some_file");
window.onload = function () {
var some = new some_file_1.SomeClass();
};
Browsers don't have a understanding of modules (yet). That means while require() works if you execute it in node only, browsers don't know what to do with it.
There are 2 main methods of fixing this:
Remove the exports/require statements and include every single js file (in correct dependency order) into your html file (as script tags). They will all have the same scope and will work as if there was just one large concatenated file. This solution is pretty bad tho as it destroys scoping, doesn't really work with modules you got with npm and has bad loading times, as it has to fetch multiple js files from the server.
Use a resource bundler like webpack. Webpack will look at all dependant files starting from your main.js file and bundle and compress them down into a single small .js file. This also works for external dependencies (npm). All you have to do then is to include that single bundle.js file into the html.
This question already has answers here:
Using Node.js require vs. ES6 import/export
(11 answers)
Closed 6 years ago.
Right now I'm importing modules into node project as
import * as name from "moduleName";
Im not doing it as
var name = require("moduleName");
as we used to be in the node project, earlier
my question is there difference in the writing a module when we import using require or import, are modules written internally are same just we are importing in different way or its something internal that forces us to use require or import when importing
and what is the difference between require and import(es6)
Thanks!
import runs at the beginning of the file and it would be already
loaded before the code itself runs.
require on the other hand is run
inline and can be inserted within the code conditionally.