JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 24
  • Score
    100M100P100Q28507F
  • License MIT

conveniently get npm_package_ variables using property accessors

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

NPM

installation

npm i npm-package-env -S

overview

this package provides easy access to package.json variables (or per-package config settings) via property accessors - both .dot and [bracket] notation are supported.

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')._in('config');
npmEnv['dev-server'].port._as('string'); // -> '7777'
package.json
"config": {
    "dev-server": {
        "port": 7777
    }
}

getting an array

my-npm-script.js
const npmEnv = require('npm-package-env');
npmEnv.keywords._as('array'); // -> ['foo', 'bar', 'wat']
npmEnv.keywords._as('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._as('object', ['auto-exports']); // -> {'auto-exports': '14.1.3'}
package.json
"dependencies": {
    "auto-exports": "14.1.3",
    "npm-package-env": "^2.0.21"
}

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).

_as(type, [...indices])

converts the current variable value to the specified type and returns it.

type {String} the result type, one of: 'string', 'array' or 'object'.
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.

_in([...prefixes])

initializes a new instance in the specified location, starting from npm_package_. for example, to start every next property access from npm_package_config_server_auth:

let authConfig = npmEnv._in('config', 'server', 'auth');
authConfig.user._as('string');
authConfig.policies._as('array');

prefixes {*} (optional) a list of prefix segments to bind to the new instance.

Returns: {I} a new instance, bound to the passed prefix.