繁体   English   中英

尝试在 Heroku 上托管 VueJS 应用程序。 在 Heroku 日志中获取“npm ERR!缺少脚本:启动”,并在我的页面上获取“应用程序错误”

[英]Trying to host a VueJS app on Heroku. Getting "npm ERR! missing script: start" in heroku logs and "Application error" on my page

我只是把我的网站放在 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 logs --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 ERR!缺少脚本:开始”

不知道该怎么办。

我从其他各种 StackOverflow 页面尝试过的关于此错误的解决方案:1) 将"scripts": { "start": "node app.js" }到我的 package.json 的脚本部分,重新​​上传到 GitHub,重新部署。 结果: "Error: Cannot find module '/app/app.js'""Error: Cannot find module '/app/app.js'" !)。

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

3) 添加"scripts": { "start": "node server.js" } 结果: Error: Cannot find module '/app/server.js'

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

如果此页面包含解决方案,我不知道如何使其适用于我的情况。 “index.js 是你的切入点”? VueJS 相当于 index.js 是什么? 我的项目根目录只包含:App.vue、db.js、main.js、index.html(在public文件夹下)和一些配置文件。

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

编辑:按照穆贾扬的建议,我现在得到:

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]: > twitter-clone@0.1.0 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',

安装 express 在根文件夹的 server.js 中添加以下代码

const express = require('express');
const app = express();

// Serve all the files in '/dist' directory
app.use(express.static('dist'));


app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

在您的 package.json 中添加以下脚本

"scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint",
    "start": "node server.js"
  },

简单 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 新应用

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

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

heroku 存储库

连接到它并在 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM