Deployment procedure for NestJS

Hello,
I am a beginner and I would like to upload a NestJS project.
I can’t find any information on google that’s why I’m turning to you. I think that might interest a lot of people.
with you a procedure?

thank you

NestJS seems to be more like a server-side rendering solution and Netlify can not host such projects. Netlify can only serve static HTML, CSS, JS, assets. However, Documentation | NestJS - A progressive Node.js framework shows that, there might be a possibility of static HTML being exported by NestJS. If that’s true, you can simple setup a repo, connect it to Netlify, and it would be done.

I wouldn’t describe it as a server-side rendering solution. Nest.js application can be deployed to netlify lambda. See docs here: Documentation | NestJS - A progressive Node.js framework

Doesn’t that make it server-side rendering? It is using a code on a server to render a page on the fly.

But anyways, I am not a NestJS expert or anything, I had said so only from the initial impression - the ones I got by looking at the home page. This is what it said:

So yeah, I had not dug deep into the docs other than what I had linked. If it’s possible, I stand corrected. Thank you.

1 Like

More for building an API - the A in JAM :slight_smile:

I am not affiliated with nest, just someone else trying to get this working. The example in their docs I linked didn’t actually work but changing to use serverless-http seems to have done the trick for me

Here is my main.js in case that is useful for someone else

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app/app.module';
import serverless = require('serverless-http');

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const globalPrefix = '.netlify/functions/main';
  app.setGlobalPrefix(globalPrefix);
  await app.init();

  const expressApp = app.getHttpAdapter().getInstance();
  return serverless(expressApp)
}

let server;
export const handler = async (event, context, callback) => {
  server = server ?? (await bootstrap());
  return server(event, context, callback);
};

1 Like

A quick look at the documentation link you provided @spacem I see

A cold start is the first time your code has been executed in a while … This process adds significant latency depending on several factors

Remember that functions have a 10 second execution limit so will always run from a cold start. This is not the most effective way to serve a site.

After downloading and building the example static site NestJS offers @duke it doesn’t include an index.html which is the default file for serving on Netlify.

im testing this right now

Hey there, @u007 :wave:

Welcome to the Netlify Forums! Thanks for sharing that. Are you encountering specific obstacles related to Netlify? Let us know how we can help you.

i couldnt get nestjs to work, keep getting 404 error

Hey @u007

Are you able to share the repository you are trying to deploy?

I (sort of) managed to get it working (I think) with Netlify CLI—suggest you might like to try this, if you haven’t, to test before deploying.

npm run build creates a dist directory, inside which is a src directory and and netlify/functions directory chain with a main.js function. This, is appears, it the main function for the site—I believe it is using SSR rather than statically generating pages. The function is not (fully) built at this point. It appears there is a two-step built process.

However I only managed to get it working to a point as I don’t have (nor should I have) the keys/tokens required to fetch data, etc.

What I did was create a netlify.toml file in the project root with the following

[build]
  command = "npm run build"
  publish = "dist/src"
  functions = "dist/netlify/functions"

[[redirects]]
  from = "/*"
  to = "/.netlify/functions/main"
  status = 200

Then I ran netlify dev to try testing it. This is where I need a secret JWT key according to the error.

The TOML above is the build settings for Netlify to use, plus the rewrite rule required to have main.js do all the SSR.

If you can try this locally and see what result you get. If this works, commit this file and see how if the build then works in Netlify. Remember, if you require environment variables in the function, you will need to set this via the UI first.

2 Likes

Thanks for this super thorough write up, @coelmay!

thank you @coelmay , i can see the routes being mapped, but im seing new error:

...
Apr 24, 12:01:09 AM: [Nest] 9  - 04/23/2022, 4:01:09 PM     LOG [RouterExplorer] Mapped {/.netlify/functions/main/crud/v1/roles, GET} route +1ms
Apr 24, 12:01:09 AM: [Nest] 9  - 04/23/2022, 4:01:09 PM     LOG [RouterExplorer] Mapped {/.netlify/functions/main/crud/v1/roles/:id, GET} route +0ms
Apr 24, 12:01:09 AM: [Nest] 9  - 04/23/2022, 4:01:09 PM     LOG [RouterExplorer] Mapped {/.netlify/functions/main/crud/v1/roles/:id, PATCH} route +0ms
Apr 24, 12:01:09 AM: [Nest] 9  - 04/23/2022, 4:01:09 PM     LOG [RouterExplorer] Mapped {/.netlify/functions/main/crud/v1/roles/:id, DELETE} route +1ms
Error - EROFS: read-only file system, mkdir '/var/task/src'

It would appear the function is attempting to write something however the location it is trying to write to is read-only.

Do you know is this is part of your code, of core NestJS functionality that is attempting to write data the the FS?

Thats a good question. ive tried to disable swagger, and it seems to show its disabled. but even with this, im still getting the same error.
i tried to run locally on my machine using
npm run dev or netlify dev with

chmod -w * on root folder and chmod -w dist/src,
i did not see the read-only file system error.

but netlify dev wise, im getting error:

Error - Unable to determine event source based on event.

Hey @u007,

That error usually means that the event parser was unable to guess the type of event generator - for example, is it AWS Lambda, or is it something else. In Netlify’s case, it should be AWS Lambda, but the library that’s parsing it, is not able to detect it. I remember having this issue when I was using this library: integration: lambda gives "Unable to determine event source based on event." · Issue #416 · vendia/serverless-express · GitHub whe trying to integrate Express into my app.

I was able to workaround it by using serverless-http. Not sure if you’re using anything like that, and if you’re, maybe you could try the above?