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.

(lambda-nodejs): NodeJsFunction does not correctly install function dependencies since 1.110.0

See original GitHub issue

This pull request which has been merged into 1.110.0 seems to create issues with our CDK project which cannot bundle our Lambda functions part of our project anymore.

While running npm ci instead of npm install seems like a good idea since it will not update package-lock.json, in reality it is preventing us to build our Lambda functions that are part of a CDK project.

Reproduction Steps

Create a sample CDK project and a NodeJsFunction that is part of the project. The function should be in its own directory with its own package.json and package-lock.json. Give the Lambda function a dependency (e.g lodash.template).

Instanciate NodeJsFunction in the CDK project to use with ESBuild.

const description = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'lambdas', 'transformer', 'package.json')).toString());
const transformer = new node.NodejsFunction(this, 'Transformer', {
  entry: `${__dirname}/lambdas/transformer/index.js`,
  runtime: lambda.Runtime.NODEJS_14_X,
  handler: 'handler',
  logRetention: logs.RetentionDays.ONE_MONTH,
  bundling: {
    nodeModules: Object.keys(description.dependencies),
    externalModules: ['aws-sdk']
  }
});

The above example will install the dependencies specified in the package.json of the Lambda function and since 1.110.0 will run npm ci instead of npm install.

What did you expect to happen?

The installation of the dependencies to be correctly installed and the function to be bundled.

What actually happened?

The installation failed with error cipm can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync. Please update your lock file with npm install before continuing..

This happens even if the package.json and the package-lock.json of the Lambda function are in sync.

The reason why this happens is because the package.json the AWS CDK creates for the function before running npm in the cdk.out/ directory is not the same as the original package.json of the Lambda function. The generated package.json in the bundle folder looks like the below.

{"dependencies": {"lodash.template": "^4.5.0"}}

Because npm ci will stop if it finds that the package.json and package-lock.json are not the same, which is clearly the case because the CDK generates a package.json instead of using the original one, we get the error message.

Environment

  • CDK CLI Version : 1.110.0
  • Framework Version:
  • Node.js Version: 14.17.0
  • OS : MacOS
  • Language (Version): TypeScript (4.3.5)

This is 🐛 Bug Report

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
jogoldcommented, Jul 9, 2021

@HQarroum sorry for the breaking change.

How about specifying depsLockFilePath? I think it should solve your issue. Without it it’s the root lock file that is being copied and that’s why npm complains about the files not being in sync.

const description = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'lambdas', 'transformer', 'package.json')).toString());
const transformer = new node.NodejsFunction(this, 'Transformer', {
  entry: `${__dirname}/lambdas/transformer/index.js`,
  depsLockFilePath: `${__dirname}/lambdas/transformer/package-lock.json`, // <-- add this line
  runtime: lambda.Runtime.NODEJS_14_X,
  handler: 'handler',
  logRetention: logs.RetentionDays.ONE_MONTH,
  bundling: {
    nodeModules: Object.keys(description.dependencies),
    externalModules: ['aws-sdk']
  }
});
0reactions
github-actions[bot]commented, Jul 9, 2021

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

Read more comments on GitHub >

github_iconTop Results From Across the Web

(@aws-cdk/aws-lambda-nodejs): NodejsFunction ... - GitHub
It says that a dependencies lock file is required and that dependencies will be installed, but with CDK v1.105.0 I'm not seeing that...
Read more >
class NodejsFunction (construct) · AWS CDK
A list of layers to add to the function's execution environment. You can configure your Lambda function to pull in additional code during...
Read more >
Nodejs AWS Lambda Functions dependencies not installing
You need to include the node_modules bundleded with your lambda function. Lambda executes the code straight away without an install step.
Read more >
@aws-cdk/aws-lambda-nodejs | Yarn - Package Manager
The NodejsFunction construct creates a Lambda function with automatic ... packages that contain only the code and dependencies needed to run the function....
Read more >
aws-cdk.aws-lambda-nodejs - PyPI
The NodejsFunction allows you to define your CDK and runtime dependencies in a single ... Function can be used to customize the underlying...
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