Heroku:部署到 Heroku 时出现错误 - /bin/sh: 1: npm: not found

3

我有一个刚开发完成的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.

我已经多次重新运行了命令,但仍然遇到相同的错误。

1个回答

2

我终于找到了问题所在。

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

以下是我如何解决它的方法

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

heroku buildpacks:add --index 1 heroku/nodejs

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

注意:您的应用程序主要语言的构建包应该是最后添加的构建包。在这种情况下,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的引擎部分来指定在Heroku上使用的Node.jsnpm版本yarn版本

"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 

就这些了。

希望这可以帮到你。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接