erikras / react-redux-universal-hot-example

A starter boilerplate for a universal webapp using express, react, redux, webpack, and react-transform
MIT License
12.01k stars 2.5k forks source link

How to use `nconf` module in config.js #1336

Closed ghost closed 7 years ago

ghost commented 7 years ago

Tried to add the following code

let externalConfig = {};

console.log('*** process.env.NODE_ENV', process.env.NODE_ENV);

if (process.env.NODE_ENV !== 'production') {
  console.log('--- client side code');
  externalConfig = require('./configDev.json');
}
else {
  console.log('--- production');
  const nconf = require('nconf');
  const path = require('path');

  nconf.env({separator: '__'}).file({file: path.join(__dirname, 'config.json')});
  externalConfig = nconf.get();
}
....
module.exports = Object.assign(
   {...}
  environment,
  externalConfig
}

In the dev mode I have the following

ERROR in ./~/nconf/lib/nconf.js
Module not found: Error: Cannot resolve module 'fs' in /Users/dsmiljanic/Projects/xxx/dashboard.web/node_modules/nconf/lib
 @ ./~/nconf/lib/nconf.js 8:9-22
ERROR in ./~/nconf/lib/nconf/common.js
Module not found: Error: Cannot resolve module 'fs' in /Users/dsmiljanic/Projects/xxx/dashboard.web/node_modules/nconf/lib/nconf
 @ ./~/nconf/lib/nconf/common.js 8:9-22
ERROR in ./~/nconf/lib/nconf/stores/file.js
Module not found: Error: Cannot resolve module 'fs' in /Users/dsmiljanic/Projects/xxx/dashboard.web/node_modules/nconf/lib/nconf/stores
 @ ./~/nconf/lib/nconf/stores/file.js 9:9-22
ERROR in ./~/nconf/~/yargs/lib/completion.js
Module not found: Error: Cannot resolve module 'fs' in /Users/dsmiljanic/Projects/xxx/dashboard.web/node_modules/nconf/node_modules/yargs/lib
 @ ./~/nconf/~/yargs/lib/completion.js 1:9-22

If the production the code

  const nconf = require('nconf');
  const path = require('path');

  nconf.env({separator: '__'}).file({file: path.join(__dirname, 'config.json')});
  externalConfig = nconf.get();

is commented out, everything is fine (in dev mode)

Please, could you tell me how can I solve this issue properly. Thanks

jdosullivan commented 7 years ago

It's an isomorphic app. fs isn't available on the browser so you need to instruct webpack to only look for fs when constructing the server bundle.

Try adding this to your webpack config:

node: { fs: "empty" }

ghost commented 7 years ago

Thank you David. The complete solution was

// dashboard.web/webpack/dev.config.js
...
module.exports = {
...
node: { fs: "empty", child_process: "empty" },
...
};