Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Netlify functions not working with ESM; always build to module.exports CJS syntax even with package.json "type" set to "module" #6445

Open
CCBaxter84 opened this issue Mar 15, 2024 · 0 comments
Labels
type: bug code to address defects in shipped code

Comments

@CCBaxter84
Copy link

CCBaxter84 commented Mar 15, 2024

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:

Screenshot 2024-03-15 at 7 30 44 AM

package.json set for "module"
Screenshot 2024-03-15 at 7 37 59 AM

Of note, if I set my project to use CJS by removing "type": "module" from package.json, then the functions work:
Screenshot 2024-03-15 at 7 40 29 AM

Steps to reproduce

In a Vite Vue 3 app with Netlify globally installed

  1. Use provided netlify.toml file in Configuration section
  2. Create functions folder with following test.js file:
const handler = async (event, context) => {
  return {
    statusCode: 200,
    body: JSON.stringify({ message: "test" })
  }
}

export { handler }
  1. Run netlify dev
  2. 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

@CCBaxter84 CCBaxter84 added the type: bug code to address defects in shipped code label Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug code to address defects in shipped code
Projects
None yet
Development

No branches or pull requests

1 participant