【问题标题】:yarn + webpack search pathyarn + webpack 搜索路径
【发布时间】:2019-01-24 22:09:11
【问题描述】:

我正在查看一个包含以下行的 package.json 文件的项目:

"scripts": {
    "clean": "rm -rf lib && rm -rf umd",
    "prebuild": "yarn clean",
    "build": "../../node_modules/typescript/bin/tsc",
    "build-production": "yarn build && TS_NODE_PROJECT=\"tsconfig.webpack.json\" webpack",
    "watch": "../../node_modules/typescript/bin/tsc -w",
    "test": "",
    "lint": "../../node_modules/.bin/tslint -c ../../tslint.json -p ./tsconfig.json"
  },
  "devDependencies": {
    "webpack": "^4.25.1",
    "webpack-cli": "^3.1.2"
  },

webpack 不在我的路径中,所以运行 webpack

$ webpack
-bash: /usr/bin/webpack: No such file or directory

但不知何故,当我运行“yarn build-production”时……事情似乎按预期工作。你可以在输出中看到“版本:webpack 4.25.1”。

yarn build-production
yarn run v1.13.0
$ yarn build && TS_NODE_PROJECT="tsconfig.webpack.json" webpack
$ yarn clean
$ rm -rf lib && rm -rf umd
$ ../../node_modules/typescript/bin/tsc
Hash: 365eaed22ba7f6d609fc
Version: webpack 4.25.1
Time: 1289ms
Built at: 01/24/2019 5:03:35 PM
                              Asset       Size  Chunks             Chunk Names
                  ../lib/index.d.ts  268 bytes          [emitted]
eos-transit-scatter-provider.min.js    4.3 KiB       0  [emitted]  main
Entrypoint main = eos-transit-scatter-provider.min.js
[0] ./src/index.ts 6.6 KiB {0} [built]
[1] external "ScatterJS" 42 bytes {0} [built]
[2] external "ScatterEOS" 42 bytes {0} [built]
Done in 8.04s.

让我感到困惑的是,我不明白 npm 脚本如何能够找到 webpack 二进制文件并使用它。显然,如果我在 shell 中运行相同的命令,它将失败,因为 webpack 未安装在我的搜索路径中。

$ yarn build && TS_NODE_PROJECT="tsconfig.webpack.json" webpack
yarn run v1.13.0
$ yarn clean
$ rm -rf lib && rm -rf umd
$ ../../node_modules/typescript/bin/tsc
Done in 2.93s.
-bash: /usr/bin/webpack: No such file or directory

npm 脚本如何找到 webpack 二进制文件?

【问题讨论】:

    标签: npm webpack yarnpkg


    【解决方案1】:

    webpack 二进制文件安装在 ./node_modules/.bin/ 相对于该 package.json 的位置。 npm 脚本在其 PATH 上使用该目录运行,请参阅:https://docs.npmjs.com/misc/scripts#path

    【讨论】:

      猜你喜欢
      • 2012-06-07
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-20
      • 2012-03-05
      • 2015-04-07
      • 2017-07-11
      相关资源
      最近更新 更多