Netlify site: leafy-begonia-4fe4c1
I’m getting a very annoying issue when trying to call my lambda functions. I’m running a Next.js site, with a handful of backend functions.
When I call any of my functions I get this message:
{"errorType":"ReferenceError","errorMessage":"module is not defined in ES module scope\nThis file is being treated as an ES module because it has a '.js' file extension and '/var/task/package.json' contains \"type\": \"module\". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.","trace":["ReferenceError: module is not defined in ES module scope","This file is being treated as an ES module because it has a '.js' file extension and '/var/task/package.json' contains \"type\": \"module\". To treat it as a CommonJS script, rename it to use the '.cjs' file extension."," at file:///var/task/___netlify-handler.js:1:1"," at ModuleJob.run (node:internal/modules/esm/module_job:218:25)"," at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)"," at async _tryAwaitImport (file:///var/runtime/index.mjs:1008:16)"," at async _tryRequire (file:///var/runtime/index.mjs:1057:37)"," at async _loadUserApp (file:///var/runtime/index.mjs:1081:16)"," at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)"," at async start (file:///var/runtime/index.mjs:1282:23)"," at async file:///var/runtime/index.mjs:1288:1"]}
I do not get this issue when running locally with netlify dev
. Everything runs perfectly on local. I’ve tried other machines/OSs as well, it’s fine everywhere except in the deployment.
After much debugging I have reduced the issue down to be something to do with having a custom path set in the function config.
This code fails to run with the module exception:
import { type Config, type Context } from "@netlify/functions";
export const config: Config = {
path: "/api/test", // <------- [4] NOTE THIS LINE
};
export default async function test(req: Request, context: Context): Promise<Response> {
await new Promise((resolve) => {
setTimeout(resolve, 1000);
});
return new Response(
JSON.stringify({
test: "success",
req,
context,
}),
);
}
If I remove line [4] where the path is set, the issue goes away and everything works fine. I’m still exporting a config object, but without path it works.
I would just take that win and forget about the nice looking url, however many of my actual functions take advantage of path parameters (/api/user/:userId
) and refactoring those out will be a massive pain at this point.
My functions are all written in typescript, and I have attempted putting in my own build system to bundle convert them to js, cjs or mjs as part of the build step, but this made no difference to the error returned. I am a little uncertain here if the deployment picks up cjs or mjs over the ts files, the docs only state that js files are preferred to ts.
I do have in my package type
set to module
, so it is correct about that in the error message, but this is normal for a Next.js project and my code is indeed all module based anyway.
Using Node 20 as my runtime. I don’t believe I’ve done anything unusual with any of my config. The project is based on T3 App and I’ve changed nothing much in any of the config.
I have also attempted to reuse the default netlify function path, rather than my /api/
path:
path: "/.netlify/functions/test"
But this also fails to work and gives the module error.
My suspision is that it’s something to do with the Next.js SSR handler function, but I have no control over that or ability to debug it as it’s part of the Netlify environment.
Edit: I have just tried to rewrite a function into the Next.js API format, placing it in src/pages/api/test.ts
. No config export. This also returns the same error about ES modules. I can only guess that under the hood it’s doing something similar to to what I was doing in the functions folder with a path config.