Skip to content

Commit 54ef765

Browse files
committed
docs(docs-infra): add npm link documentation for library development
Add comprehensive guide for using npm link with Angular libraries, including required angular.json. fixes angular#61114
1 parent 0f2e6e8 commit 54ef765

1 file changed

Lines changed: 23 additions & 75 deletions

File tree

adev/src/content/tools/libraries/creating-libraries.md

Lines changed: 23 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -242,19 +242,19 @@ TypeScript path mappings should _not_ point to the library source `.ts` files.
242242

243243
## Linking libraries for local development
244244

245-
This approach is useful when:
246-
- You need to test library changes in a consuming application outside the monorepo
247-
- You're working on a standalone library that needs to be tested with external projects
248-
- You want to develop and test without setting up monorepo tooling
245+
This section explains how to use your package manager's local linking feature
246+
(such as [`npm link`](https://pnpm.io/cli/link) or [`pnpm link`](https://pnpm.io/cli/link) to test a standalone Angular library with an external application during
247+
local development, without relying on the monorepo workspace structure or publishing to the NPM registry.
249248

250-
**Note**: If you're working within a monorepo setup, the standard monorepo workflow is generally more efficient as it handles linking automatically and provides better integration.
249+
NOTE: If your library and application are in the same Angular workspace (a monorepo setup), the standard monorepo workflow automatically handles the linking and is generally more efficient. This local linking approach is best when:
250+
- You are developing a standalone library and need to test changes with an external, consuming application.
251+
- You are testing library changes in a consuming application outside the monorepo workspace.
251252

252253
### Configuring the consuming application
253254

254255
To use linked libraries, you need to configure your application's `angular.json` file with the following settings:
255256

256257
```json
257-
258258
{
259259
"projects": {
260260
"your-app": {
@@ -271,87 +271,35 @@ To use linked libraries, you need to configure your application's `angular.json`
271271
"vendor": true
272272
},
273273
"prebundle": {
274-
"exclude": ["@your-scope/*"]
274+
"exclude": [
275+
"@your-scope/*"
276+
]
277+
}
278+
}
279+
}
280+
},
281+
"serve": {
282+
"configurations": {
283+
"development": {
284+
"prebundle": {
285+
"exclude": [
286+
"@your-scope/*"
287+
]
275288
}
276289
}
277290
}
278291
}
279292
}
280293
}
281-
},
282-
"cli": {
283-
"cache": {
284-
"environment": "local"
285-
}
286294
}
287295
}
288296
```
289297

290298
**Configuration options explained:**
291299

292-
- **`preserveSymlinks: true`** - Ensures Angular follows symlinks created by `npm link` instead of resolving to their original locations
293-
- **`sourceMap`** - Enables source maps for easier debugging of linked library code
294-
- **`prebundle.exclude`** - Excludes linked libraries from pre-bundling to ensure changes are detected on each rebuild. Replace `@your-scope/*` with your library scope (e.g., `@mycompany/*`) or specific library names (e.g., `["my-lib-1", "my-lib-2"]`)
295-
296-
### Usage
297-
298-
1. In your library project, create a global link:
299-
```bash
300-
cd /path/to/your-library
301-
npm link
302-
```
303-
304-
2. In your application project, link to the library:
305-
```bash
306-
cd /path/to/your-app
307-
npm link @your-scope/your-library
308-
```
309-
310-
3. Start the dev server in your application:
311-
```bash
312-
ng serve
313-
```
314-
315-
4. When making changes to your library, rebuild it:
316-
```bash
317-
cd /path/to/your-library
318-
npm run build
319-
# Or use watch mode for automatic rebuilds
320-
npm run build -- --watch
321-
```
322-
323-
5. The application will pick up the changes on the next rebuild (dev server will auto-reload if running)
324-
325-
### Troubleshooting
326-
327-
If changes aren't being picked up:
328-
- Ensure the library is built after making changes
329-
- Try clearing the Angular cache: `ng cache clean`
330-
- Verify the symlink exists: `ls -la node_modules/@your-scope/`
331-
- Restart the dev server
332-
333-
**To unlink:**
334-
```bash
335-
cd /path/to/your-app
336-
npm unlink @your-scope/your-library
337-
npm install # Reinstalls the original package
338-
339-
cd /path/to/your-library
340-
npm unlink
341-
```
342-
343-
## Publishing libraries
344-
345-
There are two distribution formats to use when publishing a library:
346-
347-
| Distribution formats | Details |
348-
| :-------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
349-
| Partial-Ivy \(recommended\) | Contains portable code that can be consumed by Ivy applications built with any version of Angular from v12 onwards. |
350-
| Full-Ivy | Contains private Angular Ivy instructions, which are not guaranteed to work across different versions of Angular. This format requires that the library and application are built with the _exact_ same version of Angular. This format is useful for environments where all library and application code is built directly from source. |
351-
352-
For publishing to npm use the partial-Ivy format as it is stable between patch versions of Angular.
353-
354-
Avoid compiling libraries with full-Ivy code if you are publishing to npm because the generated Ivy instructions are not part of Angular's public API, and so might change between patch versions.
300+
- `preserveSymlinks: true`: Instructs the build system to follow the symlinks created by your package manager's linking command instead of resolving to the symlink's original location. This is essential to to avoid multiple copies of the dependent node packages.
301+
- `sourceMap.vendor`: Enabling vendor source maps (especially `vendor: true`) for easier debugging of linked library code.
302+
- `prebundle.exclude`: By default, the Angular CLI can pre-bundle all node dependencies. Excluding your library's scope (e.g., @your-scope/*) ensures that the linked source code is properly watched and rebuilt when changes occur.
355303

356304
## Ensuring library version compatibility
357305

0 commit comments

Comments
 (0)