我安装了the color-convert library通过 npm 但浏览器向我显示错误消息
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'));
我认为路径有问题?
最佳答案
require()
不是浏览器提供的函数,它更多地表明此源代码是一个通用 JS 模块。
为了使用通用 JS 模块,您首先需要通过一个捆绑源代码的程序来运行源代码,并替换每个 require('other_module')
与其他模块的源代码一起生成一个 Javascript 源文件,该文件可以包含在您的前端 HTML 中。
bundler 的两个示例是 browserify
和 webpack
。
关于javascript - 使用 javascript 链接外部库时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56026051/