Package Exports
- npm-package-env
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (npm-package-env) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
npm-package-env
conveniently get
npm_package_variables using property accessors
installation
npm i npm-package-env -Soverview
this package provides easy access to package.json variables (or
per-package config settings) via property accessors - both .dot and
[bracket] notation are supported.
API
.<property> / [<'property-name'>] (property accessors)
used to walk deeper into the package.json object tree. equivalent to
stating the full variable path in process.env.npm_package_<var_path>.
Returns: {I} a self reference (chainable), bound to the new namespace.
<property>(type, [...indices]) (function invocation)
converts the variable value in the current namespace to the specified type and returns it.
type {String} (optional) the result type, one of: 'string', 'array'
or 'object'. if type is not passed it defaults to 'string'.indices {*} (optional) when passing 'object' as the type,
this is a whitelist of property keys that will be included in the result
object.
Returns: {String|Array|Object} the current variable value as the
specified type, or undefined if no such variable was found.
examples
all examples assume my-npm-script.js is run via npm run, i.e. the
package.json has something like this:
"scripts": {
"start": "my-npm-script"
}getting a string
my-npm-script.js
const npmEnv = require('npm-package-env');
// with implicit type inference (no args)
npmEnv.config['dev-server'].port(); // -> '7777'
// with explicit type inference
npmEnv.config['dev-server'].port('string'); // -> '7777'package.json
"config": {
"dev-server": {
"port": 7777
}
}getting an array
my-npm-script.js
const npmEnv = require('npm-package-env');
npmEnv.keywords('array'); // -> ['foo', 'bar', 'wat']
npmEnv.keywords('array')[1]; // -> 'bar'package.json
"keywords": [
"foo", "bar", "wat"
]getting an object by keys
my-npm-script.js
const npmEnv = require('npm-package-env');
npmEnv.dependencies('object', ['auto-exports']); // -> {'auto-exports': '14.1.3'}package.json
"dependencies": {
"auto-exports": "14.1.3",
"npm-package-env": "^2.0.21"
}