首页 > 解决方案 > Yarn 无法运行任何脚本

问题描述

当我运行yarn start或任何其他以下脚本时:

"scripts": {
        "start": "webpack-dev-server --config scripts/webpack.dev.js",
        "clean": "rimraf build",
        "build": "yarn run clean && yarn run compile",
        "compile": "webpack --config scripts/webpack.prod.js",
        "compile-for-test": "webpack --config scripts/webpack.test.prod.js",
        "build-for-test": "yarn run clean && yarn run compile-for-test",
        "test": "jest -c scripts/jest.config.js --testPathIgnorePatterns=\"services/contract-tests\"",
        "test-ci": "node scripts/test-shell-commands.js unitTestCI",
        "test-contract": "node scripts/test-shell-commands.js testLocal",
        "test-contract-ci": "node scripts/test-shell-commands.js testCI",
        "coverage": "node scripts/test-shell-commands.js unitTestCoverage",
        "lint": "./node_modules/.bin/eslint --max-warnings=0 \"src/**\"",
        "start-backend": "bash -l ./scripts/start-backend-container.sh",
        "stop-backend": "bash -l ./scripts/stop-backend-container.sh",
        "start-stub": "bash -l ./scripts/start-backend-stub-container.sh",
        "stop-stub": "bash -l ./scripts/stop-backend-stub-container.sh",
        "prettier": "prettier --write **/*{ts,tsx}"
    },

我收到以下错误:

# yarn start
$ webpack-dev-server --config scripts/webpack.dev.js
error Couldn't find the binary webpack-dev-server --config scripts/webpack.dev.js
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

# yarn test
$ jest -c scripts/jest.config.js --testPathIgnorePatterns="services/contract-tests"
error Couldn't find the binary jest -c scripts/jest.config.js --testPathIgnorePatterns="services/contract-tests"
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

这适用于所有脚本(它并不特别适用于 webpack 等)。但是,当我使用它时npm run start,它可以工作。yarn addyarn单独的命令也有效。只是我无法使用yarn.

有没有人遇到过这个?

我的纱线版本是:1.22.10 我已经卸载并安装了几次,但问题仍然存在。操作系统:Windows

标签: npmyarnpkg

解决方案


由于 Yarn 使用 node 的child_process.spawn.

中指定的 shell 脚本.yarnrc将该 shell 作为 shell 选项传递给 spawn,当指定 shell 并process.platform计算结果为 时win32['/d', '/s', '/c'将在参数中添加(参见下面的源代码spawn())。

 if (options.shell) {
    const command = [file].concat(args).join(' ');

    if (process.platform === 'win32') {
      if (typeof options.shell === 'string')
        file = options.shell;
      else
        file = process.env.comspec || 'cmd.exe';
      args = ['/d', '/s', '/c', `"${command}"`];
      options.windowsVerbatimArguments = true;

请检查您的纱线配置yarn config get script-shell,以验证 bash-path 的设置。

有关更多信息,请参见child_process.spawn ..


推荐阅读