javascript - Webpack use node 'fs' module during buildtime so browser gets the result of function -
question: there way tell webpack tell built-in modules modules fs execute during build browser gets result of function, not function call itself?
my situation:
currently i'm developing application browser using webpack. i'm trying use node 'fs' module in 1 files require index.js
files other directories. example:
plugins ├──plugin1 │ ├── index.js (simply exports object) │ ├──plugin2 │ ├── index.js (simply exports object) | ├──plugin3 │ ├── index.js (simply exports object) | |──index.js (want require index.js each plugin directory here)
i'm getting error webpack saying: can't resolve 'fs' in somepath/node_modules/require-dir
my file index.js
located @ `plugins/index.js' trying require other files.
//module npm uses 'fs' module ('im not explicity using it) const requiredir = require('require-dir'); const allplugins = requiredir('./plugins/'); console.log(allplugins);
can't resolve 'fs' in '/some_path/node_modules/require-dir'
you have 2 options here.
- i haven't used personally, can use
node
config value specified here.
node: { fs: {true, "mock", "empty", false} }
set fs
of above values.
- don't use
fs
module. built/native modules may or may not rely on native v8/c++ functions/libraries. remember webpack typically bundles assets browser. instead of relying on plugin, can manually import plugins like:
plugins/index.js
const plugin1 = require('./plugin1') const plugin2 = require('./plugin2') module.exports = { plugin1, plugin2 }
you use this answer polyfill require-dir
module.
Comments
Post a Comment