【问题标题】:Heroku: How to run npm build on a python application in Heroku during deployment?Heroku:如何在部署期间在 Heroku 中的 python 应用程序上运行 npm build?
【发布时间】:2021-03-18 18:32:43
【问题描述】:

我有一个使用 Python/Django 后端 API 和 Vue.js 前端编写的 Web 应用程序。

我们将 Vue 生成的静态文件作为静态 Django 文件提供。前端应用程序位于 Django 项目中的一个单独文件夹中。

在每次部署之前,我需要在本地运行 npm build 并使用 dist 文件夹进行部署。

我有一个用于部署的小脚本:

cd ./vuejs-frontend
npm install
npm run build

git add --force dist/*
git commit -m "Add dist folder"

local_branch=`git rev-parse --abbrev-ref HEAD`
git push heroku ${local_branch}:master -f
git reset --hard HEAD~1

cd ..

我想移动要在部署期间执行的npm install 部分。我正在尝试将其添加到 heroku 在每次部署期间执行的 release.sh 中。基本上我想要这个在我的release.sh:

cd ./vuejs-frontend
npm install
npm run build
cd ..

问题是heroku/python buildpack 没有 node 和 npm。我尝试将 heroku/nodejs 与其他构建版本一起添加,但 Heroku 在部署期间抱怨说它无法找到节点应用程序:

remote:  !     The 'heroku/nodejs' buildpack is set on this application, but was
remote:  !     unable to detect a Node.js codebase.

有没有办法在 Heroku 上获得npm

【问题讨论】:

  • 我认为这与您有关:devcenter.heroku.com/articles/…
  • @KiraLT 该文章的建议是列表中的最后一个 buildpack,确定了应用程序类型。我最后添加了python,但仍然出现同样的错误。
  • 最后一个 buildpack 将用于主要语言。所以我认为你应该从 python 中删除所有节点逻辑并将 package.json 移动到根级别。然后将 2 个构建包添加到配置中:python 和节点(节点应该是第一个)。因此它将运行 node buildpack,它将仅构建 node。之后它将运行 python buildpack,它应该构建 python。我从未尝试过,所以我不能说它是否有效。

标签: heroku npm


【解决方案1】:

问题是 heroku/python buildpack 没有节点和 npm。我尝试将 heroku/nodejs 与其他构建版本一起添加, 但是 Heroku 在部署过程中抱怨说它无法 定位节点应用程序:

remote:  !     The 'heroku/nodejs' buildpack is set on this application, but was
remote:  !     unable to detect a Node.js codebase.

你需要一个package.json 在你的 git repo 的根级别 ./。你只有一个./vuejs-frontend

只需从./vuejs-frontend/package.json复制package.json并将"build": "<command>"中的脚本调整为cd ./vuejs-frontend && <command>


旁注: 这可能会变得有用。您可以定义一些 Heroku 特定的构建步骤:https://devcenter.heroku.com/articles/nodejs-support#heroku-specific-build-steps

"scripts": {
  "heroku-prebuild": "echo This runs before Heroku installs dependencies.",
  "heroku-postbuild": "echo This runs after Heroku installs dependencies, but before Heroku prunes and caches dependencies.",
  "heroku-cleanup": "echo This runs after Heroku prunes and caches dependencies."
}

【讨论】:

    猜你喜欢
    • 2014-01-22
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 2018-03-29
    • 2016-09-22
    • 1970-01-01
    • 2022-07-15
    • 1970-01-01
    相关资源
    最近更新 更多