-
-
Notifications
You must be signed in to change notification settings - Fork 796
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
Add for support for ESM #1014
Comments
Looks like it should be pretty easy. I just tweaked lines around ~69 in the InProcessRunner.js dist file and seems to handle loading modules. Using node 14.x without webpack.
|
Now that Node v14 support is released, this issue is the only blocker left (I hope) to fully use ES modules on lambda. I tried out your suggestion as a patch-package on my skeleton project and it seems to work: https://github.com/makenew/serverless-nodejs/blob/cab54afb4bd4acf97bfa3ea2a94bea50c1b88581/patches/serverless-offline%2B6.9.0.patch Only issue now is how to port this to the actual source code? Seems like the source already uses serverless-offline/src/lambda/handler-runner/in-process-runner/InProcessRunner.js Line 104 in 58dc8ae
Thus I suspect this is some webpack or babel magic that transforms the It's possible to distribute both using
Side note, we also need a way to deal with the file extension issue since the handler must be imported with a file extension, but how do we know if it's |
I worked a bit more on this. Turns out the AWS lambda runtime always loads the handler as a commonjs module via require. So while ESM support for this serverless-offline would be nice, it's not needed to deploy ESM to lambda today. You just need to have thin commonjs handler entry points and put all your ESM code somewhere else. (It should even be possible to put You can see a working example of this here (note the empty package.json file): https://github.com/makenew/serverless-nodejs/tree/v7.0.0/handlers |
I cannot find a reference to this, can you link the source or was this trial and error testing ? I would like to add my thanks for your time looking in to this. |
I did not find documentation for this. I tired deploying and invoking the function as an ES module but got this error:
Or re-formatting so you can actually read it:
If I had to take a guess, AWS probably just updated their lambda runtime to Node 14 without changing the code that loads the handler. It's possible there may be some way to make it load the ES module, but without access I bet it would be possible with a custom lambda runtime that used |
Any idea of when we can expect this support ? We are using Webpack at the moment, but if ESM modules are supported we can strip that from our process 🙏 |
You can work around this by putting all your handler entry points in their own folder with an "empty" package.json. If you want ES modules in the same folder as the handlers, you can use the .mjs extension. For a working example, see https://github.com/makenew/serverless-nodejs/blob/fa148a9dd210c21736c84014c32f6da151176aba/handlers/todo.js |
Lambda now supports ESM for node 14 https://aws.amazon.com/about-aws/whats-new/2022/01/aws-lambda-es-modules-top-level-await-node-js-14/ |
Any update on this issue, now that ESM is natively supported in lambda? I've got a few projects where developers went whole hog and used ESM in production without realizing their offline configs were now broken. |
We'd be happy to accept a PR that adds this 👍 |
I tried removing |
Honestly I'm not an expert on the topic so I don't have any opinion, I would have to look deeper into that topic, but I definitely won't be able to dedicate time to this in the foreseeable future. |
I'm interested in looking further into this, but I suspect that this might involve replacing |
vitest supports mocking esm dependencies and has a strong level of API compatibility with jest: https://vitest.dev/ |
Ah nice, didn't know that! That could save a lot of work!!! |
If there would be a need for migration to another test runner, then |
I did forget that vitest is more aimed at front end code, so perhaps that's not so great a suggestion. |
Well.. now I know why the previous developer on the project I just inherited was transpiling a node project in 2022 🤦 Stack traces on exceptions in our CloudWatch logs are essentially useless, even w/ |
Did anyone get
|
Still not able to use a package with Setting |
You can apply the patch here using |
Gotta love how much "unit testing" "speeds up development" 🤪 |
Any update on this, and of course the common dependent plug-ins, e.g. offline, prune, ssm offline? With node 16 now available for Lambda and more and more modules moving to ESM we'd like to start using ESM for new projects... |
This patch broke in 9.x |
Here's the new patch: diff --git a/node_modules/serverless-offline/src/lambda/handler-runner/in-process-runner/InProcessRunner.js b/node_modules/serverless-offline/src/lambda/handler-runner/in-process-runner/InProcessRunner.js
index f42be20..81286dd 100644
--- a/node_modules/serverless-offline/src/lambda/handler-runner/in-process-runner/InProcessRunner.js
+++ b/node_modules/serverless-offline/src/lambda/handler-runner/in-process-runner/InProcessRunner.js
@@ -53,7 +53,16 @@ export default class InProcessRunner {
// eslint-disable-next-line import/no-dynamic-require
;({ [this.#handlerName]: handler } = require(this.#handlerPath))
} catch (err) {
- log.error(err)
+ if (err.code?.includes('ERR_REQUIRE_ESM')) {
+ try {
+ const { [this.#handlerName]: esHandler } = await import(`file://${require.resolve(this.#handlerPath)}`);
+ handler = esHandler;
+ } catch (err) {
+ log.error(err)
+ }
+ } else {
+ log.error(err)
+ }
}
if (typeof handler !== 'function') { And a much more thorough fix that covers more cases is coming soon with #1397 |
resolved in: #1534 |
Feature Request
Add the ability to use (ESM modules)(https://nodejs.org/docs/latest-v12.x/api/esm.html#esm_interoperability_with_commonjs)
Currently if you declare your serverless project as type module in package.js. Node will flag that InProcessRunner.js is trying to require an es module
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module
Expected behavior/code
Any ES module used as entry point should work without any further compiling (webpack/babel)
The text was updated successfully, but these errors were encountered: