Skip to content

Instantly share code, notes, and snippets.

@perpil
Created March 28, 2024 20:18
Show Gist options
  • Select an option

  • Save perpil/7e2c41638e11ba311272842e33aa1a9a to your computer and use it in GitHub Desktop.

Select an option

Save perpil/7e2c41638e11ba311272842e33aa1a9a to your computer and use it in GitHub Desktop.
Serverless .mjs support patch. Put in patches/ directory. Use with https://www.npmjs.com/package/patch-package
diff --git a/node_modules/serverless/lib/plugins/aws/invoke-local/index.js b/node_modules/serverless/lib/plugins/aws/invoke-local/index.js
index b227110..6d10a53 100644
--- a/node_modules/serverless/lib/plugins/aws/invoke-local/index.js
+++ b/node_modules/serverless/lib/plugins/aws/invoke-local/index.js
@@ -838,12 +838,15 @@ class AwsInvokeLocal {
if (error.code === 'ERR_REQUIRE_ESM') {
return await require('../../../utils/import-esm')(`${modulePath}.js`);
} else if (error.code === 'MODULE_NOT_FOUND') {
- // Attempt to require handler with `.cjs` extension
- pathToHandler = `${pathToHandler}.cjs`;
+ // Attempt to require handler with `.cjs` or `.mjs` extension
try {
- return require(pathToHandler);
+ if (await fse.pathExists(`${modulePath}.cjs`)) {
+ return require(`${modulePath}.cjs`);
+ } else if (await fse.pathExists(`${modulePath}.mjs`)) {
+ return await require('../../../utils/import-esm')(`${modulePath}.mjs`);
+ }
} catch (innerError) {
- // Throw original error if still "MODULE_NOT_FOUND", as not finding module with `.cjs` might be confusing
+ // Throw original error if still "MODULE_NOT_FOUND", as not finding module with `.cjs` or `.mjs` might be confusing
if (innerError.code !== 'MODULE_NOT_FOUND') {
throw innerError;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment