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.

  1. i haven't used personally, can use node config value specified here.

node: { fs: {true, "mock", "empty", false} }

set fs of above values.

  1. 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

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -