netlify / remix-template

Deploy your Remix site to Netlify
83 stars 27 forks source link

support serverModuleFormat: "cjs" #135

Closed maggisk closed 9 months ago

maggisk commented 9 months ago

Is your feature request related to a problem? Please describe.

I'm trying to upgrade a project to remix version 2 that runs on netlify. It looks like it would be a huge effort to get it to run with esm module format and would therefor like to configure remix to use serverModuleFormat: "cjs". Everything seems to work fine running locally, but I can not get it to run on netlify.

Describe the solution you'd like

Being able to set remix config to serverModuleFormat: "cjs" and deploy to netlify.

Describe alternatives you've considered

I tried creating a fresh remix project from this starter and change serverModuleFormat to cjs. It runs fine locally but on netlify the frontpage returns 404 and opening the handler directly returns an error: Runtime.HandlerNotFound - server.handler is undefined or not exported

https://bucolic-blini-fa5899.netlify.app and https://bucolic-blini-fa5899.netlify.app/.netlify/functions/server

I also attempted converting our project to esm, but I don't think that's going to happen anytime soon, so we're stuck on remix v1 at the moment

ascorbic commented 9 months ago

Hi. The server module format shouldn't affect your site's module format. What happens if you try to deploy as-is, with your site in cjs and the server still as esm?

maggisk commented 9 months ago

Do you mean using cjs for your application but set the remix config serverModuleFormat to esm?

You'd have to remove "type": "module" from package.json but the remix build would output esm module containing import statements etc that I'm not sure how to fix. I tried setting serverBuildPath: "./build/index.mjs" but netlify won't even deploy any serverless functions

I made a demo project at https://github.com/Life-Swap/remix2test with a few branches that attempt to get things working but no luck so far.

It's deployed at https://bucolic-blini-fa5899.netlify.app with branch deploys enabled

ascorbic commented 9 months ago

If you remove "type": "module" and convert remix.config.js to cjs it should work.

ascorbic commented 9 months ago

https://deploy-preview-1--bucolic-blini-fa5899.netlify.app/

maggisk commented 9 months ago

That works. Thank you.

I also had to set serverBuildPath: "./build/index.mjs" in development for the dev server to treat it as esm

ascorbic commented 9 months ago

Great! The dev server shouldn't be using the Netlify config unless you're deploying to edge functions, because it should be using the Remix dev server instead. That's why there's the conditional env var in the sample config