npm-package-env

0.4.9 • Public • Published

npm-package-env

conveniently use npm_package_ variables using property accessors

NPM

installation

npm i npm-package-env -S

overview

provides easy access to per-package config settings (or package variables) via a virtual object.

API

.<property> / [<'property-name'>] (property accessors)

get

retrieves the current value or walks deeper into the npm_package_ tree. equivalent to stating the full variable path in process.env.npm_package_<var_path>.

Returns: {String|I} the current value, if exists, or a chainable object, bound to the new namespace.

set

sets a new value in the specified namespace, coerced to a string. equivalent to assigning a variable value to process.env.npm_package_<var_path>.

examples

all examples assume npm-script.js is run via npm run, i.e. the package.json has something like this:

"scripts"{
    "start": "npm-script"
}

getting / setting a value

npm-script.js
const npmEnv = require('npm-package-env');
npmEnv.config['dev-server'].port; // -> '7777'
npmEnv.config['dev-server'].port = 9999;
npmEnv.config['dev-server'].port; // -> '9999'
package.json
"config"{
    "dev-server": {
        "port": 7777
    }
}

getting / setting a value inside an array

npm-script.js
const npmEnv = require('npm-package-env');
npmEnv.keywords[1]; // -> 'bar'
npmEnv.keywords[1] = 'wow';
npmEnv.keywords[1]; // -> 'wow'
package.json
"keywords"[
    "foo", "bar", "wat"
]

getting / setting a value inside an object

npm-script.js
const npmEnv = require('npm-package-env');
npmEnv.dependencies['auto-exports']; // -> '14.1.3'
npmEnv.dependencies['auto-exports'] = '~2.0.1';
npmEnv.dependencies['auto-exports']; // -> '~2.0.1'
package.json
"dependencies"{
    "auto-exports": "14.1.3",
    "npm-package-env": "^2.0.21"
}

Package Sidebar

Install

npm i npm-package-env

Weekly Downloads

2

Version

0.4.9

License

MIT

Last publish

Collaborators

  • eliranmal