Skip to content

Instantly share code, notes, and snippets.

@coderbyheart
Last active April 8, 2024 08:37
Show Gist options
  • Save coderbyheart/56c81c798bf97460c48ed4ceea7aae67 to your computer and use it in GitHub Desktop.
Save coderbyheart/56c81c798bf97460c48ed4ceea7aae67 to your computer and use it in GitHub Desktop.
Broken ESM Lambda when folder exists with same name as handler module

When a lambda is authored with this structure:

acme.js
acme/lib.js

and the acme.js imports from acme/lib.js it breaks:

{
  "errorType": "Error",
  "errorMessage": "require() of ES Module /var/task/acme.js from /var/runtime/index.mjs not supported.\nInstead change the require of acme.js in /var/runtime/index.mjs to a dynamic import() which is available in all CommonJS modules.",
  "trace": [
    "Error [ERR_REQUIRE_ESM]: require() of ES Module /var/task/acme.js from /var/runtime/index.mjs not supported.",
    "Instead change the require of acme.js in /var/runtime/index.mjs to a dynamic import() which is available in all CommonJS modules.",
    "    at _tryRequireFile (file:///var/runtime/index.mjs:1002:37)",
    "    at _tryRequire (file:///var/runtime/index.mjs:1046:29)",
    "    at _loadUserApp (file:///var/runtime/index.mjs:1081:22)",
    "    at UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:27)",
    "    at start (file:///var/runtime/index.mjs:1282:42)"
  ]
}

acme.js

import { hello } from './acme/lib.js'

export const handler = async (event) => {
  // TODO implement
  const response = {
    statusCode: 200,
    body: JSON.stringify(hello()),
  };
  return response;
};

acme/lib.js

export const hello = () => 'Hello from lib!'

package.json

{"type":"module"}
import { hello } from './acme/lib.js'
export const handler = async (event) => {
// TODO implement
const response = {
statusCode: 200,
body: JSON.stringify(hello()),
};
return response;
};
export const hello = () => 'Hello from lib!'
{"type":"module"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment