user11321870
user11321870

Reputation:

I got error when link external library with javascript

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?

Upvotes: 0

Views: 253

Answers (1)

ctt
ctt

Reputation: 1435

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.

Upvotes: 1

Related Questions