Search code examples
lerna

Install dependencies using lerna fails


I am trying to add a module as dev-dependency from npm to one of several modules I have in my lerna repo, but when doing that neither is the module added to only one of my modules, nor is it added as a dev-dependency. Instead it is added to all of my local modules and it also is added as a normal dependency.

I have created the following:

mkdir FirstProject
cd FirstProject
yarn init
yarn add lerna --dev
npm run env lerna init
mkdir packages/one
mkdir packages/two
cd packages/one
yarn init
cd ../..
cd packages/two
yarn init
cd ../..

I now have the following tree structure:

myuser@mylaptop:~/FirstProject$ tree -I node_modules
.
├── lerna.json
├── package.json
├── packages
│   ├── one
│   │   └── package.json
│   └── two
│       └── package.json
└── yarn.lock

Both, packages/one/package.json and packages/two/package.json look alike (except the name field):

{
  "name": "one",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT"
}

If I now try to install a new devDependency in packages/one, i.e. angular, I could cd packages/one and then yarn add angular --dev.

But as suggested in the lerna documentation there is a lerna add command that is supposed to do exactly that. The command to do exactly the above is supposed to be the following:

npm run env lerna add angular --scope=one --dev

But, now both packages/one/package.json and packages/two/package.json look again alike (except the name field) and the entry was made as a dependency and not as a devDependency:

{
  "name": "one",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "angular": "^1.6.10"
  }
}

Is this a bug or am I doing something wrong?

EDIT:

Just in case this matters:

  • lerna version 2.11.0
  • npm version 5.8.0
  • yarn version 1.6.0
  • node version v9.11.1
  • Work environment Ubuntu 16.04

Solution

  • You forgot to mention the missing last step of lerna init to create lerna.json :-)

    Anyhow, try with npm>5.2:

    npx lerna add angular --scope=one --dev
    

    npx --help: Execute binaries from npm packages.

    or the manual execution of the binary:

    ./node_modules/.bin/lerna add angular --scope=one --dev
    

    And I do have the resulting:

    {
      "name": "one",
      "version": "1.0.0",
      "main": "index.js",
      "license": "MIT",
      "devDependencies": {
        "angular": "^1.6.10"
      }
    }