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.
Related
This question already has answers here:
ESM importing all files from folder with index.js not working
(2 answers)
Closed 2 years ago.
I'm using node.js v14.8.0 and ES6 style module imports.
My code looks like this:
import myModule from "./my-module";
Expected behaviour:
The file ./my-module/index.js is imported.
Actual behaviour:
Node.js throws the ERR_UNSUPPORTED_DIR_IMPORT error.
Question:
How can I configure node.js to allow directory imports and resolve them using the index.js file?
In short: "vanilla" node.js doesn't support this.
We decided to use babel, which allows for this notation.
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.
I installed the color-convert library via npm but the browser shows me an error message
Uncaught ReferenceError: require is not defined home.js:134
at HTMLButtonElement.<anonymous> (home.js:134)
at HTMLButtonElement.dispatch (jquery-3.4.0.js:5233)
at HTMLButtonElement.elemData.handle (jquery-3.4.0.js:5040)
JS
var convert = require('color-convert'); // this is line 134
alert(convert.hex.lab('DEADBF'));
I think there is a problem with paths?
require() isn't a function provided by your browser, and is more of a sign that this source code is a common JS module.
In order to use a common JS module, you first need to run your source through a program that bundles the source, sorta replacing each require('other_module') with the source of the other module, producing a single Javascript source file which can included in your frontend HTML.
Two examples of bundlers are browserify and webpack.
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.