Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

json

node.js

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

like image 909
notme Avatar asked Oct 30 '25 04:10

notme


1 Answers

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"
  },
  ...
}
like image 129
Mbrevda Avatar answered Nov 01 '25 13:11

Mbrevda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!