简体   繁体   中英

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. I want to host it in GCP cloud with gcloud app deploy command. So, first of all, I build my TS sources to JavaScript -is that the correct way of doing it?.

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:

在此处输入图片说明 .

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.

But afterward, I receive a 500 error on each request whenever I'm trying to reach the deployed app. 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:

    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:

{
  "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?

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. To host Node.Js wrote on TS first we need to build it to simple JS using tsc command. Then in the build folder rewrite package.json file to use correct commands. Look at my start command: "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. I've changed start command to "start": "node server.js" and then all works well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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