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.

Lerna bootstrap does not link local dependencies

See original GitHub issue

I have configured two modules in a mono repo to use lerna with a local dependency. I expected that

$ lerna bootstrap
$ lerna run test

would be sufficient to download all external dependencies, link a local dependency and execute and pass all tests in all modules.

Expected Behavior

As per the lerna bootstrap documentation:

  1. Symlink together all Lerna packages that are dependencies of each other.

Thus, I expected that lerna bootstrap would create a symlink in module-b/node_modules below that points to module-a (which then would allow the tests to be executed and pass).

Current Behavior

No linking occurs, which cause the tests to fail:

lerna ERR! yarn run test exited 1 in ‘module-b’ lerna ERR! yarn run test stdout: yarn run v1.19.1 $ jest info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

lerna ERR! yarn run test stderr: FAIL ./import.test.js ● Test suite failed to run

Cannot find module 'module-a' from 'import.test.js'

> 1 | const moduleA = require('module-a');
    | ^
  2 | 
  3 | test('should import module-a', () => {
  4 |   moduleA();

  at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:259:17)
  at Object.<anonymous> (import.test.js:1:1)

Steps to Reproduce

  1. Create the folder structure below
  2. Copy the files inlined to their corresponding directory
  3. Execute $ lerna bootstrap followed by $ lerna run test
project-root
   + packages
   |       + module-a
   |       |      + package.json
   |       |      + index.js
   |       |
   |       + module-b
   |       |      + package.json
   |       |      + import.test.js
   |
   +  lerna.json     
packages/module-a/package.json

{
  "name": "module-a",
  "version": "1.0.0",
  "private": true,
  "main": "index.js",
  "scripts": {
    "test": "echo \"Test passed in module-a\""
  }
}

packages/module-a/index.js

module.exports = () => console.log('Log in module-a');

packages/module-b/package.json

{
  "name": "module-b",
  "version": "1.0.0",
  "private": true,
  "main": "index.js",
  "scripts": {
    "test": "jest"
  },
  "dependencies": {
    "module-a": "file:../module-a"
  },
  "devDependencies": {
    "jest": "^24.9.0"
  }
}

packages/module-b/import.test.js

const moduleA = require('module-a');

test('should import module-a', () => {
  moduleA();
});

lerna.json

{
  "npmClient": "yarn",
  "packages": [
    "packages/*"
  ],
  "version": "independent"
}

Observation

Executing lerna link --force-local does not change status quo, the test still fails because module-b/node_modules/ still does not contain a reference to module-a.

Context

I would like to have a build script that executes all tests in all modules as well as having a simple instruction for new project members so that they can be productive in our multi-module project.

I cannot use yarn workspaces because module-b is an Electron app and the electron builder expects its dependencies to be installed in the packages/module-b/node_modules/ folder.

Your Environment

Executable Version
lerna --version 3.18.4
npm --version 6.11.3
yarn --version 1.19.1
node --version v12.12.0
OS Version
NAME VERSION
macOS Mojave 10.14.6

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:76
  • Comments:33

github_iconTop GitHub Comments

18reactions
MrDevinBcommented, Dec 4, 2019

I am also experiencing this issue. Even when using file: ../module-a to symlink packages together.

10reactions
Domiiicommented, Jul 20, 2020

I also thought that lerna bootstrap --force-local takes care of everything (at least it promises that in the docs), however some local packages are linked, but most are not. I don’t know what the pattern is either.

Luckily, this workaround at least added the local links (however, it did not link all hoisted dependencies, only some; again, seemingly at random):

lerna bootstrap --force-local && lerna link --force-local

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lerna bootstrap does not link local dependencies?
Another work-around is to manually "hoist" your intra-package dependencies by adding them to the root package.json per lerna issue #2352.
Read more >
lerna/bootstrap - npm Package Health Analysis
Bootstrap the packages in the current Lerna repo. Installs all of their dependencies and links any cross-dependencies. When run, this command will: npm...
Read more >
Setting up a monorepo with Lerna for a TypeScript project
As Lerna is intended to be used with Node.js, not TypeScript, ... lerna bootstrap installs and links all dependencies, both local and remote ......
Read more >
Lerna Mono Repos with Internal Dependencies
With lerna, this problem is solved elegantly. In a nutshell, it does not download the internal dependency into the (sub-)package's node_modules ...
Read more >
Can't add local package in a monorepo lerna project
Perhaps it is trying to search it in npm repository? I also tried to change the build command to “npx lerna add tsdx-test...
Read more >

github_iconTop Related Medium Post

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