question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(aws-lambda-nodejs): `.mjs` file extension and `import.meta` not supported

See original GitHub issue

Although Lambda now supports Node v14:

  1. .mjs entry files are rejected by a regex pattern in aws-lambda-nodejs
  2. esbuild is transpiling the scripts to cjs format (commonjs) but BundlingOptions provides no means to specify format: "esm", and consequently esbuild polyfills import.meta which breaks all of its uses in the scripts

Reproduction Steps

lib/my-stack.mjs:

export class MyStack extends Stack {
    constructor(scope, id, props) {
        super(scope, id, props);

        // ...

        new NodejsFunction(this, 'Example', {
            runtime: Runtime.NODEJS_14_X,
            entry: 'src/entry-file.mjs',
            bundling: {
                target: 'es2020',
                // format: 'esm',   <-- should be able to pass this option here
            },
        };

        // ...
    }
}

src/entry-file.mjs:

export async function Example() {
    return import.meta.url;
}

What did you expect to happen?

  1. entry-file.mjs to be allowed to be used as an entry file.
  2. import.meta to be defined and Example() to return a string.

What actually happened?

  1. Error: Only JavaScript or TypeScript entry files are supported.
  2. Example() returns undefined since import.meta is polyfilled with an empty plain object.

Environment

  • CDK CLI Version : n/a
  • Framework Version:
  • Node.js Version: 14.13.0
  • OS : n/a
  • Language (Version):

Other

  1. For the .mjs extension: https://github.com/aws/aws-cdk/blob/5d71d8e815529ccde7ffe13bb72f57ae88e896dc/packages/%40aws-cdk/aws-lambda-nodejs/lib/function.ts#L140

  2. Allow passing in the format option to esbuild: https://github.com/aws/aws-cdk/blob/5d71d8e815529ccde7ffe13bb72f57ae88e896dc/packages/%40aws-cdk/aws-lambda-nodejs/lib/bundling.ts#L141-L158


This is 🐛 Bug Report

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:27 (12 by maintainers)

github_iconTop GitHub Comments

10reactions
ronaldocpontescommented, Mar 9, 2021

This is what I get when using “type”: “module” on package.json. As you can see on the error: require() of ES modules is not supported. require() of /var/task/index.js…

The lambda engine is using require() to load the code on index.js.

# package.json
{
"name": "myLambda",
"type": "module",
"main": "index.js",
}
# index.js
export const handler = async (event) => {  
    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};
Response
{
  "errorType": "Error",
  "errorMessage": "Must use import to load ES Module: /var/task/index.js\nrequire() of ES modules is not supported.\nrequire() of /var/task/index.js from /var/runtime/UserFunction.js is an ES module file as it is a .js file whose nearest parent package.json contains \"type\": \"module\" which defines all .js files in that package scope as ES modules.\nInstead rename index.js to end in .cjs, change the requiring code to use import(), or remove \"type\": \"module\" from /var/task/package.json.\n",
  "trace": [
    "Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /var/task/index.js",
    "require() of ES modules is not supported.",
    "require() of /var/task/index.js from /var/runtime/UserFunction.js is an ES module file as it is a .js file whose nearest parent package.json contains \"type\": \"module\" which defines all .js files in that package scope as ES modules.",
    "Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove \"type\": \"module\" from /var/task/package.json.",
    "",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1080:13)",
    "    at Module.load (internal/modules/cjs/loader.js:928:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:769:14)",
    "    at Module.require (internal/modules/cjs/loader.js:952:19)",
    "    at require (internal/modules/cjs/helpers.js:88:18)",
    "    at _tryRequire (/var/runtime/UserFunction.js:75:12)",
    "    at _loadUserApp (/var/runtime/UserFunction.js:95:12)",
    "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
    "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
    "    at Module._compile (internal/modules/cjs/loader.js:1063:30)"
  ]
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

NodeJS 14.x - Native AWS Lambda Import/Export Support
A commonJS file can access ESM files if the have .mjs extension, but as typescript compiles to .js I use some unix commands...
Read more >
import.meta - JavaScript - MDN Web Docs
The import.meta meta-property exposes context-specific metadata to a JavaScript module. It contains information about the module, ...
Read more >
@aws-cdk/aws-lambda-nodejs - npm
Start using @aws-cdk/aws-lambda-nodejs in your project by running `npm i ... accepts .js, .jsx, .ts, .tsx and .mjs files handler: 'myExportedFunc', ...
Read more >
@aws-cdk/aws-lambda-nodejs | Yarn - Package Manager
Amazon Lambda Node.js Library. cdk-constructs: Stable. This library provides constructs for Node.js Lambda functions. Node.js Function.
Read more >
Using Node.js ES modules and top-level await in AWS Lambda
By setting the type to “module”, you designate all “.js” files in the ... named index.mjs – it will always be treated as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found