【问题标题】:Heroku: Error - /bin/sh: 1: npm: not found when deploying to HerokuHeroku:错误 - /bin/sh:1:npm:部署到 Heroku 时未找到
【发布时间】:2020-12-18 02:42:40
【问题描述】:

我有一个刚刚构建的 Rails 6 应用程序。

但是,当我尝试使用以下命令从命令行部署到 Heroku 时:

git push heroku master 

经过一段时间后我得到以下错误:

yarn install v1.22.4

   [1/4] Resolving packages...

   [2/4] Fetching packages...

   info fsevents@1.1.1: The platform "linux" is incompatible with this module.

   info "fsevents@1.1.1" is an optional dependency and failed compatibility check. Excluding it from installation.

   [3/4] Linking dependencies...
   [4/4] Building fresh packages...
   success Saved lockfile.
   $ npm run build
   /bin/sh: 1: npm: not found
   error Command failed with exit code 127.
   info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

我已经重新运行了几次命令,但我仍然得到同样的错误。

【问题讨论】:

    标签: ruby-on-rails heroku npm yarnpkg


    【解决方案1】:

    我终于弄清楚了问题。

    问题是因为我没有 Nodejs 构建包来运行 npm run build 命令进行资产编译。

    这是我修复它的方法:

    我使用 buildpacks:add 命令将 Nodejs buildpack 添加到应用程序:

    heroku buildpacks:add --index 1 heroku/nodejs
    

    这将在 buildpack 执行顺序的第一个位置插入 Node.js buildpack,并将其前面的其他 buildpack 向下移动一个位置。

    注意:应用主要语言的 buildpack 应该是最后添加的 buildpack。在这种情况下,Ruby 是我们的主要语言。

    然后您可以查看应用程序的构建包:

    heroku buildpacks 
    
    === nameless-brushlands-4859 Buildpack 
    1. heroku/nodejs 
    2. heroku/ruby
    

    注意:列表中的最后一个 buildpack 将用于确定应用程序的进程类型。

    您应该始终指定与您正在开发和测试的运行时相匹配的 Node.js 版本、npm 版本 和 yarn 版本 .在本地查找您的版本:

    node --version
    npm --version
    yarn --version
    

    现在,使用 package.json 的引擎部分指定 Node.js、npm 版本 和 yarn 版本 的版本以在 Heroku 上使用。

    "version": "0.1.0",
      "engines": {
        "node": "12.x",
        "npm": "6.x",
        "yarn": "1.x"
      },
    

    现在您可以部署到 Heroku,它会正常工作:

    git add .
    git commit -m "add nodejs buildpack"
    git push heroku master 
    

    就是这样。

    我希望这会有所帮助

    【讨论】:

      猜你喜欢
      • 2019-07-14
      • 2019-07-03
      • 1970-01-01
      • 2018-06-27
      • 2021-11-25
      • 2021-10-05
      • 2018-03-20
      • 2017-02-10
      • 2018-05-30
      相关资源
      最近更新 更多