简体   繁体   English

当我尝试通过 GCP deploy 命令托管 Node.js 应用程序时,它不起作用。 错误:找不到模块“express”

[英]Node.js app doesn't work when I try to host it via GCP deploy command. Error: Cannot find module 'express'

I have my NodeJS app wrote using TypeScript and based on the Express framework.我的 NodeJS 应用程序使用 TypeScript 编写并基于 Express 框架。 I want to host it in GCP cloud with gcloud app deploy command.我想使用gcloud app deploy命令gcloud app deploy它托管在 GCP 云中。 So, first of all, I build my TS sources to JavaScript -is that the correct way of doing it?.所以,首先,我将我的 TS 源构建为 JavaScript - 这是正确的做法吗?。

Then from the build (with JS source code) folder I'm trying to run npm start command and it works successfully and I'm also able to check it with Preview:然后从build (带有 JS 源代码)文件夹中,我尝试运行npm start命令并且它成功运行,我也可以使用预览检查它:

在此处输入图片说明 . .

It works well.它运作良好。 So far, so good.到现在为止还挺好。

Then I run gcloud app deploy from the build folder (with built to JS sources) and I didn't see any errors during deploy.然后我从build文件夹(内置到 JS 源)运行gcloud app deploy并且在部署过程中我没有看到任何错误。

But afterward, I receive a 500 error on each request whenever I'm trying to reach the deployed app.但之后,每当我尝试访问已部署的应用程序时,每个请求都会收到 500 错误。 I've taken a look into a log and I see next error:我查看了一个日志,我看到了下一个错误:

Error: Cannot find module 'express'

What seems to be the problem?似乎是什么问题?

I tried the next commands in the build folder:我在build文件夹中尝试了下一个命令:

    npm install
    npm install express --save
    npm install -g express
    sudo apt-get install node-express

Nothing works for me.没有什么对我有用。

Here is my package.json file:这是我的package.json文件:

{
  "name": "full-node",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "tsc",
    "dev": "node -r ts-node/register ./src/server.ts",
    "debug": "ts-node --inspect ./src/server.ts",
    "start": "node build/server.js",
    "prod": "npm run build && npm run start"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "ts-node": "^7.0.1",
    "typescript": "^3.0.1"
  },
  "dependencies": {
    "@types/lodash": "^4.14.116",
    "body-parser": "^1.18.3",
    "connect": "^3.6.6",
    "cors": "^2.8.4",
    "crypto": "^1.0.1",
    "express": "^4.16.3",
    "firebase-admin": "^6.0.0",
    "lodash": "^4.17.10"
  }
}

Any idea what I'm missed?知道我错过了什么吗? Is this the correct way to deploy an app wrote with TypeScript to GCP cloud?这是将使用 TypeScript 编写的应用程序部署到 GCP 云的正确方法吗?

app.yaml : app.yaml :

# [START app_yaml]
runtime: nodejs8
# [END app_yaml]

由于您正在构建文件夹中运行 gcloud app deploy,可能 package.json 未部署,因为 npm install 首先由应用引擎运行,因此不可能缺少 express。您可以转到 gcp 控制台并在应用引擎视图下版本,然后在诊断下,您可以查看源文件(实际部署到应用程序引擎的文件)。请记住,这仅适用于标准版本,不适用于 flex。我可以从您的 app.yaml 中看到您是使用标准。如果缺少某些文件,则转到您的应用程序根目录,在您的 .gcloudignore 文件中,您可以忽略您不想部署的文件/文件夹。然后从项目的根目录中运行 gcloud app deploy

The problem was pretty simple.问题很简单。 Seems like gcloud app deploy use npm run build & npm run start commands to start application somewhere inside.似乎gcloud app deploy使用npm run build & npm run start命令在内部某处启动应用程序。 To host Node.Js wrote on TS first we need to build it to simple JS using tsc command.要托管在 TS 上编写的 Node.Js,我们需要使用tsc命令将其构建为简单的 JS。 Then in the build folder rewrite package.json file to use correct commands.然后在build文件夹中重写package.json文件以使用正确的命令。 Look at my start command: "start": "node build/server.js" .看看我的启动命令: "start": "node build/server.js" I was using it inside build folder as well so that's mean gcloud command was searching in /build/build/ folder.我也在build文件夹中使用它,这意味着gcloud命令正在/build/build/文件夹中搜索。 I've changed start command to "start": "node server.js" and then all works well.我已将start命令更改为"start": "node server.js" ,然后一切正常。

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

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