Simple function gives Runtime.HandlerNotFound - main.handler is undefined or not exported

I am testing my nx repro with simple function deploy. It is typescript project and function code is very simple.

import {Handler} from "@netlify/functions";

const handler: Handler = async (event, context) => {
    return {
        statusCode: 200,
        body: JSON.stringify({message: "Hello World"}),
    };
};

export default handler;

But i am getting the following error:

Runtime.HandlerNotFound: main.handler is undefined or not exported
    at Object.UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1034:15)
    at async start (file:///var/runtime/index.mjs:1194:23)
    at async file:///var/runtime/index.mjs:1200:1

Anybody has idea what I am doing wrong?

Thanks

Try changing

to

export { handler };

as shown in this documentation.

Thanks, sorry. The code is actually as follows but I am still getting this error.

import {Handler} from "@netlify/functions";

const handler: Handler = async (event, context) => {
    return {
        statusCode: 200,
        body: JSON.stringify({message: "Hello World"}),
    };
};

export {handler};

Your code works fine for me using Netlify CLI.

I think is problem with NX. When compiled I get the following code:

const Module = require("module");
const path = require("path");
const fs = require("fs");
const originalResolveFilename = Module._resolveFilename;
const distPath = __dirname;
const manifest = [{ "module": "@spacebrainz/core/components/sidebar", "exactMatch": "libs/core/components/sidebar/src/index.js", "pattern": "libs/core/components/sidebar/src/index.ts" }, { "module": "@spacebrainz/core/data", "exactMatch": "libs/core/data/src/index.js", "pattern": "libs/core/data/src/index.ts" }, { "module": "@spacebrainz/core/layout", "exactMatch": "libs/core/layout/src/index.js", "pattern": "libs/core/layout/src/index.ts" }, { "module": "@spacebrainz/core/markdown", "exactMatch": "libs/core/markdown/src/index.js", "pattern": "libs/core/markdown/src/index.ts" }];
Module._resolveFilename = function(request, parent) {
  let found;
  for (const entry of manifest) {
    if (request === entry.module && entry.exactMatch) {
      const entry2 = manifest.find((x) => request === x.module || request.startsWith(x.module + "/"));
      const candidate = path.join(distPath, entry2.exactMatch);
      if (isFile(candidate)) {
        found = candidate;
        break;
      }
    } else {
      const re = new RegExp(entry.module.replace(/\*$/, "(?<rest>.*)"));
      const match = request.match(re);
      if (match?.groups) {
        const candidate = path.join(distPath, entry.pattern.replace("*", ""), match.groups.rest + ".js");
        if (isFile(candidate)) {
          found = candidate;
        }
      }
    }
  }
  if (found) {
    const modifiedArguments = [found, ...[].slice.call(arguments, 1)];
    return originalResolveFilename.apply(this, modifiedArguments);
  } else {
    return originalResolveFilename.apply(this, arguments);
  }
};
function isFile(s) {
  try {
    return fs.statSync(s).isFile();
  } catch (_e) {
    return false;
  }
}
require("./apps/spacez-backend/src/index.js");

That would seem the likely culprit. Though I’ve never used it so couldn’t suggest why it is building (or attempting to build) a Netlify function.

HA, figured out the issue. I used the wrong bundler. Default setting is esbuild when you add new app to the NX workspace. I created new project using webpack and it start working.

1 Like

Hi @nolafs :wave:t6: thanks so much for coming back and sharing your solution with the community. Nice chat and good luck. :rocket: