我需要使用脚本加载器从 node_modules 加载脚本。我尝试在我的 client_entry.js 中执行以下要求
require('script-loader!canvas-to-blob');
但我在浏览器中收到以下错误:
VM1702:1Uncaught ReferenceError: module is not defined
at eval (eval at module.exports (http://patientwisdom-dev.com:8080/index.js:164122:9), <anonymous>:1:1)
at eval (<anonymous>)
at module.exports (http://patientwisdom-dev.com:8080/index.js:164122:9)
at Object.<anonymous> (http://patientwisdom-dev.com:8080/index.js:164108:26)
at __webpack_require__ (http://patientwisdom-dev.com:8080/index.js:20:30)
at Object.<anonymous> (http://patientwisdom-dev.com:8080/index.js:669:2)
at __webpack_require__ (http://patientwisdom-dev.com:8080/index.js:20:30)
at Object.<anonymous> (http://patientwisdom-dev.com:8080/index.js:48:19)
at __webpack_require__ (http://patientwisdom-dev.com:8080/index.js:20:30)
at http://patientwisdom-dev.com:8080/index.js:40:18
我也试过:
require('script-loader!canvas-to-blob/index.js');
仍然没有运气
仅供引用,这适用于具有相对路径的 js 文件:
require('script-loader!./test.js'); // works
但不适用于来自 node_modules 的东西
最佳答案
您只需要完整的相对路径,包括 node_modules
.
根据您项目中的位置,您的 js 文件相对于 node_modules
它可能看起来像这样。
require('script-loader!../../node_modules/canvas-to-blob/index.js');
关于javascript - 如何使用 webpack 从 node_modules 加载脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40869649/