Giter Site home page Giter Site logo

Serverless deploy about docs.nestjs.com HOT 43 CLOSED

nestjs avatar nestjs commented on May 4, 2024 90
Serverless deploy

from docs.nestjs.com.

Comments (43)

rynop avatar rynop commented on May 4, 2024 13

Cross post from #238, but don't want it to get buried in a closed issue...

For those of you looking to deploy to APIGateway (proxy+) + Lambda, I just figured it out (Thanks @brettstack for all your work). Took me some time to piece all the parts together, hope this helps someone else out.

lambda.ts:

require('source-map-support').install();

import { Context, Handler } from 'aws-lambda';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { Server } from 'http';
import { createServer, proxy } from 'aws-serverless-express';
import { eventContext } from 'aws-serverless-express/middleware';
import express from 'express';

let cachedServer: Server;
const expressApp = require('express')();

// NOTE: If you get ERR_CONTENT_DECODING_FAILED in your browser, this is likely
// due to a compressed response (e.g. gzip) which has not been handled correctly
// by aws-serverless-express and/or API Gateway. Add the necessary MIME types to
// binaryMimeTypes below
const binaryMimeTypes: string[] = [];

async function bootstrapServer(): Promise<Server> {
  const nestApp = await NestFactory.create(AppModule, expressApp);
  nestApp.use(eventContext());
  await nestApp.init();
  return createServer(expressApp, undefined, binaryMimeTypes);
}

export const handler: Handler = async (event: any, context: Context) => {
  if (!cachedServer) {
    console.log('Bootstraping server');
    cachedServer = await bootstrapServer();
  } else {
    console.log('Using cached server');
  }
  return proxy(cachedServer, event, context, 'PROMISE').promise;
};

For extra credit, you can add the following to your package.json's scripts to reduce the size of your zip file prior to deploy. If someone knows of an npm module to only include the code in node_modules that typescript needs I'd appreciate it:

"package": "rm /tmp/lambda.zip; zip -r /tmp/lambda.zip dist node_modules -x 'node_modules/typescript/*' 'node_modules/@types/*'"

from docs.nestjs.com.

rdlabo avatar rdlabo commented on May 4, 2024 12

Updated rdlabo/serverless-nestjs to nestjs6, and add environment for swagger. I hope this will help.

rdlabo/serverless-nestjs
https://github.com/rdlabo/serverless-nestjs

from docs.nestjs.com.

rdlabo avatar rdlabo commented on May 4, 2024 7

I created repository how to use nestjs in serverless framework. I hope this will help. nestjs/nest#238

rdlabo/serverless-nestjs
https://github.com/rdlabo/serverless-nestjs

from docs.nestjs.com.

rynop avatar rynop commented on May 4, 2024 7

@svstartuplab @dylandechant I built an open source project called https://github.com/rynop/aws-blueprint that is an easy to use CI/CD driven blueprint you can use to drive production grade applications.

In the examples section, check out the abp-single-lambda-api. It will do everything you need to run a NodeJs based app. While I don't have an explicit NestJs example, my comment above from 8/22 should be a copy/paste replacement for the typescript example handler (just replace contents of this file with my post above).

aws-blueprint does too many things to cover in this post, however I think once you give it a try and get over the small learning curve, you will see it really gives you alot (ex: @neilime everything you mention aws-blueprint gives you for "free" - plus MUCH more).

Last - if you need to talk to a RDS DB or need to communicate to resources in a VPC (ex Mongo cluster), I'd save yourself some time and NOT use Lambda with NestJs (or any API for that matter). The ENI attachments cause killer cold starts. Instead I'd use fargate - aws-blueprint has a nice blueprint for it (see abp-fargate in the Examples section).

from docs.nestjs.com.

joffarex avatar joffarex commented on May 4, 2024 5

Quick question: is it a good idea to run full api on lambda or any other similar service, which connects to db on basically every request?

from docs.nestjs.com.

vfrank66 avatar vfrank66 commented on May 4, 2024 4

I found this package for AWS Lambda/API Gateway, using methods above with nestjs fastify:

https://github.com/benMain/aws-serverless-fastify

from docs.nestjs.com.

kop7 avatar kop7 commented on May 4, 2024 4

If someone is going to need it, there's an example:serverless-nestjs-typeorm

from docs.nestjs.com.

Can-Sahin avatar Can-Sahin commented on May 4, 2024 3

I used Nestjs in a free time project I made. Its fully serverless with DynamoDB, DynamoDB Streams, S3 triggers etc... You can see my way of using Nestjs and deploying to AWS if you are looking for an example. Also includes Lambda with webpack

https://github.com/International-Slackline-Association/Rankings-Backend

related with nestjs/nest#238

from docs.nestjs.com.

jmpreston avatar jmpreston commented on May 4, 2024 2

Lambda layers is the way to go now. I haven't spent time trying to figure out how this works yet. We need a good blog post on this. I'm not advanced enough for that.

from docs.nestjs.com.

cctoni avatar cctoni commented on May 4, 2024 2

Please have a look into deploying nestjs to now.sh, would be great!

from docs.nestjs.com.

rynop avatar rynop commented on May 4, 2024 2

@ripley check out https://github.com/rynop/abp-sam-nestjs mentioned in my #96 (comment) above. An example is in that repo @ https://github.com/rynop/abp-sam-nestjs/blob/master/src/apig-lambda.ts

from docs.nestjs.com.

lnmunhoz avatar lnmunhoz commented on May 4, 2024 1

For a few days I was trying to use @nestjs/graphql with the serverless approach, after a few tries I came up with a solution that worked well.

If you are looking for nestjs + graphql + code-first on aws lambda, this example might be useful for you. I am using in production 👍

Repository: https://github.com/lnmunhoz/nestjs-graphql-serverless

from docs.nestjs.com.

ripley avatar ripley commented on May 4, 2024 1

@rynop
Would you elaborate a bit on how to handle the aws event proxied to the cached nest server?

I'm trying to implement an authorizer lambda with nestjs, the auth token is passed in as "authorizationToken" field in the event object from AWS API gateway.

Cross post from #238, but don't want it to get buried in a closed issue...

For those of you looking to deploy to APIGateway (proxy+) + Lambda, I just figured it out (Thanks @brettstack for all your work). Took me some time to piece all the parts together, hope this helps someone else out.

lambda.ts:

require('source-map-support').install();

import { Context, Handler } from 'aws-lambda';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { Server } from 'http';
import { createServer, proxy } from 'aws-serverless-express';
import { eventContext } from 'aws-serverless-express/middleware';
import express from 'express';

let cachedServer: Server;
const expressApp = require('express')();

// NOTE: If you get ERR_CONTENT_DECODING_FAILED in your browser, this is likely
// due to a compressed response (e.g. gzip) which has not been handled correctly
// by aws-serverless-express and/or API Gateway. Add the necessary MIME types to
// binaryMimeTypes below
const binaryMimeTypes: string[] = [];

async function bootstrapServer(): Promise<Server> {
  const nestApp = await NestFactory.create(AppModule, expressApp);
  nestApp.use(eventContext());
  await nestApp.init();
  return createServer(expressApp, undefined, binaryMimeTypes);
}

export const handler: Handler = async (event: any, context: Context) => {
  if (!cachedServer) {
    console.log('Bootstraping server');
    cachedServer = await bootstrapServer();
  } else {
    console.log('Using cached server');
  }
  return proxy(cachedServer, event, context, 'PROMISE').promise;
};

For extra credit, you can add the following to your package.json's scripts to reduce the size of your zip file prior to deploy. If someone knows of an npm module to only include the code in node_modules that typescript needs I'd appreciate it:

"package": "rm /tmp/lambda.zip; zip -r /tmp/lambda.zip dist node_modules -x 'node_modules/typescript/*' 'node_modules/@types/*'"

from docs.nestjs.com.

vadistic avatar vadistic commented on May 4, 2024 1

I've come up with something like this for Zeit Now @now/node builder.

Works with now GitHub CI / with cloud compilation - if I give up using tsconfig-paths

import { NestFactory } from '@nestjs/core';
import { ExpressAdapter } from '@nestjs/platform-express';
import express from 'express';
import { from } from 'rxjs';

import { AppModule } from './app.module';

async function bootstrap() {
  const instance = express();
  const adapter = new ExpressAdapter(instance);
  const app = await NestFactory.create(AppModule, adapter);

  await app.init();

  return instance;
}


// Observable for cold start
const server = from(bootstrap());

export default async function serverless(req, res) {
  const handler = await server.toPromise();

  return handler(req, res);
}

from docs.nestjs.com.

TreeMan360 avatar TreeMan360 commented on May 4, 2024 1

@hbthegreat Actually this was a bug in Serverless Framework Enterprise with the bug residing in the SDK that is automatically included for enterprise/pro customers to do instrumentation.

https://github.com/serverless/serverless/issues/7543
https://forum.serverless.com/t/highly-confusing-express-router-issue/10987

Everything is now working absolutely fine on nestjs 7 :)

from docs.nestjs.com.

samdelagarza avatar samdelagarza commented on May 4, 2024

+1

from docs.nestjs.com.

samdelagarza avatar samdelagarza commented on May 4, 2024

google cloud, or aws lambda, azure functions...

from docs.nestjs.com.

simonpeters avatar simonpeters commented on May 4, 2024

+1

from docs.nestjs.com.

dpapukchiev avatar dpapukchiev commented on May 4, 2024

+1

from docs.nestjs.com.

rdlabo avatar rdlabo commented on May 4, 2024

+1

from docs.nestjs.com.

jmpreston avatar jmpreston commented on May 4, 2024

@rynop, I'm exploring using AWS Lambda with Nestjs. Where does your code go? In each Lambda function? Or does code in Lambda call it from an EC2 server with each Lambda call? Or some other setup?

from docs.nestjs.com.

neilime avatar neilime commented on May 4, 2024

I have build an API powered by NestJs, and serverless (https://github.com/serverless/serverless) hosted on lambda.

It runs in production.

What I have done :

A single one handler (endpoint) /api/v1/{proxy+} redirect to the nestjs app.

For performance reasons, the handler is caching :

  • express App
  • Nestjs app
  • Mongodb connection

To avoid preflight CORS requests, I choose to only accept only GET, HEAD, POST requests whith text/plain Content-Type.


I think I can improve performance by using fastify instead of express, but I don't manage it for the moment

from docs.nestjs.com.

dylandechant avatar dylandechant commented on May 4, 2024

also interested in this

from docs.nestjs.com.

jmpreston avatar jmpreston commented on May 4, 2024

@rynop I'm a single dev and don't want to get into CI/CD or containers. Either just Ubuntu as setup on my dev machine and just deploy to AWS EC2 or API Gateway and Lambda. I already have Postgres setup on RDS. My current app won't have much traffic, at least for a while.

So Lambda Layers is interesting to me. I'm trying to figure out how to setup Layers with Nestjs. So far it is difficult to find a good blog post or docs on this. I have Nestjs / REST working well in dev but don't have the Lambda concept with it worked out yet. Also exploring GraphQL.

from docs.nestjs.com.

rynop avatar rynop commented on May 4, 2024

I don't know much about Layers yet, but my initial read was it is overly complex. Hence I haven't set aside time to investigate.

from docs.nestjs.com.

kamilmysliwiec avatar kamilmysliwiec commented on May 4, 2024

We have recently published an article about Nest + Zeit Now deployment cc @MarkPieszak
https://trilon.io/blog/deploying-nestjs-to-zeit-now

from docs.nestjs.com.

samdelagarza avatar samdelagarza commented on May 4, 2024

from docs.nestjs.com.

murraybauer avatar murraybauer commented on May 4, 2024

I saw on Twitter a few days ago an annoucement for an offical Nest Schematic for Azure Functions:

Can add via nest add @nestjs/azure-func-http schematic

Uses azure-express behind the schemes:
https://github.com/nestjs/azure-func-http/blob/885fb33c109ed2a0b59c97fe0912bf412bf2228e/lib/azure-http.adapter.ts

from docs.nestjs.com.

rynop avatar rynop commented on May 4, 2024

I have a new blueprint that contains a working example of NestJS (using Fastify engine) with API Gateway to Lamba. CI/CD driven deploys. Local APIG+Lamba and DyanmoDB local support. https://github.com/rynop/abp-sam-nestjs

@kamilmysliwiec I think NestJS app running behind API Gateway and in Lambda is a pretty compelling/powerful use case. I'm happy to do the work to document how to do this but:

Is this something you want documented in the official docs? If yes: where and I'll work on a PR.

If no:

Not just trying to get notoriety for my repo. The concept is not super straight forward, and IMO should be an entity that evolves on its own - updating best practices as AWS evolves...

"not a good fit for nestjs docs" is a fine answer too - wont hurt my feelings.

from docs.nestjs.com.

kop7 avatar kop7 commented on May 4, 2024

any example how to deploy serverless nestjs with typeorm?

It should be simple???

I create CompanyModule in nest-serverless project with typeorm crud:

companyModule

@Module({
  imports: [TypeOrmModule.forFeature([Company])],
  controllers: [CompanyController],
  providers: [CompanyService],
})
export class CompanyModule {}

CompanyService

@Injectable()
export class CompanyService extends TypeOrmCrudService<Company> {
    constructor(
        @InjectRepository(Company)
        private readonly CompanyRepository: Repository<Company>) {
        super(CompanyRepository);
    }
}

CompanyEntity

@Entity('company')
export class Company {
    @PrimaryGeneratedColumn()
    id: number;
    @Column()
    name: string;
}

CompanyController

@Crud({model: {
        type: Company,
    }})
@Controller('api/company')
export class CompanyController implements CrudController<Company> {
    constructor(public readonly service: CompanyService) {}
}

AppModule:

@Module({
  imports: [
    TypeOrmModule.forRoot({
          type: 'mysql',
          host: 'x.x.x.x,
          port: 3306,
          username: 'xxxxx',
          password: 'xxxxx',
          database: 'xxxx',
          charset: 'utf8',
          logging: false,
          logger: 'advanced-console',
          entities: [__dirname + './../**/*.entity!(*.d).{ts,js}'],
          keepConnectionAlive: true,
          synchronize: true,
    }),
    CompanyModule,
  ],
  controllers: [ AppController],
  providers: [ AppService,],
})
export class AppModule { }

commad: rimraf dist && npm run build && sls deploy -s dev

Serverless: Optimize: starting engines
Serverless: Optimize: nestjs-restapi-dev-company
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service nestjs-restapi.zip file to S3 (6.71 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
.........................................
Serverless: Stack update finished...
Service Information
service: nestjs-restapi
stage: dev
region: eu-central-1
stack: nestjs-restapi-dev
resources: 35
api keys:
  None
endpoints:
  ANY - https://xxxxxxxxxxxxx.execute-api.eu-central-1.amazonaws.com/dev/api/company

functions:
  company: nestjs-restapi-dev-company
layers:
  None
Serverless: Removing old service artifacts from S3...
Serverless: Run the "serverless" command to setup monitoring, troubleshooting and testing.

Getting errors from Aws:


�[32m[Nest] 1 - �[39m2019-08-27 08:09 �[38;5;3m[NestFactory] �[39m�[32mStarting Nest application...�[39m
�[32m[Nest] 1 - �[39m2019-08-27 08:09 �[38;5;3m[InstanceLoader] �[39m�[32mTypeOrmModule dependencies initialized�[39m�[38;5;3m +91ms�[39m
�[32m[Nest] 1 - �[39m2019-08-27 08:09 �[38;5;3m[InstanceLoader] �[39m�[32mPassportModule dependencies initialized�[39m�[38;5;3m +1ms�[39m
�[32m[Nest] 1 - �[39m2019-08-27 08:09 �[38;5;3m[InstanceLoader] �[39m�[32mJwtModule dependencies initialized�[39m�[38;5;3m +1ms�[39m
�[32m[Nest] 1 - �[39m2019-08-27 08:09 �[38;5;3m[InstanceLoader] �[39m�[32mAdsModule dependencies initialized�[39m�[38;5;3m +0ms�[39m
�[32m[Nest] 1 - �[39m2019-08-27 08:09 �[38;5;3m[InstanceLoader] �[39m�[32mAppModule dependencies initialized�[39m�[38;5;3m +1ms�[39m
�[32m[Nest] 1 - �[39m2019-08-27 08:09 �[38;5;3m[InstanceLoader] �[39m�[32mTypeOrmCoreModule dependencies initialized�[39m�[38;5;3m +77ms�[39m
�[31m[Nest] 1 - �[39m2019-08-27 08:09 �[38;5;3m[ExceptionHandler] �[39m�[31mNo repository for "Company" was found. Looks like this entity is not registered in current "default" connection?�[39m�[38;5;3m +1ms�[39m
RepositoryNotFoundError: No repository for "Company" was found. Looks like this entity is not registered in current "default" connection?
at new RepositoryNotFoundError (/var/task/_optimize/nestjs-restapi-dev-company/dist/index.js:179054:28)
at EntityManager.getRepository (/var/task/_optimize/nestjs-restapi-dev-company/dist/index.js:177032:19)
at Connection.getRepository (/var/task/_optimize/nestjs-restapi-dev-company/dist/index.js:153557:29)
at getRepository (/var/task/_optimize/nestjs-restapi-dev-company/dist/index.js:10232:26)
at InstanceWrapper.useFactory [as metatype] (/var/task/_optimize/nestjs-restapi-dev-company/dist/index.js:10241:20)
at Injector.instantiateClass (/var/task/_optimize/nestjs-restapi-dev-company/dist/index.js:4017:55)
at callback (/var/task/_optimize/nestjs-restapi-dev-company/dist/index.js:3812:41)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)

from docs.nestjs.com.

kamilmysliwiec avatar kamilmysliwiec commented on May 4, 2024

We started working on articles:

More articles coming soon

from docs.nestjs.com.

vadistic avatar vadistic commented on May 4, 2024

Also did someone managed to get @nestjs/graphql on azure functions?

Zeit Now is blocked by this: vercel/vercel#3115, so I wanted to try azure. Rest functions/ rests parts of nest apps are working fine, but I'm getting silent 204/timeous on graphql requests (both in func start & deployed). Whole app inits and works - just nothing is returned from GraphQLModule. No exceptions either.

I've tried all permutation of:

  • disable cors on whole app

  • disable cors in GraphQLModule

  • useGlobalPrefix in GraphQLModule both ways

  • diable all fs related things (autoSchemaFile: false & typeDefs from string const)

  • disable/prune all subscription related things

  • and here https://www.apollographql.com/docs/apollo-server/deployment/azure-functions/ theyre recommending to use some magic $return token which does god knows what...

// function.json
   {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }

I'm runnig out of ideas. I would be glad to simply hear it's possible 😄

from docs.nestjs.com.

jparneodo avatar jparneodo commented on May 4, 2024

@vadistic Do you solved your problem ?
I am stuck also on same point.

To be sure of kinematic, I started with:
set DEBUG=* & npm run start:azure

In file node_modules/@nestjs/azure-func-http/dist/azure-http.adapter.js line 16
return handler(context, req);

Returns undefined

Not sure but it doesn't seems to be a good value.

from docs.nestjs.com.

marekm1844 avatar marekm1844 commented on May 4, 2024

'return proxy(cachedServer, event, context, 'PROMISE').promise;`

Today I had constant Error 502 from Api Gateway and could not figure out what is going on since Lambda logs were ok.

At the end the returning promise in proxy server saved the day and 502 Internal server error is gone !

from docs.nestjs.com.

karocksjoelee avatar karocksjoelee commented on May 4, 2024

I am wondering if someone thinks mapping NestJS controller to Serverless Lambda handler is a good idea ?

Basically I want local develop server can have Swagger document and can deploy each API end point as a Lambda function. Is this possible ?

from docs.nestjs.com.

marekm1844 avatar marekm1844 commented on May 4, 2024

I am wondering if someone thinks mapping NestJS controller to Serverless Lambda handler is a good idea ?

Basically I want local develop server can have Swagger document and can deploy each API end point as a Lambda function. Is this possible ?

You can split into multiple functions with serverless and using direct path to controller instead of {proxy} but you would still need whole nestjs instance for each lambda to use shared stuff like guards, models etc.

from docs.nestjs.com.

vnenkpet avatar vnenkpet commented on May 4, 2024

Is there any advice on how to run it with Google Cloud function? I think it should be pretty straightforward, but I can't figure out how should the exported function for google cloud look.

This is a default google cloud function:

export const helloWorld = (request, response) => {
  response.status(200).send("Hello World 2!");
};

I suppose I need to extract something like HttpServer or HttpAdapter from nest app instance and pass the request/response parameters to it, but I'm not sure on how to exactly do it.

The only issue about this that I could find nestjs/nest#238 basically only answered for AWS for reasons I don't understand 😕

from docs.nestjs.com.

vnenkpet avatar vnenkpet commented on May 4, 2024

Ok, I figured it out, for anyone who wants to run Nestjs with Google Cloud Functions (without Firebase), it's actually quire straightforward:

import { NestFactory } from "@nestjs/core";
import { ExpressAdapter } from "@nestjs/platform-express";
import { AppModule } from "./app.module";
import express from "express";

const server = express();

export const createNestServer = async expressInstance => {
  const app = await NestFactory.create(
    AppModule,
    new ExpressAdapter(expressInstance)
  );

  return app.init();
};

createNestServer(server)
  .then(v => console.log("Nest Ready"))
  .catch(err => console.error("Nest broken", err));

export const handler = server;

You can also add this to package.json to make the process a bit more straightforward:

  "main": "dist/handler.js",
  "scripts": {
    "gcp:start": "functions-framework --target=handler --port=8001",
    "gcp:watch": "concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"yellow.bold,cyan.bold,green.bold\" \"npm run gcp:watch-ts\" \"npm run gcp:watch-node\"",
    "gcp:watch-node": "nodemon --watch dist --exec npm run gcp:start",
    "gcp:watch-ts": "nest build --watch",
    "gcp:deploy": "gcloud functions deploy handler --runtime nodejs10 --trigger-http"
 }

Of course you have to have concurrently, functions-framework etc. installed for this to run.
Then just

npm run gcp:watch

to run it locally, and

npm run gcp:deploy

to deploy.

from docs.nestjs.com.

kukuandroid avatar kukuandroid commented on May 4, 2024

I have build an API powered by NestJs, and serverless (https://github.com/serverless/serverless) hosted on lambda.

It runs in production.

What I have done :

A single one handler (endpoint) /api/v1/{proxy+} redirect to the nestjs app.

For performance reasons, the handler is caching :

  • express App
  • Nestjs app
  • Mongodb connection

To avoid preflight CORS requests, I choose to only accept only GET, HEAD, POST requests whith text/plain Content-Type.

I think I can improve performance by using fastify instead of express, but I don't manage it for the moment

Hello, does the performance for monolith architecture is ok ? I'm thinking of implementing single-point access , do have you any examples ? Thank you

from docs.nestjs.com.

TreeMan360 avatar TreeMan360 commented on May 4, 2024

Hey all,

Since migrating to Nestjs 7 I am really struggling to get aws-serverless-express working. It was working absolutely fine on version 6 and it runs fine locally without the lambda.

I have tried an insane amount of different package version combinations but alas I still receive the following error:

ERROR TypeError: **express_1.default.Router is not a function at ApolloServer.getMiddleware** (/var/task/node_modules/apollo-server-express/dist/ApolloServer.js:81:42) at ApolloServer.applyMiddleware (/var/task/node_modules/apollo-server-express/dist/ApolloServer.js:76:22) at GraphQLModule.registerExpress (/var/task/node_modules/@nestjs/graphql/dist/graphql.module.js:119:22) at GraphQLModule.registerGqlServer (/var/task/node_modules/@nestjs/graphql/dist/graphql.module.js:103:18) at GraphQLModule.<anonymous> (/var/task/node_modules/@nestjs/graphql/dist/graphql.module.js:93:18) at Generator.next (<anonymous>) at fulfilled (/var/task/node_modules/tslib/tslib.js:110:62) at processTicksAndRejections (internal/process/task_queues.js:97:5)

I have wasted over a day on this now, so frustrating. Before going any further has anybody managed to get Nestjs 7 running on Lambda?

Thanks

from docs.nestjs.com.

hbthegreat avatar hbthegreat commented on May 4, 2024

@TreeMan360 We have quite a few projects running on 6.9.0 and am yet to migrate to 7 but the error you are getting there looks it could be build related rather than nest version related. Are you using webpack by any chance?
If you are I ran into this same issue with postgres and a few other platforms and was able to solve it with serverless-webpack forceIncludes.
image

from docs.nestjs.com.

hbthegreat avatar hbthegreat commented on May 4, 2024

@TreeMan360 Great to hear you got a resolution!

from docs.nestjs.com.

danieldanielecki avatar danieldanielecki commented on May 4, 2024

Ok, I figured it out, for anyone who wants to run Nestjs with Google Cloud Functions (without Firebase), it's actually quire straightforward:

import { NestFactory } from "@nestjs/core";
import { ExpressAdapter } from "@nestjs/platform-express";
import { AppModule } from "./app.module";
import express from "express";

const server = express();

export const createNestServer = async expressInstance => {
  const app = await NestFactory.create(
    AppModule,
    new ExpressAdapter(expressInstance)
  );

  return app.init();
};

createNestServer(server)
  .then(v => console.log("Nest Ready"))
  .catch(err => console.error("Nest broken", err));

export const handler = server;

You can also add this to package.json to make the process a bit more straightforward:

  "main": "dist/handler.js",
  "scripts": {
    "gcp:start": "functions-framework --target=handler --port=8001",
    "gcp:watch": "concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"yellow.bold,cyan.bold,green.bold\" \"npm run gcp:watch-ts\" \"npm run gcp:watch-node\"",
    "gcp:watch-node": "nodemon --watch dist --exec npm run gcp:start",
    "gcp:watch-ts": "nest build --watch",
    "gcp:deploy": "gcloud functions deploy handler --runtime nodejs10 --trigger-http"
 }

Of course you have to have concurrently, functions-framework etc. installed for this to run.
Then just

npm run gcp:watch

to run it locally, and

npm run gcp:deploy

to deploy.

My setup was really similar for Firebase deployment, i.e.

import * as admin from 'firebase-admin';
import * as express from 'express';
import * as functions from 'firebase-functions';
import { ApplicationModule } from './app.module';
import { enableProdMode } from '@angular/core';
import { Express } from 'express';
import {
  ExpressAdapter,
  NestExpressApplication
} from '@nestjs/platform-express';
import { NestFactory } from '@nestjs/core';

enableProdMode(); // Faster server renders in production mode (development doesn't need it).
admin.initializeApp(); // Initialize Firebase SDK.
const expressApp: Express = express(); // Create Express instance.

(async () => {
  const nestApp = await NestFactory.create<NestExpressApplication>(
    ApplicationModule,
    new ExpressAdapter(expressApp)
  );
  nestApp.useStaticAssets(join(process.cwd(), 'dist/apps/my-app-browser'));
  nestApp.init();
})().catch(err => console.error(err));

// Firebase Cloud Function for Server Side Rendering (SSR).
exports.angularUniversalFunction = functions.https.onRequest(expressApp);

from docs.nestjs.com.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.