Is it possible to reference a property in package.json

JSON itself doesn't support variables.

It's up to the program consuming JSON that can decide whether to treat any particular pattern as a variable or to be replaced with some other text somehow.

While other answers have mentioned using the $ or %% notation for variables (that are OS-dependent), I think you can also solve your problem in the following way:

Instead of nodemon app.js you can just write nodemon .:

"main": "app.js",
"scripts": {
  "dev": "nodemon ."
}

. will also automatically resolve to app.js

If you have a "main": "app.js" in package.json (in any folder, be it top level or sub folders) then any node process will identify the app.js file as the default one to load (either in require calls or executing via cli), just like it does index.js automatically.


Yes, you can reference to any field value from package.json when executing scripts.
But there is a difference, when you run script under windows, you should use %npm_package_field% and with unix based OS you should use $npm_package_field.
Where field is field name from package.json.

Under windows, you can use:

"dev": "./node_modules/.bin/nodemon %npm_package_main%"

Under unix:

"dev": "./node_modules/.bin/nodemon $npm_package_main"

You can do it through environment variables

Under Linux

"scripts": {        
    "dev": "./node_modules/.bin/nodemon $npm_package_main"
  },

Under Windows

"scripts": {        
    "dev": "./node_modules/.bin/nodemon %npm_package_main%"
  },