11

Case 1: variable name used instead of value

package.json:

{
  "name": "example",
  "config": {
    "url": "localhost/dev"
  },
  "scripts": {
    "watch": "browser-sync start --files \"./**/*, !.node_modules/, !src\" --proxy $npm_package_config_url"
  }
}

$npm run watch opens http://localhost:3000/$npm_package_config_url in the browser, instead of http://localhost:3000/dev

So, $npm_package_config_url is used as a string, not as a variable.

Case 2: command substitution is not working

{ 
  { ... },
  "scripts": {
    "rm:all": "npm rm $(ls -1 node_modules | tr '/\\n' ' ')"
   }
}

Sub-command lists folders in node_modules.

Again, npm run rm:all does nothing, because $(ls -1 node_modules | tr '/\\n' ' ') is interpreted as a folder name.

ENV: windows 10 | npm 3.5.1 | node 4.2.2 | git-bash 2.6.0

1 Answer 1

13

A bit late, but on Windows you need to use %npm_package_config_url%

There's a potential package that will "fix" this for you (i.e. give you a work-around) (https://www.npmjs.com/package/cross-env) that was referenced in one of the npm issue posts.

Sign up to request clarification or add additional context in comments.

6 Comments

cross-env will help setting environment variables cross platform, if you want to use environment variables in your scripts in a cross platform way, use cross-var
you saved me so much time with this answer :)
does not work on windows 10, cmd and powershell tested
@silvos , this was Windows 10 that it worked on. Perhaps something else has changed since - can you add a more up to date answer if you work out the new solution?
@Robbie Found out, that there was a bug in NPM, that got fixed at version 7.7.0 github.com/npm/cli/issues/2731 Now it's working - thx
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.