javascript - 尝试在 Heroku 上托管 VueJS 应用程序。在heroku日志中获取 "npm ERR! missing script: start",在我的页面上获取 "Application error"

标签 javascript node.js vue.js heroku npm

我刚刚将我的网站放在 Heroku 上。预期结果:我的 VueJS 应用程序“Twitter Clone”的实时版本。数据是通过将我的 GitHub 同步到 Heroku 获得的。

现在我遇到了一个问题。

https://rolys-twitter-clone.herokuapp.com/这是我的网站,没什么可看的,因为有一条错误消息

内容如下:


An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. You can do this from the Heroku CLI with the command heroku logs --tail

所以我进入了我的应用程序的heroku日志(通过在cmd中输入“heroku log --tail --app my-app-name”,对于任何想知道如何检查它们的人)......我发现了错误消息.

我得到的具体错误隐藏在这个日志数据中:

2020-01-09T04:17:41.201106+00:00 heroku[web.1]: Starting process with command `npm start`
2020-01-09T04:17:44.040787+00:00 heroku[web.1]: State changed from starting to crashed
2020-01-09T04:17:43.902074+00:00 app[web.1]: npm ERR! missing script: start
2020-01-09T04:17:43.921370+00:00 app[web.1]:
2020-01-09T04:17:43.925728+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-01-09T04:17:43.926046+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-01-09T04_17_43_903Z-debug.log
2020-01-09T04:17:44.002368+00:00 heroku[web.1]: Process exited with status 1

“npm 错误!缺少脚本:启动”

不知道该怎么办。

我在其他各种 StackOverflow 页面上尝试过有关此错误的解决方案: 1)将 "scripts": { "start": "node app.js"} 添加到我的 package.json 的脚本部分,重新​​上传到 GitHub,重新部署。结果:“错误:找不到模块'/app/app.js'”(毫不奇怪!)。

2) 将 "scripts": { "start": "npm runserve"} 添加到我的 package.json 的脚本部分,重新​​启动,重新部署。我认为 npm runserve 适用于本地服务器,为什么不尝试一下呢?结果:“sh:1:vue-cli-service:找不到//npm ERR!code ELIFECYCLE//npm ERR!syscall spawn”

3) 添加"scripts": { "start": "node server.js"}。结果:错误:找不到模块“/app/server.js”

注意,我已经浏览了大约 4 个关于此错误的不同 StackOverflow 链接,但似乎没有任何内容相关(我什至尝试了一些解决方案,如上所示)。据我所知,它们都是针对此错误的其他版本。

如果this page包含一个解决方案,我不知道如何使它适合我的情况。 “index.js 是你的入口点”?在 VueJS 中,index.js 的等价物是什么?我的项目根目录只包含:App.vue、db.js、main.js、index.html(在public文件夹下)和一些配置文件。

发送帮助!预先感谢所有阅读本文的人。

编辑:按照 Muljayan 的建议,我现在得到:

2020-01-09T06:43:41.857621+00:00 heroku[web.1]: Starting process with command `npm start`
2020-01-09T06:43:44.546969+00:00 heroku[web.1]: State changed from starting to crashed
2020-01-09T06:43:44.413480+00:00 app[web.1]:
2020-01-09T06:43:44.413503+00:00 app[web.1]: > <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a2d6d5cbd6d6c7d08fc1cecdccc7e2928c938c92" rel="noreferrer noopener nofollow">[email protected]</a> start /app
2020-01-09T06:43:44.413505+00:00 app[web.1]: > node server.js
2020-01-09T06:43:44.413506+00:00 app[web.1]:
2020-01-09T06:43:44.466287+00:00 app[web.1]: internal/modules/cjs/loader.js:796
2020-01-09T06:43:44.466291+00:00 app[web.1]: throw err;
2020-01-09T06:43:44.466294+00:00 app[web.1]: ^
2020-01-09T06:43:44.466296+00:00 app[web.1]:
2020-01-09T06:43:44.466300+00:00 app[web.1]: Error: Cannot find module '/app/server.js'
2020-01-09T06:43:44.466302+00:00 app[web.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:793:17)
2020-01-09T06:43:44.466304+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:686:27)
2020-01-09T06:43:44.466306+00:00 app[web.1]: at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
2020-01-09T06:43:44.466308+00:00 app[web.1]: at internal/main/run_main_module.js:17:11 {
2020-01-09T06:43:44.466311+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',

最佳答案

简单 4 步:在 Heroku 上自动部署 Vue.js 应用

我正在寻找以最佳方式在 Heroku 上部署我的 Vue.js 应用程序。发现在 Heroku 上部署 Vue.js 应用程序的文章现在已经很旧了。在这里,我写这篇文章是为了通过持续集成交付来部署您的 Vue.js 应用程序,并在 Heroku 上部署您的 Vue.js 应用程序。

先决条件:

npm 基础知识 基本的git知识 基础 Vue.js 知识 本教程涵盖的主题:

创建一个 Vue.js 项目

配置 Vue.js 应用程序以在本地 Express 服务器上提供应用程序 在 GitHub 上创建 git 存储库并推送代码 创建并配置 Heroku 应用程序 使用 GitHub 配置 Vue.js 项目和 Heroku 应用程序,以便 Heroku 可以在每次新推送时为我们的应用程序提供服务

第 1 步:创建 Vue.js 项目

如果你本地没有Vue。首先安装 Vue.js(此处说明)。我们还需要 Vue 的 CLI 来轻松创建项目。我相信,你已经熟悉 npm 包管理器了:

npm install vue
npm install -g @vue/cli
vue create <YOUR-PROJECT-NAME>
cd <YOUR-PROJECT-NAME>
npm install
npm run serve

现在您的项目应该在 localhost:8080 上启动并运行

第 2 步:配置应用程序以在本地 Express 服务器上提供服务

返回终端并使用 ctrl+c 命令停止运行应用程序。

现在安装express并添加一个server.js文件来为Vue.js应用程序提供服务

npm install express serve-static --save

在您的“您的项目名称”目录下创建一个 server.js 文件,它应如下所示:

添加以下脚本以在 server.js 文件中的快速静态服务器上为您的应用程序提供服务。

const express = require('express')
const serveStatic = require('serve-static')
const path = require('path')

const app = express()

//here we are configuring dist to serve app files
app.use('/', serveStatic(path.join(__dirname, '/dist')))

// this * route is to serve project on different page routes except root `/`
app.get(/.*/, function (req, res) {
    res.sendFile(path.join(__dirname, '/dist/index.html'))
})

const port = process.env.PORT || 8080
app.listen(port)
console.log(`app is listening on port: ${port}`)

现在我们可以运行我们的 server.js 文件来测试我们的应用程序在配置的端口上运行。

Note: before running server.js. we must need to build our vue.js app to create dist dir

npm run build

您的 dist 目录应该已构建并准备好提供服务。

运行以下命令在本地测试您的 server.js 文件

node server.js

在浏览器上通过 localhost:8080 进行测试

现在编辑您的 package.json 文件以告诉 heroku 从您的 server.js 文件提供应用程序。

"scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint",
    "start": "node server.js" <--- add this line under scripts block
  },

您现在已经完成了本地项目。这里不再有工作。

第 3 步:在 GitHub 上创建 git 存储库并推送代码

GitHub is a Git repository hosting service, but it adds many of its own features. 
While git is a command line tool, GitHub provides a Web-based graphical interface. 
It also provides access control and several collaboration features, such as a wikis 
and basic task management tools for every project.

在 GitHub 上创建一个 git 存储库并使用您创建的项目进行设置

setup remote GitHub repository in your project terminal using command as follows:

git remote add origin https://github.com/<your-user-name>/<repo-name>.git

将代码推送到 GitHub

Tip: Don’t forget saving all edited files before pushing on GitHub.

git push -u origin master

酷!! 🙂 您现在应该在 GitHub 上拥有源代码。

第 4 步:创建并配置 Heroku 应用

Heroku is a cloud platform as a service. That means you do not have to worry 
about infrastructure; you just focus on your application. ... Full Logging and 
Visibility - easy access to all logging output from every component of your app
and each process.

在 Heroku 上创建一个应用程序(如果您还没有帐户,请注册一个。)

这是添加新应用程序的片段:

heroku new app

创建应用程序后,它会将您重定向到 Heroku 仪表板上的应用程序部署选项卡。

  • 如果您尚未连接,请将您的 Heroku 帐户连接到 GitHub。
  • 在部署方法下选择连接到 GitHub 选项
  • 搜索您的存储库名称,搜索成功后,您应该拥有如下所示的 GitHub 存储库:

heroku repository

连接到它并在 Heroku 上启用自动部署选项。第一次没有部署,因为没有新的推送。它将在选定分支上的每个下一个新推送时自动部署。

现在进行手动部署并从 Heroku 仪表板打开您的应用程序。

谢谢!!! 👏你终于完成了。

如果部署成功,请测试项目的 URL https://.herokuapp.com 即可完成!

I hope this tutorial was helpful to anyone looking to auto deploy your Vue.js app from GitHub to heroku on every new push.

来源:https://www.codementor.io/@ravianand1988/easily-4-steps-to-continuous-delivery-auto-deploy-vue-js-app-on-heroku-xljk977pq

关于javascript - 尝试在 Heroku 上托管 VueJS 应用程序。在heroku日志中获取 "npm ERR! missing script: start",在我的页面上获取 "Application error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59657252/

相关文章:

javascript - 为什么我不能将 Node 请求方法直接传递给 Bluebird Promise?

node.js - Node js 计数器值不会增加

javascript - 在内部点击时隐藏弹出窗口

javascript - 如何在Vue中提交表单,重定向到新路由并传递参数?

单击时更改 JavaScript 按钮样式

javascript - 性能问题: Components library following the dot notation structure

javascript - 如何将javascript对象映射到mvc模型?

javascript - 无法从 Youtube IFrame 更改焦点

javascript - 带方括号的 Node.js 全局变量和函数 - 为什么这有效?

vue.js - 在 vue.js 中添加新类时样式不适用