I want to deploy my project which is created by express.js, mongoose and apollo server on Netlify and when I run the project by "set NODE_ENV=development&& npm run start:lambda" command it throws the following error, how can I fix it. error: unable to det

    // src/lambda/server.js
    const express = require("express");
const path = require("path");
const mongoose = require("mongoose");
const graphqlUploadExpress = require("graphql-upload/graphqlUploadExpress.js");
const authMiddleware = require("../v1/middlewares/auth");
const cors = require("cors");
const { I18n } = require("i18n");
const apolloServerHandler = require("../apolloServer");

if (process.env.NODE_ENV != "production") {
  require("dotenv").config();
}

const i18n = new I18n();

i18n.configure({
  locales: ["en"],
  defaultLocale: "en",
  directory: path.join(__dirname, "../v1/locales"),
});

const app = express();
app.use(cors());
app.use((req, res, next) => {
  res.setHeader("Access-Control-Allow-Headers", "Content-Type, Autorization");
  next();
});

app.use((req, res, next) => {
  req.i18n = i18n;
  next();
});

app.use(graphqlUploadExpress());
app.use("/", express.static(path.join(__dirname, "../v1/uploads")));

app.use(authMiddleware);

const URI = process.env.URI;

mongoose.set("strictQuery", false);
mongoose.connect(URI, { family: 4 }, (err) => {
  throw err;
});

exports.handler = (event, context, callback) => {
  return apolloServerHandler(
    {
      ...event,
      requestContext: event.requestContext || {},
    },
    context,
    callback
  );
};

// src/apolloServer.js
const constructResolvers = require("./v1/graphql/resolvers");
const constructSchema = require("./v1/graphql/schema");
const typeDefs = constructSchema.typeDefs;
const resolvers = constructResolvers.resolvers;
const { ApolloServer } = require("apollo-server-lambda");

const apolloServer = new ApolloServer({
  typeDefs,
  resolvers,
});
const apolloServerHandler = apolloServer.createHandler({
    cors: {
      origin: '*'
    }
  });
module.exports = apolloServerHandler;

Hi @towfiqamiri, kindly visit the documentation below if you have not done so already on how to properly deploy express on Netlify

In addition, you can also watch the video below for some guidance.

Thanks for your guidance, but I want to integrate my express app with apollo server, so how can I do it?

Hi @towfiqamiri, I suggest you create a minimal version as test to see if it works and then build upon the test. You can use the basic code below to see if it works.

You have 2 options to try out using the minimal example below.

1. Use netlify-lambda (For Netlify Functions)

Since Netlify uses AWS Lambda functions under the hood, Express needs to be wrapped as a serverless function.

Steps:

Step 1: Install Dependencies

npm install express apollo-server-express graphql netlify-lambda

Step 2: Create Your Express + Apollo Server

Inside a folder like src/, create a file called graphql.js:

const express = require("express");
const { ApolloServer, gql } = require("apollo-server-express");
const serverless = require("serverless-http");

const app = express();

// Define GraphQL schema
const typeDefs = gql`
  type Query {
    hello: String
  }
`;

const resolvers = {
  Query: {
    hello: () => "Hello from Apollo Server on Netlify!",
  },
};

// Create Apollo Server
const server = new ApolloServer({ typeDefs, resolvers });
await server.start();
server.applyMiddleware({ app, path: "/graphql" });

module.exports.handler = serverless(app);

Step 3: Configure Netlify Functions

Create a netlify.toml file in the root directory:

[build]
  functions = "functions"

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

Step 4: Deploy to Netlify

Push your code to GitHub and deploy it on Netlify. Netlify will automatically detect the functions directory and deploy your GraphQL API.


2. Alternative: Using Netlify Functions Directly

Instead of using Express, you can use Apollo Server as a standalone function:

1. Install Apollo Server for Lambda

npm install apollo-server-lambda graphql

2. Create a Netlify Function (functions/graphql.js)

const { ApolloServer, gql } = require("apollo-server-lambda");

const typeDefs = gql`
  type Query {
    hello: String
  }
`;

const resolvers = {
  Query: {
    hello: () => "Hello from Apollo Server on Netlify!",
  },
};

const server = new ApolloServer({ typeDefs, resolvers });

exports.handler = server.createHandler();

This method is simpler because you don’t need Express.

I suggest using the 2nd Option Using Netlify Functions Directly to see if it works.

For more information, kindly visit the Apollo documentation below on how to deploy Apollo with Netlify functions if you have not done so already.
Thanks.