node workspaces — require local package
Asked Answered
M

3

12

I am using the workspaces feature of node/npm and the layout is like that:

.
+-- package.json
`-- packages
   `-- p1
      `-- package.json
   `-- p2
      `-- package.json
./package.json

{
  …
  "workspaces": [
    "./packages/*"
  ],
  …
  "dependcies": { … }
  …
}

npm install and everything is working to far. But now I would like to add the package p1 as dependency to package p2. But how do I have to do that? Naively I have tried that:

./packages/p2/package.json

{
  …
  "dependencies": {
    "p1": "*"
  }
  …
}

But that yields an error on install, telling me that p1 cannot be found the registry.

Meteorology answered 11/8, 2021 at 7:20 Comment(0)
M
15

UPDATE - using npm cli
From the project root directory run

npm i ./packages/p1 -w packages/p2

The ./ is a MUST or else npm will confuse the install as a package install from npm registry vs from a local directory.

Manual Workaround
Manually add p1 as a local dependency by providing p1's relative path in p2 package.json, so in your example:

./packages/p2/package.json

{
  …
  "dependencies": {
    "p1": "file:../p1"
  }
  …
}

Then you'll need to run npm install in the p2 package.

I found that solution from a LinkedIn article Things I wish I had known when I started JavaScript monorepo with Lerna.

Mummy answered 12/9, 2021 at 23:20 Comment(1)
Not quite what he wanted. His question is relating to npm workspaces. docs.npmjs.com/cli/v9/using-npm/workspaces?v=trueSmalley
G
4
.
+-- package.json
`-- packages
   `-- p1
      `-- package.json
   `-- p2
      `-- package.json

With NPM workspaces, do not specify your dependency to p1 in ./packages/p2/package.json.

NPM will figure out your local dependencies automatically.

if your specify p1 in your dependencies, it will install an outdated version under your projects P2 node_modules. To fix this when that happens:

  1. Delete that old version of P1 under the node_modules of P2
  2. Remove the P1 dependency in ./packages/p2/package.json.
  3. Rebuild everything.

Wish they explained this better in the docs: docs.npmjs.com/cli/v9/using-npm/workspaces?v=true

Gerrald answered 18/6, 2023 at 13:0 Comment(0)
L
2

That naive approach should work, which makes me suspect that maybe there's something wrong in your setup. Couple of things you can double-check:

  • is the "name" field in ./p1/package.json exactly p1 ? (for example, if you're using scoped names then it might be that you need to use @scopename/p1 in ./p2/package.json "dependencies" instead)
  • do you have the latest version of the npm cli? npm -v (currently v7.23.0)
  • make sure the p1 package is properly symlinked in the ./node_modules folder, e.g: ls -l ./node_modules/p1

But how do I have to do that?

Given that everything is properly setup, you can add packages as dependencies of a workspace (including another workspace) using the -w <workspace-name> argument, e.g: (adding p1 as a dep of p2) npm install p1 -w p2, ref: https://docs.npmjs.com/cli/v7/using-npm/workspaces#adding-dependencies-to-a-workspace

Liana answered 13/9, 2021 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.