notme
notme

Reputation: 444

Get the value of another field in package.json file (npm)

I have the following package.json file:

{
  "main": "./dist/index.js",
  "scripts": {
    "build": "tsc",
    "build:live": "tsc && node ./dist/index.js"
  },
  ...
}

How can I get the value of the main field, then put it into the command node ./dist/index.js, something like this (just a example, I know this $ is for ENV variables):

{
  "main": "./dist/index.js",
  "scripts": {
    "build": "tsc",
    "build:live": "tsc && node $main"
  },
  ...
}

Thanks in advance

Upvotes: 4

Views: 740

Answers (1)

Mbrevda
Mbrevda

Reputation: 3090

All package.json fields are available as variables by prefixing them with $npm_package_. In your example, you can use this:

{
  "main": "./dist/index.js",
  "scripts": {
    "build": "tsc",
    "build:live": "tsc && node $npm_package_main"
  },
  ...
}

Upvotes: 3

Related Questions