Created
March 28, 2024 20:18
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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