Ian McNally

Variables in npm scripts

February 04, 2016

Did you know you can have variables in your package.json? They can be very handy, especially when using npm scripts. Consider the following:

{
  "main": "index.js",
  "scripts": {
    "start": "node $npm_package_main"
  }
}

It eliminates the need for copy-pasting. Any key can be referenced, beginning with $npm_package_ and adding an underscore for every level you go down. Say you had a config object, where you stored reusable values, like a domain address, that’s passed as an argument in a script:

{
  "main": "index.js",
  "config": {
    "domain": "localhost:8000"
  },
  "scripts": {
    "start": "node $npm_package_main --domain $npm_package_config_domain"
  }
}

Super useful!


Ian McNally

Hey, I'm Ian. I build websites and write about what I learn as I go. Follow me on Twitter.