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): publishing a construct - package-lock.json dependency workaround

See original GitHub issue

I don’t think this is really a bug or feature request as it relates to NPM behaviour.

I stumbled a bit trying to package up a NodejsFunction as a construct because of these two things in conflict:

  1. https://docs.npmjs.com/cli/v6/configuring-npm/package-lock-json
  • states: "One key detail about package-lock.json is that it cannot be published, and it will be ignored if found in any place other than the toplevel package. It shares a format with npm-shrinkwrap.json, which is essentially the same file, but allows publication. "
  1. aws-lambda-nodejs has a dependency on package-lock.json being present.

For the construct I’m trying to publish, I have the CDK project at the root, and a lambda folder with it’s own src, test folders and package.json.

It’s all kind of awkward, so I thought documenting the best way of packaging this would be helpful. I’m not sure if the way I am doing it is the best way. (Also why this was put here and not stack overflow).

To support testing locally with npm pack as well as npm publish / install this is what I came up with:

cdk package.json snippet:

"scripts": {
   ~
    "prepare_lambda": "cd lambda; npm run prepack",
    "prepack": "npm run prepare_lambda",
    "prebuild": "cd lambda; npm i",  // alternatively move lambda to a sibling of the cdk project to avoid compile failures on a fresh clone.
    "postinstall": "cd lambda; npm i install --production" // generates the package-lock.json  when someone installs the construct
  },
  "files": [
~
    "/lambda/src/**/*.ts",
    "/lambda/*.json",
    "/lambda/node_modules/*"
  ],

lambda package.json snippet:

 "scripts": {
~
    "prepack": "npm i --production" // packages node_modules - but not required if the post install step in the cdk project is kept.
  },
  "files": [
    "node_modules",
    "src",
    "*.json"
  ],

The intent was to not have to ‘npm install’ the lambda - and have it packaged in the constructs NPM package. It would all work but I can’t seem to force packaging of the package-lock.json (other than changing it’s name with a klunkier workflow?).

Is this way simpler than I’m making it? If so - can we document this?


This is a 📕 documentation issue

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
brettswiftcommented, Feb 17, 2021

Ok I see that in the docs now. I must have been stubborn in how I like to keep infra code and app code separate, and didn’t pick up on that initially. For this case I see that.

Thanks for the assist. 😃. I’ll close this up.

0reactions
github-actions[bot]commented, Feb 17, 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

How do I build a Lambda deployment package for Node.js?
Install your dependencies locally to your function project folder by running the following command for each module required for your function:
Read more >
Aws lambda, cant run npm module - node.js - Stack Overflow
I am trying to create a AWS Lambda function to merge pdf files stored in S3, and I am getting the following error:...
Read more >
Reduce your package size when uploading a Lambda function
Basically you have to keep your project clean and tidy and specify exactly what you are "zipping" and uploading. This means making sure...
Read more >
Automating CDK Version Bumping with AWS Serverless and ...
I publish my CDK constructs for specific versions of the AWS CDK. Here's why and how I automated parts of the publishing process....
Read more >
Webpack - Serverless Framework: Plugins
Serverless plugin to bundle your lambdas with Webpack ... transitive dependencies to your package.json , we will use the package-lock.json when possible.
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