opennextjs-netlify: [Bug]: ESM not supported with Netlify functions? (part 2)

https://github.com/netlify/next-runtime/issues/1456#issuecomment-1997058498

But just to be absolutely clear, Deploy Previews are currently using [@5](https://github.com/netlify/next-runtime/issues/1456#issuecomment-1994377971) which causes all deploys to fail for users who have opted in to the Next.js v5 Runtime. (both Deploy Previews on new pushes to branches and also regular deploys from new pushes to the main branch)

So without my workaround, the opted-in Next.js v5 Runtime is unusable.

Looks like there are potential issues with deploy previews.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 6
  • Comments: 30 (3 by maintainers)

Commits related to this issue

Most upvoted comments

same issue here with next.js 😦

I have also got this issue with my Next.js project. The issue affects both the Netlify functions (/netlify/functions/) and Next.js APIs (/src/pages/api/).

Using netlify dev will run the project correctly on local, but not when deployed to Netlify itself.

I started up my project using the T3 App tool and changed none of the configuration settings out of the box, and ran directly into this problem when adding an API.

This is a major failing of the Netlify Next.js environment and a complete blocker to my project. I didn’t want to but I’ll have to switch to Vercel to meet my deployment deadline at this point. I was only linked to this bug report after a couples weeks of posting on your forum and raising a support ticket.

I did discover that if you remove any path config from the Netlify functions it seemingly will work, at least in my reduced test case, but my APIs were all written with path parameters, so this wasn’t a viable workaround for me.

Minimum replication repo: https://github.com/timothy-bailey-redbox/t3-dash-test Forum conversion: https://answers.netlify.com/t/netlify-functions-es-module-error-when-using-paths-config/111942

Running on Node v20, with Next.js 14.

Checking dependencies @netlify/plugin-nextjs@4.41.3 uses @netlify/ipx@1.4.6 which uses ipx@1.3.1 which is quite old. Latest is v3 but I’m afraid there is much interest as i guess Netlify is planning to advocate new Native optimizer…

BTW almost all 3 major versions of IPX use dual ESM/CJS exports with explicit import extensions (unpkg v3 - unpkg v1.3.1).

Please let me know if I can do to help for this or if you spot any bundling issues, i can trigger a hotfix also for 1.x however I’m note sure issue is originating from ipx.

This is a hard blocker for a project I am on, unfortunately. We have an existing Next.js v13 app deployed to Vercel that is authored in ESM using type: "module" in package.json to allow for top-level await (which we depend on for reasonable ergonomics when working with slonik). We were looking to migrate to Next.js to get around some issues we’ve had with Vercel. On Netlify the build process runs flawlessly, but the serverless functions (e.g., ___netlify-handler) just fail due to their produced code using module.exports = require("...").

Here’s a minimal reproduction of the issue: esm.zip

@karlhorky - Thanks for the extra info. We’ll take a look at that now.

Potential Workaround

@MarcL @orinokai is this fix contained in the ā€œNext.js Runtime v5ā€ feature (currently @netlify/plugin-nextjs@5.0.0-rc.4) that was just offered to me in early access?

I just saw this in my Netlify dashboard under the Deploys page

Screenshot 2024-03-13 at 12 54 36

Maybe people in this thread can try this out and we can see whether this helps… (it was offered to me on the Deploys page of the site)

+1 on this. Still an issue

Yeah, the issue is still open. If you have something to add feel free, but please stop with +1 posts with no additional context—they notify all participants and anybody else that has subscribed to the issue looking for a solution. There are already 20+ people on the thread, and likely many more following. If you want to just support add a šŸ‘ to the first post and move on.

+1 on this. Still an issue

As a paying Netlify customer, I find this super annoying. Especially given how long this issue has been open. It’s 2024, and being unable to set "type": "module" because the run time can’t be bothered to support modern standards feels very archaic.

@pieh @ascorbic @orinokai @danez @sarahetter any chance of getting the fix mentioned in the PR published soon?

Or at least an officially-blessed workaround?

cc IPX maintainer @pi0 (_ipx.js is the failing file in the error messages above)

I’ve had the same issue. Turns out that my package.json had "type": "module", in it, which wasn’t needed. Removing this has fixed the issue. I think the built netlify functions look for the nearest package.json. Because it was a module then that’s what caused the issue.

What is strange is that mine worked with module for a good week or 2, so something that has been added has triggered this. At the moment I don’t have time to debug what that package is 🤷

I am having the same error. I have a next.js project with "type": "module" in the package.json.

image

Any update on this?

Hi, I’m having a similar issue.

To see the error message, try to load this image: https://gamliela.com/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Fproject-setup-ready.e736f9d0.png&w=1200&q=75

netlify.toml (site gamliela-website):

[build.environment]
  NODE_VERSION = "16"
  NPM_FLAGS = "--version" # prevent Netlify npm install
  AWS_LAMBDA_JS_RUNTIME = "nodejs16.x"
[build]
  publish = ".next"
  command = "npx pnpm@7.6.0 install && npm run build"

Source code: https://github.com/gamliela/website/tree/master

I also added NODE_VERSION and AWS_LAMBDA_JS_RUNTIME in the UI, as advised in this thread, but it didn’t help.