-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] npm install in a workspace project ignores the workspace #2546
Comments
I would very much not expect it to do the same thing - when cc’d into the package, I’d expect npm install to work the same way it would as if the package was not in a workspaces repo at all. |
In other words, the outer dir is a workspace, the inner dir is not. All repo commands should only be run from a project root. |
Thanks for the clarification. 🙂 The inner dir is part of a workspace. If that changes depending on where I last ran I would expect the file structure to be the source of truth, It's predictable and logical. If I didn't want |
If that were true, what happens if I make |
Yes, but creating a workspace in the root of your file system makes as much sense as running Keeping workspaces contained in respective folders lets projects detect whether they're part of a workspace by letting them traverse down the tree and check for a
|
They'd have to traverse up the tree, which seems like a tricky thing to do. |
Would this fit the bill? const { sep, join } = require('path')
const multimatch = require('multimatch');
console.log(resolveWorkspacePath(__dirname))
/**
* traverses back through the tree and returns the first workspace dir
* that has a matching workspaces glob for this dir
*/
function resolveWorkspacePath(projectDir) {
const dirs = projectDir.split(sep)
while (dirs.length) {
if (isInWorkspaceBlob(projectDir, join(...dirs)))
return join(...dirs)
dirs.pop()
}
}
function isInWorkspaceBlob(projectDir, workspaceDir) {
try {
const { workspaces } = require(join(workspaceDir, 'package.json'))
const globs = workspaces.map(glob => join(workspaceDir, glob))
return multimatch(projectDir, globs).length
} catch { }
} |
I ran into the exact same issue after trying to install a new dependency in a workspace that already depends on another local package. To illustrate the problem using by using @jakobrosenberg's initial scenario, it was after running I created a sample repo to reproduce and isolate the problem before landing on this issue. Sharing it here in the hope that it can be useful to steer the discussion and resolve the problem or at least give better hints/warnings when running https://github.com/raphaelsaunier/npm-install-in-workspace-issue (I tried with both scoped and unscoped modules because at first I wrongly assumed it was related to scoped packages, before noticing that it affected both) I'll let you discuss and figure out what's best, but if I may, here are two additional points to consider in the discussion:
|
I don't understand what is tricky about this. Node and npm traverse up the file system tree regularly for all sorts of common operations. Did I misunderstand the comment? I think the command To install a dependency in a workspace, the developer currently has to manually I'm sure that the current behavior will lead to unexpected behavior and redundant lockfiles, and unless this is changed I will personally avoid using npm workspaces for the projects I maintain. |
I agree. I didn't even know that installing dependencies in a workspace required special syntax. Could explain past issues. |
This also causes some issues with the way Netlify handles mono repo support. You can see the overview I posted there - but making npm 7 context aware would go a long way in making this experience smoother for developers. |
I hit this when installing deps inside my sub-package constantly, just like #2546 (comment). I usually cd directly inside one of the sub-packages so that I can have a smaller file tree, run tests easily, etc. I want npm to be smart enough to handle that I'm inside a sub-package without having to go to the root, do the operation, and return to my sub-package. I can understand there may be some performance/semantics that tricky if we make npm aware of workspaces. Particularly, it'd force a full traversal to the root just to check if there's a workspace definition somewhere above the local When npm finds the local |
I think it would be reasonable to stop upwards traversal as soon as a |
@frangio that assumes one is always present; none of my library packages, including ones in a monorepo, have one. |
Couldn't the upwards traversal stop when it reaches a Similar to how |
@ljharb In that case it would traverse either up to the root or some reasonable configurable default like $HOME. Are you confirming that the concern here is the performance hit of doing this traversal? |
@frangio no, I’m just saying that the presence of a lockfile can’t be relevant. |
I just published a helper binary that will perform workspace-aware operations called @ampproject/npw. Running
That would only work if you were actually inside a workspace, but 99% of packages aren't a workspace. We don't want to force all of those packages to traverse to the root directory just to figure out there's no workspace root. |
Came here to look for or file this same issue. I really want to be able to run workspace-aware commands from anywhere within my monorepo. When I'm working my cwd can easily be in the repo root or in a subpackage and I want Thanks for the npw helper other @jridgewell ! |
How do you handle different version dependencies of packages? main.ts I want to use packages in main, basically just a simlink to the packages. Npm right now uses some fancy module resolving leading to error for my packages as the versions do not match the dependency of the package itself. I really have a hard time modularizing npm packages which I all want to publish but also want to develop without publishing and installing them all the time. Any suggestions on how to do that? Are workspaces even ment for that? |
I expect everyone new to NPM Workspaces breaks their dependencies by doing an |
this is fixed by #4372 which will be part of the next npm release, later this week. |
Is it possible to keep the origin behavior with a flag like Consider the following case where I have a mono repo with few packages Currently to build this package The difficult part would be that if you go to the folder where package So maybe one solution can be install as if there is a monorepo, however, only install all the packages needed by |
Avoiding install of unnecesary packages: npm install -w path/to/C
# or
npm ci -w path/to/C |
How do we This is useful for testing projects that can be used as a template for new standalone projects that are not in a workspace. |
Current Behavior:
If I have
👍
cd workspace && npm install
links localproject1
toproject2
correctly.👎
cd workspace/project2 && npm install
doesn't link localproject1
package.Expected Behavior:
npm install
within a workspace should do the same whether it's in the workspace root or a workspace package.Steps To Reproduce:
npm install [1st project]
from 2nd projectEnvironment:
The text was updated successfully, but these errors were encountered: