| 1 |
--- |
| 2 |
title: Node.js dependencies |
| 3 |
description: How to manage Node dependencies in Hugo modules. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
weight: 40 |
| 7 |
--- |
| 8 |
|
| 9 |
Modules that need Node packages (e.g. for Tailwind CSS) can declare those dependencies in a standard `package.json` at the module root. Hugo consolidates dependencies from all modules into an [npm workspace][], so you only need a single `npm install` at the project level. |
| 10 |
|
| 11 |
## Declaring dependencies |
| 12 |
|
| 13 |
Each module declares its Node dependencies in a `package.json` file in its root directory, using the standard `dependencies` and `devDependencies` fields. |
| 14 |
|
| 15 |
<!-- TODO |
| 16 |
In the admonition below, remove the reference to v0.159.0 somewhere |
| 17 |
after v0.174.0, 15 minor releases after the improvement. |
| 18 |
--> |
| 19 |
|
| 20 |
> [!NOTE] |
| 21 |
> We improved this setup greatly in Hugo [v0.159.0][], but we kept the old `package.hugo.json` in the search path. Mostly to preserve as much backward compatibility as possible, but it may also be useful in some situations to reserve a separate set of Node dependencies for Hugo. |
| 22 |
|
| 23 |
## Consolidating dependencies |
| 24 |
|
| 25 |
Run [`hugo mod npm pack`][] to collect Node dependencies from all modules and write them to `packages/hugoautogen/package.json`. Hugo also adds a `workspaces` entry to your project's root `package.json` pointing to this auto-generated package. |
| 26 |
|
| 27 |
The resulting project structure: |
| 28 |
|
| 29 |
```tree |
| 30 |
project/ |
| 31 |
├── package.json # your project's package.json (updated with workspaces entry) |
| 32 |
├── packages/ |
| 33 |
│ └── hugoautogen/ |
| 34 |
│ ├── package.json # auto-generated, contains consolidated module deps |
| 35 |
│ └── hugo_packagemeta.json # metadata and checksums for staleness detection |
| 36 |
└── ... |
| 37 |
``` |
| 38 |
|
| 39 |
<!-- TODO |
| 40 |
In the admonition below, remove the reference to v0.159.0 somewhere |
| 41 |
after v0.174.0, 15 minor releases after the improvement. |
| 42 |
--> |
| 43 |
|
| 44 |
> [!NOTE] |
| 45 |
In Hugo < v0.159.0 Hugo wrote the dependencies into your project's package.json, so if you have used `hugo mod npm pack` on your project using older Hugo versions, now is the time to do a spring cleaning of your project `package.json` file: Only direct Node dependencies needs to live in this file, all incoming dependencies from imported modules gets written to `packages/hugoautogen/package.json`. |
| 46 |
|
| 47 |
When merging, the **topmost version, starting from the project, take precedence**. If a module declares `[email protected]` but your project already has `[email protected]`, the project version wins and the module dependency is excluded from the generated workspace package. |
| 48 |
|
| 49 |
## Staleness detection |
| 50 |
|
| 51 |
When Hugo detects that the npm dependency configuration has changed in one or more of the modules in use, you will get a warning in the console: |
| 52 |
|
| 53 |
```text |
| 54 |
WARN npm dependencies are out of sync, please run "hugo mod npm pack" (you may also want to run "npm install" after that) |
| 55 |
``` |
| 56 |
|
| 57 |
This ensures you don't forget to re-run `hugo mod npm pack` after updating module versions. |
| 58 |
|
| 59 |
[`hugo mod npm pack`]: /commands/hugo_mod_npm_pack/ |
| 60 |
[npm workspace]: https://docs.npmjs.com/cli/using-npm/workspaces |
| 61 |
[v0.159.0]: https://github.com/gohugoio/hugo/releases/tag/v0.159.0 |