Netlify functions not working with ESM; always build to module.exports CJS syntax even with package.json "type" set to "module"
Opened this issue · 0 comments
Describe the bug
In a simple app I just began today, Netlify functions aren't working with ESM. Despite setting node_bundler = "esbuild"
in my netlify.toml file, the bundled version of my functions file still includes "CJS module.exports" syntax, which throw errors in testing:
Of note, if I set my project to use CJS by removing "type": "module" from package.json, then the functions work:
Steps to reproduce
In a Vite Vue 3 app with Netlify globally installed
- Use provided netlify.toml file in Configuration section
- Create functions folder with following test.js file:
const handler = async (event, context) => {
return {
statusCode: 200,
body: JSON.stringify({ message: "test" })
}
}
export { handler }
- Run
netlify dev
- Test netlify function: in VSCode, I'm using the REST client with following code within an apiTests.http file:
get http://localhost:<port>/.netlify/functions/test
Configuration
[build]
functions = "functions"
[build.environment]
NODE_VERSION = "20.11.1"
[functions]
directory = "functions"
node_bundler = "esbuild"
Environment
System:
OS: macOS 14.0
CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Memory: 11.95 GB / 32.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.11.1 - ~/.nvm/versions/node/v20.11.1/bin/node
npm: 10.2.4 - ~/.nvm/versions/node/v20.11.1/bin/npm
npmGlobalPackages:
netlify-cli: 17.19.3