Heroku:错误 - /bin/sh:1:npm:部署到 Heroku 时未找到

Heroku: Error - /bin/sh: 1: npm: not found when deploying to Heroku

我有一个 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.

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

我终于弄明白了。

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

这是我修复它的方法:

我使用 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

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

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

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

现在,使用 package.json 的引擎部分指定 Node.js 的版本,npm versionyarn 版本 在 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 

就这些了。

希望对您有所帮助