node.jsaws-lambdaserverlesses6-modules

Error [ERR_MODULE_NOT_FOUND]: Cannot find module occurs when invoking a serverless lambda function locally while functioning well on remote aws


To improve the project's functionality and overall performance. As part of this effort, we updated our SLS "handler.js" file to use ES modules and renamed it to "handler.mjs".

The Lambda function was executed successfully on remote (AWS) and everything is functioning well.


We are experiencing an issue when invoking the Serverless Lambda function locally.


I am attaching my local console log for reference.

✖ Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/handler.js' imported from /Users/.serverless/releases/4.4.19/package/dist/sf-core.js
    at finalizeResolution (node:internal/modules/esm/resolve:265:11)
    at moduleResolve (node:internal/modules/esm/resolve:933:10)
    at defaultResolve (node:internal/modules/esm/resolve:1169:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:540:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:509:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:239:38)
    at ModuleLoader.import (node:internal/modules/esm/loader:472:34)
    at defaultImportModuleDynamicallyForModule (node:internal/modules/esm/utils:214:31)
    at importModuleDynamicallyCallback (node:internal/modules/esm/utils:253:12)
    at loadModule (file:///Users/.serverless/releases/4.4.19/package/dist/sf-core.js:919:6595)
    at async AwsInvokeLocal.invokeLocalNodeJs (file:///Users/.serverless/releases/4.4.19/package/dist/sf-core.js:919:5755)
    at async PluginManager.runHooks (file:///Users/.serverless/releases/4.4.19/package/dist/sf-core.js:925:9311)
    at async PluginManager.invoke (file:///Users/.serverless/releases/4.4.19/package/dist/sf-core.js:925:10082)
    at async PluginManager.run (file:///Users/.serverless/releases/4.4.19/package/dist/sf-core.js:925:10813)
    at async Serverless.run (file:///Users/.serverless/releases/4.4.19/package/dist/sf-core.js:932:10667)
    at async runFramework (file:///Users/.serverless/releases/4.4.19/package/dist/sf-core.js:980:2770) {
  code: 'ERR_MODULE_NOT_FOUND',
  url: 'file:///Users/handler.js'
}

Basically, it is a simple fucntion just a console.log

I have explored multiple options to resolve it


Here are some URLs for reference: serverless/issues/11920 serverless-support-for-aws-sdk-v3


I tested out different versions of Node and serverless.


Solution

  • Explored numerous options to resolve the problem. here are some option I have tried

    1. added "type": "module" in package.json
    2. clean cache, clear dependencies and reinstall everything

    rm -rf .serverless/ .webpack/ node_modules package-loc.json npm install

    1. debug using serverless print
    2. finally worked! explicitly set .mjs in serverless.yml

    Before serverless.yml

    functions:
      myfunction:
        handler: handler.main
    

    After serverless.yml

    functions:
      myfunction:
        handler: handler.mjs.main
    

    That means Serverless Framework wasn't automatically detecting .mjs files