extract-npm-package-config v0.1.2
extract-npm-package-config
This package can extract npm package config variables from process.env for in-app usage.
Npm per-package configuration setting is an underrated feature.
https://docs.npmjs.com/misc/config#per-package-config-settings
package.json
...
"config": {
"scheme": "http",
"domain": "localhost",
"port": "3001"
},
...- As long as you use
npm run ...scripts this config will be available in theprocess.envvariables. - Each of these variables can be referenced inside the
package.jsonfor usage with other scripts. - Can easily be overridden via
envvariables server-side.
Install
npm install --save-dev extract-npm-package-config
Usage
Standalone
const packageConfig = extractPackageConfig(process.env);npm_package_config_domain: 'localhost' npm_package_config_port: 3000
Webpack
It can easily be used in conjunction with webpack EnvironmentPlugin client-side.
webpack.config.js
...
plugins: [
new webpack.EnvironmentPlugin(extractPackageConfig(process.env))
]
...Note 1: You should'nt make use of it server side, because
weback.DefinePluginreplace the variables in the source code itself, thus this would defeat the purpose of havingenvvariables that can be easily overridden.Note 2: There is currently a limitation due to the way
weback.DefinePluginreplace process.env occurrences in the code. You can't destructureprocess.envsee https://github.com/webpack/webpack/issues/5392
Licence
MIT