npm config variables

1 min read  •  27 Oct 2022
view-count

In javascript projects, we can set config object in the package.json which takes config variables which we can use in our script declarations.

Let's take an example, here's a minimal project with following package.json

{
  "name": "app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "config": {
    "port": 3005
  },
  "scripts": {
    "start": "echo My port is $npm_package_config_port"
  },
  "author": "Akul Srivastava",
  "license": "ISC"
}

We can use this variable in our scripts by adding $npm_package_config_ prefix to it, as shown above. Here's the output for the same-


config-usecase-script-output