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): Use esbuild API instead of CLI for bundling

See original GitHub issue

What is the problem?

If a dependency has a .node dependency, for example ssh2 in node_modules/ssh2/lib/protocol/crypto.js has

binding = require('./crypto/build/Release/sshcrypto.node');,

this will have esbuild fail the bundling with

error: No loader is configured for ".node" files: ../node_modules/ssh2/lib/protocol/crypto/build/Release/sshcrypto.node.

In https://github.com/evanw/esbuild/issues/1051 they provide a plugin, but sadly plugins can’t be used with binary esbuild.

Reproduction Steps

Run npx cdk init app --language=typescript to create sample app

Add new NodejsFunction(this, 'testfunc', { entry: 'lib/testfunc/index.ts' }); to test stack

Add to file lib/testfunc/index.ts:

import { Client } from "ssh2";

const handler = async () => {
  const conn = new Client();

  conn.end();
};

export { handler };

Add to file lib/testfunc/package.json:

{
  "dependencies": {
    "ssh2": "^1.5.0"
  }
}

Run npm run cdk synth

What did you expect to happen?

It bundling the example code

What actually happened?

$ npm run cdk synth

> cdktest@0.1.0 cdk
> cdk "synth"

Bundling asset CdktestStack/testfunc/Code/Stage...
✘ [ERROR] No loader is configured for ".node" files: lib/testfunc/node_modules/cpu-features/build/Release/cpufeatures.node

    lib/testfunc/node_modules/cpu-features/lib/index.js:1:24:
      1 │ const binding = require('../build/Release/cpufeatures.node');
        ╵                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

✘ [ERROR] No loader is configured for ".node" files: lib/testfunc/node_modules/ssh2/lib/protocol/crypto/build/Release/sshcrypto.node

    lib/testfunc/node_modules/ssh2/lib/protocol/crypto.js:30:20:
      30 │   binding = require('./crypto/build/Release/sshcrypto.node');
         ╵                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2 errors

/home/azatoth/tmp/cdktest/node_modules/aws-cdk-lib/core/lib/asset-staging.ts:395
      throw new Error(`Failed to bundle asset ${this.node.path}, bundle output is located at ${bundleErrorDir}: ${err}`);
            ^
Error: Failed to bundle asset CdktestStack/testfunc/Code/Stage, bundle output is located at /home/azatoth/tmp/cdktest/cdk.out/bundling-temp-43706a6374f4884c6246dca414315bc291a0eff7496c6eda3da7968ee92f4c47-error: Error: bash -c npx --no-install esbuild --bundle "/home/azatoth/tmp/cdktest/lib/testfunc/index.ts" --target=node14 --platform=node --outfile="/home/azatoth/tmp/cdktest/cdk.out/bundling-temp-43706a6374f4884c6246dca414315bc291a0eff7496c6eda3da7968ee92f4c47/index.js" --external:aws-sdk run in directory /home/azatoth/tmp/cdktest exited with status 1
    at AssetStaging.bundle (/home/azatoth/tmp/cdktest/node_modules/aws-cdk-lib/core/lib/asset-staging.ts:395:13)
    at AssetStaging.stageByBundling (/home/azatoth/tmp/cdktest/node_modules/aws-cdk-lib/core/lib/asset-staging.ts:243:10)
    at stageThisAsset (/home/azatoth/tmp/cdktest/node_modules/aws-cdk-lib/core/lib/asset-staging.ts:134:35)
    at Cache.obtain (/home/azatoth/tmp/cdktest/node_modules/aws-cdk-lib/core/lib/private/cache.ts:24:13)
    at new AssetStaging (/home/azatoth/tmp/cdktest/node_modules/aws-cdk-lib/core/lib/asset-staging.ts:159:44)
    at new Asset (/home/azatoth/tmp/cdktest/node_modules/aws-cdk-lib/aws-s3-assets/lib/asset.ts:72:21)
    at AssetCode.bind (/home/azatoth/tmp/cdktest/node_modules/aws-cdk-lib/aws-lambda/lib/code.ts:180:20)
    at new Function (/home/azatoth/tmp/cdktest/node_modules/aws-cdk-lib/aws-lambda/lib/function.ts:350:29)
    at new NodejsFunction (/home/azatoth/tmp/cdktest/node_modules/aws-cdk-lib/aws-lambda-nodejs/lib/function.ts:50:5)
    at new CdktestStack (/home/azatoth/tmp/cdktest/lib/cdktest-stack.ts:9:5)
Subprocess exited with error 1

CDK CLI Version

2.8.0 (build 8a5eb49)

Framework Version

No response

Node.js Version

v14.18.3

OS

Linux

Language

Typescript

Language Version

No response

Other information

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:12
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
shellscapecommented, Mar 29, 2022

In many cases, using the esbuild API instead of the CLI will open up the plugins configuration for ESBuild to the users.

0reactions
wprlcommented, Nov 7, 2022

Brainstorming, it seems like mostly what is needed is an option to override this command to use a custom build script: https://github.com/aws/aws-cdk/blob/v2.50.0/packages/%40aws-cdk/aws-lambda-nodejs/lib/bundling.ts#L180 and also to copy the custom build script to the Docker image when building via Docker…

For reference, the issue as seen from the esbuild side: https://github.com/evanw/esbuild/issues/884

Read more comments on GitHub >

github_iconTop Results From Across the Web

API - esbuild
This API call is used by the command-line interface if no input files are provided and the --bundle flag is not present. In...
Read more >
BundlingOptions — AWS Cloud Development Kit 1.183.0 ...
A custom bundling Docker image. This image should have esbuild installed globally. If you plan to use nodeModules it should also have npm...
Read more >
Bundling Prisma with the CDK using aws-lambda-nodejs
This is a walkthrough of the steps required to bundle Prisma with CDK ... aws-lambda-nodejs uses esbuild under the hood so your function...
Read more >
Using Typescript and Esbuild to deploy a Lambda Function ...
A CDK construct called AWS Lambda Nodejs helps us achieve our goal. When running the command cdk deploy , it will first build...
Read more >
Package your NodeJS Lambda functions individually with ...
Using esbuild with AWS SAM. Configuring your project. In this setup, we will bypass sam build and set up an esbuild bundling step...
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