Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NPM scripts - config variables and command substitution not working in package.json

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

like image 622
Luka Avatar asked Dec 01 '15 18:12

Luka


1 Answers

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.

like image 64
Robbie Avatar answered Oct 08 '22 05:10

Robbie