daveteu / nextjs-google-cloud-functions

Deploying NextJS on Google Cloud - Full Serverless
2 stars 1 forks source link

Deployments to GCP not working #1

Open alexabbott opened 3 years ago

alexabbott commented 3 years ago

Upon running npm run deploy the following error is presented:

message=Build failed: function.js does not exist; Error ID: 7485c5b6

After changing server.js to function.js the following error is presented:

 message=Function failed on loading user code. This is likely due to a bug in the user code.
marceloogeda commented 3 years ago

I faced the same issue.

I've configured the following Github Actions to deploy to gcf.

name: Deploy on GCF (Production)

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1

      - name: Read .nvmrc file
        run: echo ::set-output name=NVMRC::$(cat .nvmrc)
        id: nvm

      - name: Use Node.js (.nvmrc)
        uses: actions/setup-node@v1
        with:
          node-version: "${{ steps.nvm.outputs.NVMRC }}"

      - name: Install dependencies
        run: |
          npm i

      - name: Build
        run: |
          npm ci --legacy-peer-deps
          npm run build

      - name: Setup Google Cloud
        uses: actions-hub/gcloud@master
        env:
          PROJECT_ID: ${{ secrets.GOOGLE_PROJECT_ID_PROD }}
          APPLICATION_CREDENTIALS: ${{ secrets.GCLOUD_SERVICE_KEY_PROD }}

      - name: Delpoy to GCP
        uses: actions-hub/gcloud@master
        with:
          args: |
            functions deploy \
            --project ${{ secrets.GOOGLE_PROJECT_ID_PROD }} \
            functionName \
            --entry-point handler \
            --trigger-http \
            --runtime nodejs12 \
            --memory=128MB \
            --allow-unauthenticated

I also needed to create my own .gcloudignore file.

I hope it helps.

antun commented 1 year ago

Update: I got this working, and forked this repo and made some updates. (The file rename above, and changes to the README and update to Node 16.)

Here's my fork: https://github.com/antun/nextjs-google-cloud-functions

Here's the PR: https://github.com/daveteu/nextjs-google-cloud-functions/pull/2


@marceloogeda I'm looking at your GitHub Action, and I don't see anything significantly different from the deployment command I'm using:

gcloud functions deploy nextjs --entry-point=handler --allow-unauthenticated --trigger-http --runtime=nodejs16 --memory=128MB --project MY_PROJECT_ID

I've also renamed server.js to function.js like @alexabbott did. The error I get is:

ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function failed on loading user code. This is likely due to a bug in the user code. Error message: Provided module can't be loaded. Is there a syntax error in your code? Detailed stack trace: ReferenceError: require is not defined in ES module scope, you can use import instead This file is being treated as an ES module because it has a '.js' file extension and '/workspace/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension. at file:///workspace/function.js:1:14 at ModuleJob.run (node:internal/modules/esm/module_job:193:25) at async Promise.all (index 0) at async ESMLoader.import (node:internal/modules/esm/loader:530:24) at async importModuleDynamicallyWrapper (node:internal/vm/module:438:15) at async getUserFunction (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/loader.js:93:30) at async main (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/main.js:35:32) Could not load the function, shutting down.. Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation.