Deploy express JS to netlify

I’m currently deploying my express file to netlify platform. All the tutorials are showing to use netlify-lambda package. But the package is deprecated and advised to use netlify cli. But for build command, tutorials suggesting netlify-lambda build, which is deprecated. I used normal build method like, ‘build application.js’. The sites deploys because I pointed correct folder path for fronted, but unfortunately api calls not working. It’s working with netlify-lambda build command, but as it’s deprecated it’s not a good approach. So can I know how to deploy with latest method?

Have you read through this article?

Yes. If you go through the application example provided, in github repo, you can see it’s uses netlify-lamda as well. Which is deprecated as I mentioned. So I need a solution that doesn’t inquire netlify-lambda as it’s deprecated and it’s already has High vulnerabilities, which I don’t think a good approach to use. So I’m waiting for a new solution. Thanks for the help though, I appreciate that

Here’s a live example: netlify-file-browser/api.js at v2 · Hrishikesh-K/netlify-file-browser · GitHub

1 Like

I need the build command in package.json , because my application already works fine in netlify with API calls, but with netlify-lambda which is deprecated. So if you can provide package.json or at least build comamnd, that would be helpful. Thank you for your response though

That’s an entire repo which has everything you need though?

But to answer your question, there’s no build command for Functions. Functions are built and packaged automatically.

Yes if there’s an updated guide that would helpful. Otherwise I guess we are again in square 1 in this discussion loop.

There’s no guide, nothing official from Netlify at least. You need to follow Netlify Functions’ documentation: Functions overview | Netlify Docs and write a function like this (just the minimum bare-bones):

import express, {Router} from 'express'
import serverless from 'serverless-http'
export async function handler(event, context) {
  const app = express()
  const router = Router()
  app.use('/api/', router)
  return serverless(app)(event, context)
}

add the following to your netlify.toml:

[functions]
  external_node_modules = ["express"]
  node_bundler = "esbuild"
[[redirects]]
  force = true
  from = "/api/*"
  status = 200
  to = "/.netlify/functions/api/:splat"

and that’s it.

1 Like

@Cutting_Edge,

We’ve updated our docs: Express on Netlify | Netlify Docs to make this more clear.

1 Like

I have followed Express on Netlify | Netlify Docs precisely and it only ever results in a default Netlify 404 page.

my netlify.toml:

[build]
  command = "echo Building functions"
[functions]
  external_node_modules = ["express"]
  node_bundler = "esbuild"
[[redirects]]
  force = true
  from = "/api/*"
  status = 200
  to = "/.netlify/functions/api/:splat"

and my netlify/functions/api.mjs:

import express, { Router } from 'express';
import serverless from 'serverless-http';

export async function handler(event, context) {
  const app = express();
  const router = Router();
  router.get('/', (req, res) => res.send('Hello World!'));
  app.use('/api/', router);
  return serverless(app)(event, context);
};

using node v20. is there something that’s changed in the last year that would cause this?

adding functions = "netlify/functions/" under the [build] section in the netlify.toml file seems to fix it.