简体   繁体   中英

Error when deploying Angular Universal app on Google Cloud App Engine

When I run gcloud app deploy in Cloud Shell in my browser, I get an Error - Cannot find module '/srv/server.js' over and over. I also get a 500 server error when I view my app URL.

I have a basic Angular app with one lazy loaded path/component just for testing. I installed Angular Universal and then I built my app locally using the commands ng build --prod && npm run build:ssr which put the following files in the dist folder:

- browser
    - all the usual Angular files like index.html etc etc
- server
    - main.js
- app.yaml
- package.json
- prerender.js
- server.js

I have created a basic app on Google App Engine and logged into my app by clicking the Cloud Shell button in my App Engine console.

I then clicked the editor button where you can browse files and write commands in your browser window.

Inside my app.yaml file I have:

runtime: nodejs10

In my package.json file I have:

{
    "scripts": {
        "start": "node server.js"
    }
}

So in Cloud Shell I go to the root directory is where all my files are and where my app.yaml file is and then I run gcloud app deploy .

It then uploads my files to the storage bucket and then runs the node server.js script. Nothing happens in the cloud shell console but when I look in the error log gcloud app logs tail -s default and I go to my app URL, it shows a 500 Server Error and logs Error: Cannot find module '/srv/server.js' over and over.

internal/modules/cjs/loader.js:638
Error: Cannot find module '/srv/server.js' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)      at Function.Module._load (internal/modules/cjs/loader.js:562:25) at Function.Module.runMain (internal/modules/cjs/loader.js:831:12) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

Any help would be greatly appreciated.

Thank you!

I expect to see my Angular app running on the App Engine Cloud URL.

Can you attempt to point a main in your package.json?

This seems to be a similar issue and you can see how you can do it here as well. Link .

If that doesn't work please post how you are doing your require so we can have a further look.

Hope this helps.

UPDATE:

Maybe this can help you better understand the issue.

First, Node looks for a package.json file and checks if it contains a main property. It will be used to point a file inside the package directory that will be the entry point. If main property does not exist, then Node tries in order index.js, index.json and index.node.

Index.js is the default one that Node looks for. You do not have an index.js. So you would have to specify in the main what are you using so it knows the entrypoint.

If server.js is the file that starts the server, and it is in the root of the folder, attempt it like this.

If server.js is in another folder try /'folder'/server.js in the main.

  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },

If you are starting the server on the main.js file you mentioned in your question, point it there.

The question I linked contains a comment from someone saying that they had to use both start and main to solve they're issue, so I would attempt both ways.

Let me know.

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