The Problem
When developing multiple npm packages together (e.g., main app + shared lib), the normal publish workflow is slow:
- Fix bug in lib
- Publish lib to npm
- Update main's dependency version
- Test main
You want: change lib code → main sees it immediately.
Solution 1: npm Workspaces (Recommended for Monorepo)
Best when both packages live in the same repository.
Directory Structure
workspace/
package.json # Root package.json
packages/
lib-project/
package.json
src/...
main-project/
package.json
src/...
Root package.json
{
"private": true,
"workspaces": ["packages/*"]
}
main-project/package.json
{
"name": "@you/main-project",
"dependencies": {
"@you/lib-project": "workspace:*"
}
}
workspace:* means: use local workspace version during development.
Setup
cd workspace
npm install
npm automatically symlinks lib-project into main-project/node_modules.
Now edit packages/lib-project → main-project sees changes instantly.
Publishing
When publishing, workspace:* gets replaced with actual version number automatically (npm 7+).
Solution 2: npm link (Separate Repos)
When packages are in different repositories.
Step 1: Link the lib globally
cd /path/to/lib-project
npm link
This creates a global symlink to lib-project.
Step 2: Link into main
cd /path/to/main-project
npm link @you/lib-project
Now main-project/node_modules/@you/lib-project points to your local source.
Unlink when done
cd /path/to/main-project
npm unlink @you/lib-project
npm install # Restore from registry
⚠️ Warning:
npm linkcan cause issues with peer dependencies and multiple React instances. Workspaces are more reliable.
Solution 3: file: Protocol (Quick & Dirty)
For temporary local testing:
{
"dependencies": {
"@you/lib-project": "file:../lib-project"
}
}
⚠️ Warning: Never publish with
file:dependencies. Others can't install your package.
Solution 4: Git URL (No npm Release)
For quick fixes before official release:
{
"dependencies": {
"@you/lib-project": "github:you/lib-project#fix-branch"
}
}
Or pin to commit:
{
"dependencies": {
"@you/lib-project": "github:you/lib-project#a1b2c3d"
}
}
Works for CI/team without publishing.
Keep Production Dependencies Clean
Your published package.json should use version dependencies:
{
"dependencies": {
"@you/lib-project": "^1.2.0"
}
}
pnpm / yarn Equivalents
pnpm workspaces
pnpm-workspace.yaml:
packages:
- 'packages/*'
Dependency:
"@you/lib-project": "workspace:*"
yarn workspaces
Same as npm workspaces syntax in package.json.
Linking
| Tool | Link Command |
|---|---|
| npm | npm link |
| pnpm | pnpm link --global |
| yarn | yarn link |
Common Pitfalls
Pitfall 1: Changes not reflected
Cause: Symlink not created, or wrong version in node_modules.
Fix:
ls -la node_modules/@you/lib-project
# Should show symlink arrow (->)
# If not, re-run npm link or npm install (for workspaces)
Pitfall 2: Multiple React instances
Cause: Both main and lib have React in dependencies.
Fix: Move React to peerDependencies in lib:
{
"peerDependencies": {
"react": "^18.0.0"
}
}
Pitfall 3: CI fails but local works
Cause: CI doesn't have your local symlinks.
Fix: CI must use published versions. For workspaces, npm handles this automatically when publishing.
Comparison
| Method | Use Case | Pros | Cons |
|---|---|---|---|
| Workspaces | Monorepo | Automatic, reliable | Requires same repo |
| npm link | Separate repos | Works anywhere | Can break peer deps |
| file: | Quick test | Simple | Can't publish |
| git URL | Pre-release fix | CI works | Slower, needs network |
Recommended Setup
For most teams:
- Monorepo with workspaces - if packages are closely related
- Separate repos + npm link - for occasional cross-repo debugging
- Git URL - for sharing pre-release fixes with team/CI