Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

netlify dev supports import() but building on Netlify doesn't #1025

Open
mjswensen opened this issue Feb 15, 2022 · 5 comments
Open

netlify dev supports import() but building on Netlify doesn't #1025

mjswensen opened this issue Feb 15, 2022 · 5 comments
Labels
type: bug code to address defects in shipped code

Comments

@mjswensen
Copy link

Describe the bug

In development, the following code works as expected:

exports.handler = async function (event) {
  const myValue = await import('my-package');
  // ...
};

But when deploying to production, my-package doesn't get bundled, resulting in 500 errors when the function tries to run (Cannot find package 'my-package' imported from /var/task/my-function.js)

To Reproduce

Steps to reproduce the behavior:

  1. Write a Netlify function with a dynamic import
  2. Run netlify dev
  3. See that the function runs properly
  4. Deploy to Netlify
  5. See that the deployed function doesn't run

Expected behavior

Either Netlify's production function bundling should support this case, or netlify dev should not. The behavior should match between the two.

@mjswensen mjswensen added the type: bug code to address defects in shipped code label Feb 15, 2022
@ehmicky ehmicky transferred this issue from netlify/cli Feb 15, 2022
@mjswensen
Copy link
Author

Confusingly, in development, when trying to require() an ES module, you get the following message:

Error: require() of ES Module <path to module> from <function path> not supported.
Instead change the require of index.js in <function path> to a dynamic import() which is available in all CommonJS modules.

Which leads users into this situation.

@mjswensen
Copy link
Author

I've found a workaround:

  1. Add node_bundler = "esbuild" to the [functions] section of netlify.toml.
  2. Convert functions from CommonJS to the ES Module syntax.
  3. Add the .netlify directory to .gitignore to exclude the build artifacts from source control.

Depending on which dependencies are getting bundled, CommonJS may no longer be an option for some users (like in my case). Perhaps esbuild should be the default for new projects?

@eduardoboucas
Copy link
Member

Hi @mjswensen! We're in the processing of rolling out a change that will address this issue, without you having to do any of the workarounds you described.

The change is going to be gradually rolled out over the next few days, but if you want I can enrol you in that group right now. You'd just need to send me your Netlify site ID (Go to Site settings > General > Site details > Site information, and copy the value for API ID). You can send it to eduardo@netlify.com if you don't want to share it in public.

Thanks!

@mjswensen
Copy link
Author

Hi @eduardoboucas , sounds great, I'll send that over now. Thanks!

@PH4NTOMiki
Copy link

Possibly related #1036

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: bug code to address defects in shipped code
Projects
None yet
Development

No branches or pull requests

3 participants