You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Fixesangular#61114
Copy file name to clipboardExpand all lines: adev/src/content/tools/libraries/creating-libraries.md
+141Lines changed: 141 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -240,6 +240,147 @@ This means that the TypeScript source can result in different JavaScript code in
240
240
For this reason, an application that depends on a library should only use TypeScript path mappings that point to the _built library_.
241
241
TypeScript path mappings should _not_ point to the library source `.ts` files.
242
242
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.
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).
0 commit comments