如何使用Webpack从node_modules中加载脚本

4

我需要使用 script-loader 从 node_modules 加载一个脚本。我已经尝试在我的 client_entry.js 中执行以下 require:

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的内容

1个回答

1

您只需要包括node_modules的完整相对路径。

根据您的js文件相对于node_modules所在的项目位置,它可能看起来像这样。

require('script-loader!../../node_modules/canvas-to-blob/index.js');

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接