To install dependencies for one package in an npm workspace project on macOS, run npm install --workspace="packages/web" from the project root, replacing packages/web with the workspace’s path or its package name. You can also change into that workspace directory and run npm install. These commands select a workspace in npm’s project; they do not create a separate installation directory or guarantee that only that package’s files will change.
Check that the project is an npm workspace
An npm workspace is a package directory listed in the root package.json under workspaces. For example, a repository might contain a root package.json and a packages/web/package.json, with "packages/*" in the root’s workspace list. npm uses that configuration to recognize and link the local packages. If the folder is not configured as a workspace, --workspace cannot target it as one. See npm’s workspaces documentation.
In Terminal, go to the repository root. On macOS, quote paths that contain spaces so the shell treats the path as one argument:
cd "$HOME/Projects/sample monorepo"
npm install --workspace="packages/web"
Here, packages/web is an example path relative to the repository root. You can use a configured workspace’s package name instead, such as npm install --workspace="@example/web". npm documents workspace names, workspace directory paths, and parent workspace paths as valid selectors. Prefer the specific package path or name: selecting a parent workspace directory can select every workspace beneath it. npm’s npm install options describe the selector.
Install the selected workspace’s declared dependencies
The command above runs the install in the context of the selected workspace. With no package name following install, npm installs dependencies already declared in the project manifests; it does not add a new dependency to packages/web/package.json. npm’s workspace configuration defaults to excluding the root project when an individual workspace is selected, unless include-workspace-root is enabled. The npm configuration reference documents both options.
For a package you want to add, include its name before the workspace selector:
npm install date-fns --workspace="packages/web"
This asks npm to add date-fns to that workspace’s dependencies and install it. Check the workspace’s package.json afterward to confirm the dependency was recorded where intended. npm’s workspaces guide gives the same pattern for adding a dependency to a named workspace.
Use the workspace directory instead
If you are already working in the package folder, you can run npm install there rather than specifying --workspace:
cd "$HOME/Projects/sample monorepo/packages/web"
npm install
npm documents that running a command from within a workspace implicitly sets the workspace context and uses the workspace root as the project prefix. This can be convenient in a terminal tab dedicated to one package. If you are unsure which package the current directory belongs to, use the explicit --workspace command from the known repository root.
Know what “one workspace” does and does not mean
Selecting one workspace limits the command’s workspace context; it does not promise a physically separate node_modules tree. npm workspaces are managed from a top-level project, and npm can link local workspaces into the root node_modules directory. Dependencies may be hoisted or shared where the dependency tree allows it. The install may therefore affect the repository’s shared installation state and lockfile. Review the resulting changes in version control, especially if you expected a package-only filesystem change. npm’s install documentation explains its dependency-tree behavior and workspace linking.
If your goal is a completely standalone install isolated from the monorepo, a workspace selector is not that mechanism. A workspace is part of the configured root project; targeting it does not turn it into an independent repository or install tree.
If npm does not recognize the target
First verify that the path is correct relative to the repository root and that its package.json exists. Then inspect the root package.json and confirm its workspaces patterns include that folder. If using a name, confirm it matches the workspace package’s name field. Run the command from the repository root, or change directly into the intended workspace directory. Avoid --workspaces when you want one package: the plural option selects all configured workspaces. The practical rule is simple: use --workspace="path" from the root when you want an explicit target, and verify the manifest and repository changes after npm finishes.
Sources
- npm-install — npm Docs
- Workspaces — npm Docs
- Config — npm Docs