https://github.com/lutangar/extract-npm-package-config
Extract npm package config variables from `process.env` for in-app usage
https://github.com/lutangar/extract-npm-package-config
config env npm webpack
Last synced: 2 months ago
JSON representation
Extract npm package config variables from `process.env` for in-app usage
- Host: GitHub
- URL: https://github.com/lutangar/extract-npm-package-config
- Owner: lutangar
- License: mit
- Created: 2017-12-05T14:24:10.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-06T15:04:46.000Z (over 8 years ago)
- Last Synced: 2025-02-18T19:11:19.224Z (over 1 year ago)
- Topics: config, env, npm, webpack
- Language: JavaScript
- Size: 19.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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"
},
...
```
1. As long as you use `npm run ...` scripts this config will be available in the `process.env` variables.
2. Each of these variables can be referenced inside the `package.json` for usage with other scripts.
3. Can easily be overridden via `env` variables server-side.
# Install
`npm install --save-dev extract-npm-package-config`
# Usage
## Standalone
```
import extractPackageConfig from 'extract-npm-package-config';
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](https://webpack.js.org/plugins/environment-plugin/) 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.DefinePlugin` replace the variables in the source code itself,
>thus this would defeat the purpose of having `env` variables that can be easily overridden.
> **Note 2:** There is currently a limitation due to the way `weback.DefinePlugin` replace process.env occurrences in the code.
> You can't *destructure* `process.env` see https://github.com/webpack/webpack/issues/5392
# Licence
MIT