0

I cannot figure out how to use variables in npm scripts on Windows 8, npm 3.10.10 and node 6.11.1.

package.json:

{
  "name": "my-project",
  "scripts": {
    "start": "echo $npm_package_name"
  }
}

and npm start outputs:

> echo $npm_package_name

$npm_package_name

For some reason, the variable does not substituted. Any help will be greatly appreciated!

Articles being read- https://docs.npmjs.com/misc/scripts and variables-in-npm-scripts

Asen Arizanov
  • 930
  • 1
  • 9
  • 11

1 Answers1

2

Variables on Windows should be surrounded/escaped with % %. The example above should be:

{
  "name": "my-project",
  "scripts": {
    "start": "echo %npm_package_name%"
  }
}

More info could be found here.

Asen Arizanov
  • 930
  • 1
  • 9
  • 11
  • 1
    There is no cross platform solution to this problem? I develop on Windows and MacOS workstations, test locally, and deploy to Linux servers. – Dan Oct 10 '19 at 14:51
  • @Dan doesn't look like there's a built-in way to handle this cross platform, but there [are packages](https://www.npmjs.com/package/cross-env) that can be used to make it work cross platform. – Scribblemacher Jul 27 '20 at 11:54