Skip to content

Commit b3c1c4f

Browse files
committed
docs(adev): add npm link documentation for library development
Add comprehensive guide for using npm link with Angular libraries, including required angular.json and tsconfig configurations for preserveSymlinks, source maps, and cache settings. Fixes angular#61114
1 parent 07b8e95 commit b3c1c4f

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

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

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,147 @@ This means that the TypeScript source can result in different JavaScript code in
240240
For this reason, an application that depends on a library should only use TypeScript path mappings that point to the _built library_.
241241
TypeScript path mappings should _not_ point to the library source `.ts` files.
242242

243+
## Linking libraries for local development
244+
245+
When developing multiple libraries or testing library changes in a consuming application, you can use `npm link` to create symbolic links between your library and application. This approach provides several benefits over a monorepo setup:
246+
247+
- Auto-reload when library code changes
248+
- Selective linking of only the libraries you're actively developing
249+
- Independence from monorepo tooling and configuration
250+
- Familiar workflow consistent with other npm-based projects
251+
252+
### Configuring the consuming application
253+
254+
To use linked libraries, you need to configure your application's `angular.json` file with the following settings:
255+
256+
<docs-code language="json">
257+
258+
{
259+
"projects": {
260+
"your-app": {
261+
"architect": {
262+
"build": {
263+
"options": {
264+
"preserveSymlinks": true
265+
},
266+
"configurations": {
267+
"development": {
268+
"sourceMap": {
269+
"scripts": true,
270+
"styles": true,
271+
"vendor": true
272+
}
273+
}
274+
}
275+
}
276+
}
277+
}
278+
},
279+
"cli": {
280+
"cache": {
281+
"environment": "local",
282+
"enabled": false
283+
}
284+
}
285+
}
286+
287+
</docs-code>
288+
289+
| Configuration | Purpose |
290+
|:---|:---|
291+
| `preserveSymlinks: true` | Prevents Node.js from resolving symlinks to their real paths, which is necessary for `npm link` to work correctly |
292+
| `sourceMap` | Enables source maps for debugging both your application and linked library code |
293+
| `cache.enabled: false` | Disables the Angular CLI disk cache to ensure library changes are always reflected |
294+
295+
For more information about cache options, see [Angular CLI configuration options](reference/configs/workspace-config#cache-options).
296+
297+
### Configuring the library
298+
299+
In your library's `tsconfig.lib.json`, enable declaration maps to support debugging:
300+
301+
<docs-code language="json">
302+
303+
{
304+
"extends": "./tsconfig.json",
305+
"compilerOptions": {
306+
"declarationMap": true
307+
}
308+
}
309+
310+
</docs-code>
311+
312+
The `declarationMap` option generates source map files for TypeScript declaration files, allowing your IDE to navigate to the original TypeScript source when debugging.
313+
314+
### Linking workflow
315+
316+
<docs-workflow>
317+
318+
<docs-step title="Build the library with watch mode">
319+
In your library project, build the library with the `--watch` flag to enable incremental rebuilds:
320+
321+
<docs-code language="shell">
322+
323+
ng build my-lib --watch
324+
325+
</docs-code>
326+
327+
This keeps the library build running and automatically rebuilds when you make changes to the library source code.
328+
</docs-step>
329+
330+
<docs-step title="Create the npm link">
331+
In a separate terminal, navigate to the library's distribution folder and create the global link:
332+
333+
<docs-code language="shell">
334+
335+
cd dist/my-lib
336+
npm link
337+
338+
</docs-code>
339+
340+
This creates a global symbolic link to your library package.
341+
</docs-step>
342+
343+
<docs-step title="Link in the consuming application">
344+
In your application project, link to the library:
345+
346+
<docs-code language="shell">
347+
348+
cd path/to/your-app
349+
npm link my-lib
350+
351+
</docs-code>
352+
353+
This creates a symbolic link from your application's `node_modules/my-lib` to the globally linked library.
354+
</docs-step>
355+
356+
<docs-step title="Serve the application">
357+
Start your application's development server:
358+
359+
<docs-code language="shell">
360+
361+
ng serve
362+
363+
</docs-code>
364+
365+
Changes to your library code will now trigger rebuilds, and the application will automatically reload with the updated library code.
366+
</docs-step>
367+
368+
</docs-workflow>
369+
370+
<docs-callout type="important" title="Cache considerations">
371+
372+
When using `npm link`, you must disable the Angular CLI cache by setting `"cli.cache.enabled": false` in your `angular.json`. Otherwise, cached builds may prevent library changes from being reflected in your application.
373+
374+
For more details, see [Cache options](reference/configs/workspace-config#cache-options).
375+
376+
</docs-callout>
377+
378+
<docs-callout type="helpful" title="Unlinking libraries">
379+
380+
To remove a linked library, use `npm unlink my-lib` in your application directory, then reinstall the library from npm with `npm install my-lib`.
381+
382+
</docs-callout>
383+
243384
## Publishing libraries
244385

245386
There are two distribution formats to use when publishing a library:

0 commit comments

Comments
 (0)