NPM 脚本 - 配置变量和命令替换在 package.json 中不起作用

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

案例 1: 使用变量名代替值

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 在浏览器中打开 http://localhost:3000/$npm_package_config_url,而不是 http://localhost:3000/dev

所以,$npm_package_config_url被用作字符串,而不是变量。

情况 2: 命令替换不起作用

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

子命令列出 node_modules 中的文件夹。

同样,npm run rm:all 什么都不做,因为 $(ls -1 node_modules | tr '/\n' ' ') 被解释为文件夹名称。

环境:windows10 | npm 3.5.1 |节点 4.2.2 | git-bash 2.6.0

有点晚了,但是在 Windows 你需要使用 %npm_package_config_url%

有一个潜在的包可以 "fix" 为你解决这个问题(即给你一个解决方法)(https://www.npmjs.com/package/cross-env),在 npm 问题帖子之一中被引用。