ilearnio / module-alias

Register aliases of directories and custom module paths in Node
MIT License
1.76k stars 69 forks source link

Bug using moduleDirectories in AWS Lambda #132

Open allenada opened 2 years ago

allenada commented 2 years ago

I'm unable to use moduleDirectories specification when deploying code to AWS Lambda

I believe bug is in this function

function getMainModule () {
  return require.main._simulateRepl ? undefined : require.main
}

Error I get is:

2022-11-12T00:51:00.555Z    undefined   ERROR   Uncaught Exception  
{
    "errorType": "TypeError",
    "errorMessage": "Cannot read properties of undefined (reading '_simulateRepl')",
    "stack": [
        "TypeError: Cannot read properties of undefined (reading '_simulateRepl')",
        "    at getMainModule (/var/task/node_modules/module-alias/index.js:216:23)",
        "    at addPath (/var/task/node_modules/module-alias/index.js:85:22)",
        "    at /var/task/node_modules/module-alias/index.js:210:7",
        "    at Array.forEach (<anonymous>)",
        "    at init (/var/task/node_modules/module-alias/index.js:206:35)",
        "    at Object.<anonymous> (/var/task/node_modules/module-alias/register.js:1:13)",
        "    at Module._compile (node:internal/modules/cjs/loader:1105:14)",
        "    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)",
        "    at Module.load (node:internal/modules/cjs/loader:981:32)",
        "    at Function.Module._load (node:internal/modules/cjs/loader:822:12)"
    ]
}

Seems fix should be to rewrite the method as:

function getMainModule () {
  return require.main && require.main._simulateRepl ? undefined : require.main
}
Kehrlann commented 2 years ago

Hey @allenada 👋

Isn't moduleDirectories a Jest thing?

In any case, would you care to open a pull request for this?