简体   繁体   English

如何使用webpack从node_modules加载脚本

[英]how to use webpack to load script from node_modules

I need to use the script-loader to load a script from node_modules. 我需要使用script-loader从node_modules加载脚本。 I've tried doing the following require in my client_entry.js 我已尝试在client_entry.js中执行以下要求

require('script-loader!canvas-to-blob');

but I get the following error in my browser: 但是我的浏览器出现以下错误:

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

I also tried: 我也尝试过:

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

still no luck 仍然没有运气

fyi, this works for js files with a relative path: fyi,这适用于具有相对路径的js文件:

require('script-loader!./test.js'); // works

but not for stuff from node_modules 但不是来自node_modules的东西

You just need the complete relative path including node_modules . 您只需要完整的相对路径,包括node_modules

Depending on where in your project your js file is relative to node_modules it might look something like this. 根据项目中js文件相对于node_modules它可能看起来像这样。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM