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.

Question: how to generate package-lock.json per-package when switching to npm

See original GitHub issue

We have a Lerna mono-repo in a structure such as:

/
  /packages
    /package1
    /package2
    /package3
    ...

We currently use Yarn, but would like to switch back to npm.

We have a yarn.lock file at the root of our mono-repo, but do not have per-package yarn.lock files since we never enabled Workspaces. When moving to npm, we would like to ensure that we have per-package package-lock.json files, which properly mark devDependencies with "dev": true.

I have not been able to find a way to generate these files.

What I’ve tried:

Attempt: npm run lerna boostrap --hoist Outcome: package-lock.json generated at the top-level only (maybe this is expected due to the --hoist?), but, with no "dev": true tags. Also, it includes no local packages (again, not sure if this is expected or not).

Attempt: cd packages/package1 && npm install Outcome: install fails because npm does not know how to find a local package

Attempt: npm run lerna add express --scope=package1 Note: package1 already has express in its package.json Outcome: package-lock.json files are added for all packages except package1. Further, these files do not mark devDependencies with "dev": true, and, do not include local packages (which I assume they should, but correct me if I’m wrong about that)

I was expecting that when I ran npm run lerna boostrap --hoist it would generate the per-package package-lock.json files for me.

How can I generate these files (correctly), or is the single top-level one generate by the bootstrap command correct? If so, how can I get it to properly tag things with "dev": true, and include local dependencies?

Meta info: Node version: 8.11.1 npm version: 6.1.0 Lerna version: 2.11.0

lerna.json

{
  "lerna": "2.11.0",
  "npmClient": "npm",
  "packages": [
    "packages/*"
  ],
  "version": "0.0.0"
}

The above is also asked in this StackOverflow question.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:22
  • Comments:39 (9 by maintainers)

github_iconTop GitHub Comments

12reactions
Vishesh30commented, Jun 4, 2020

I couldn’t find a concrete solution to generate lock files for all packages. I mean there are ways but, everything is increasing the installing time to very high. Below are 2 ways to generate package-lock.json file for all packages:-

  1. Directly use lerna bootstrap without --hoist flag ------- This does generate lock file but increases the install time way to high.
  2. Use “lerna exec – npm i” ------ This will generate the lock file but “install” times are way higher not a viable solution with 25 packages in my repository.

As above 2 solutions were taking way to much time hence i could not use any of them for my scenario hence, i came up with a third way or i would call it a workaround , this is also not the cleanest solution but does the job with very slight increase in installation time.

  1. Create a npm script in all your packages which would generate only package-lock file without installation which would be something like below:- "genPackagelock": "npm i --package-lock-only"

  2. In you root package.json file as part of postinstall call the above defined script for all the packages as below:- "postinstall": "lerna run --parallel genPackagelock"

The above “postinstall” basically generates package-lock.json file for all the packages along with the internal dependencies.

I am not closing the Bug because the above solutions are a workaround and not one of the best solution.

@tivac - Any thought on above proposal.?

10reactions
jakewhelancommented, Jul 16, 2018

Using lerna@3.0.0-beta.21 package-lock is not generating per (nested) package when using npx lerna bootstrap

Have also tried npx lerna exec -- npm i, and tried using also lerna@2.11.0 and lerna@2.9.0

None of these work.

Lerna project current looks like this:

packages/
  foo/
    bar/
      package.json
    baz/
      package.json

Lerna.json

{
  "lerna": "2.11.0",
  "packages": [
    "packages/foo/*"
  ],
  "version": "independent"
}

If I manually cd to each package and npm i then it works, it goes without saying that this is an undesirable workflow.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to generate per-package package-lock.json for existing ...
The best I've been able to do is to do npm run lerna add somePackage --scope=package1 , which even though somePackage already exists...
Read more >
package-lock.json - npm Docs
Description. package-lock.json is automatically generated for any operations where npm modifies either the node_modules tree, or package.json .
Read more >
What is Package.json & Package-lock.json - YouTube
All FREE courses - https://automationstepbystep.com/Every Node.js project has package. json file located in the root folderInformation about ...
Read more >
How to create package json from package-lock json - Edureka
Run npm init and respond to the questions. The above command will generate a package.json and include the existing packages listed in package- ......
Read more >
How to enforce package-lock.json - Netlify Support Forums
We use npm (not yarn) and we want to make sure our netlify builds install the versions of packages in our package-lock.json file....
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