How To Fix Example Javascript Code; Error: Referenceerror: Require Is Not Defined?
I am trying to run some example code linked here, but upon start I get this error: ReferenceError: require is not defined I know, require.js is some package for javascript, but i
Solution 1:
The code you linked is written for NodeJS
But as the readme suggests you can get it to run if you package it using browserify (https://github.com/substack/node-browserify)
Assuming you have NodeJS installed, you need to install browserify globally using NPM (Node Package Manager that comes with NodeJS)
Open a console and run npm install -g browserify
- This will add the browserify command to your console.
Next up we need to create our on Node Package
- Make a new directory
- cd into that directory and run
npm init
- this will start a guide that will ask you to fill in some information about your package and creates apackage.json
file - run
npm install --save bpostlethwaite/colormap
this will install the colormap library you are interested in - Create an index.js file (assuming you left that unchanged when you run npm init)
- Paste the example code from the read me in index.js
- Finally run
browserify -s index.js > bundle.js
- This will generate a bundle.js file that can run in a browser
Hope that helps
Solution 2:
To solve the last issue that you get Use require([])
you must to define like this:
require(['colormap'], function() {
// colormap is now loaded
});
Post a Comment for "How To Fix Example Javascript Code; Error: Referenceerror: Require Is Not Defined?"