【问题标题】:"npm: command not found" error when deploying Python application to Heroku将 Python 应用程序部署到 Heroku 时出现“npm: command not found”错误
【发布时间】:2018-10-01 21:11:13
【问题描述】:

我刚刚创建了一个 Heroku 帐户,并且正在尝试部署我现有的代码。当我尝试git push heroku master 时,出现以下错误:

 Counting objects: 348, done.
 Delta compression using up to 4 threads.
 Compressing objects: 100% (207/207), done.
 Writing objects: 100% (348/348), 172.64 KiB | 0 bytes/s, done.
 Total 348 (delta 138), reused 279 (delta 116)
 remote: Compressing source files... done.
 remote: Building source:
 remote: 
 remote:  !     Warning: Multiple default buildpacks reported the 
 ability to handle this app. The first buildpack in the list below 
 will be used.
 remote:            Detected buildpacks: Python,Node.js
 remote:            See 
 https://devcenter.heroku.com/articles/buildpacks#buildpack-detect- 
 order
 remote: -----> Python app detected
 remote: -----> Running pre-compile hook
 remote: ----->Pre-compile hook
 remote: -----> Running Webpack
 remote: jquery-webpack-stats.json created
 remote: webpack-stats.json created
 remote: bin/run_webpack: line 15: npm: command not found
 remote:  !     Push rejected, failed to compile Python app.
 remote: 
 remote:  !     Push failed
 remote: Verifying deploy...
 remote: 
 remote: !  Push rejected to paytientdesktop.
 remote: 
 To https://git.heroku.com/paytientdesktop.git
 ! [remote rejected] master -> master (pre-receive hook declined)
 error: failed to push some refs to 
 'https://git.heroku.com/paytientdesktop.git'

这是我的app.json 文件:

   {
   "name": "myapp",
   "description": "Myapp Heroku app.",
  "scripts": {
 "postdeploy": "python manage.py migrate"
},
"env": {
  "ALLOWED_HOSTS": {
  "description": "Django ALLOWED_HOSTS setting, e.g.: 
  .appname.herokuapp.com"
  },
  "DISABLE_COLLECTSTATIC": {
    "description": "Heroku setting to disable Django collectstatic (it 
  is run by bin/post_compile)",
  "value": "1"
},
"DJANGO_SETTINGS_MODULE": {
  "description": "Django settings Python import path",
  "value": "myapp.settings.production"
},
"SECRET_KEY": {
  "description": "Django SECRET_KEY setting",
  "generator": "secret"
  }
 },
 "formation": {
 "web": {
  "quantity": 1,
  "size": "free"
  },
  "worker": {
  "quantity": 1,
  "size": "free"
  }
 },
 "addons": [
  {
    "plan": "heroku-postgresql:hobby-dev",
    "options": {
    "version": "9.5"
    },
  "as": "DATABASE"
  },
  {
    "plan": "heroku-redis:hobby-dev",
    "options": {
    "version": "3.2"
    },
    "as": "REDIS"
  },
  {
  "plan": "sendgrid:starter"
  },
  {
    "plan": "papertrail:choklad"
  },
  {
  "plan": "opbeat:free"
  }
],
 "buildpacks": [
  {
    "url": "heroku/nodejs"
  },
  {
    "url": "heroku/python"
   }
 ]
}

我该如何解决?

【问题讨论】:

  • 您是否能够使用下面显示的答案来解决这个问题?如果是这样,请记得accept it。这会向其他用户显示您已找到解决方案。

标签: python reactjs heroku npm deployment


【解决方案1】:

这是怎么回事?

Heroku 使用 buildpacks 构建应用程序,每个应用程序都特定于特定的编程语言和工具集。

在许多情况下,Heroku 可以通过在您的存储库中查找某些指标文件来检测它应该使用哪个构建包。例如,如果您的存储库的根目录中包含requirements.txt 文件或PipfilePipfile.lock 文件,则将调用the official Python buildpack

上述部署失败的输出包含以下信息:

 remote:  !     Warning: Multiple default buildpacks reported the 
 ability to handle this app. The first buildpack in the list below 
 will be used.
 remote:            Detected buildpacks: Python,Node.js
 remote:            See 
 https://devcenter.heroku.com/articles/buildpacks#buildpack-detect- 
 order

Heroku 默认只运行一个 buildpack,它不知道是应该为您的应用使用 Python buildpack 还是 Node.js buildpack。它选择了 Python。

我该如何解决?

好消息是您可以在单个应用程序中使用multiple buildpacks。你只需要对tell Heroku which buildpacks it should use, and in what order做一些手工工作。

这是一个适合您的示例:

  1. 首先,设置一个构建包。我喜欢在这里使用任何感觉像是主要语言的东西。

    heroku buildpacks:set heroku/python
    
  2. 接下来,以正确的顺序添加您的其他构建包,该顺序由--index 参数控制。看起来您的 Python 构建依赖于 npm,因此您需要 Node.js 构建包才能在 Python 构建包之前运行。

    heroku buildpacks:add --index 1 heroku/nodejs
    
  3. 通过运行heroku buildpacks 确认您的构建包已正确配置。您应该首先看到 Node.js buildpack,然后看到 Python buildpack。

  4. 再次将您的代码推送到 Heroku 并观看它的编译!

为什么我的app.json 不起作用?

您的 app.json 文件列出了 Node.js 和 Python 构建包。为什么它不起作用?

我从未使用过app.json,而且我已经使用 Heroku 大约五六年了。在 Heroku 上运行应用程序当然不需要它。

看起来app.json 是专门用于 Heroku 的平台 API,用于启动全新的应用程序(与将代码部署到已定义的应用程序相反)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-22
    • 2022-01-07
    • 2017-09-13
    • 2015-05-20
    • 2013-02-15
    • 1970-01-01
    • 2021-03-20
    • 2022-09-25
    相关资源
    最近更新 更多