简体   繁体   中英

how to use webpack to load script from node_modules

I need to use the script-loader to load a script from node_modules. I've tried doing the following require in my 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:

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

but not for stuff from node_modules

You just need the complete relative path including node_modules .

Depending on where in your project your js file is relative to node_modules it might look something like this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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