Using ES Modules in API Routes / Transpilation target of api routes and modules? #15115
Unanswered
janus-reith
asked this question in
Help
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am unable to import ES Modules in API Routes.
For example when importing from https://github.com/reactioncommerce/api-utils, this fails as it won't detect the declared export in the package.json: https://github.com/reactioncommerce/api-utils/blob/5b32c0066cc93eb28155caa4507704d735694666/package.json#L26
Directly import from the
/lib
subfolder will then fail due to the use of imports in there, so I guess this is expected to be CJS.Currently I could work around this by using
next-transpile-modules
and declaring webpack resolve aliases for that/lib
subfolder in mynext.config.js
. (As actually, I'm dealing with multiple modules importing from each other.Node 12 could use these without any transpilation when using the
--experimental-modules
and in this case, also--experimental-json-modules
flags. But with serverless deployments in mind, I'm wondering if that currently is worth achieving, as somehow I did't find much information yet if Lambdas are able to deal with ESM at all.I guess that I should aim for as as little transpilation for the server code as possible, to avoid degrading performance for some features. Like, ESM to CJS shoud be fine I guess, but I don't want a ES5 target.
However, there's tree things I wonder now:
next-transpile-modules
as part of the webpack config innext.config.js
, will my modules be transpiled properly for the server target? Also, are there alternatives? I guess I would only need CJS instead of ESM, but don't require any further feature transpilation besides that happening in the API routes anyway.Beta Was this translation helpful? Give feedback.
All reactions