Skip to content

Commit 71f01ce

Browse files
Doc/importNpmLock: general improvements (#340019)
Co-authored-by: Valentin Gagarin <[email protected]>
1 parent 086e600 commit 71f01ce

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

doc/languages-frameworks/javascript.section.md

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,34 +258,79 @@ It returns a derivation with all `package-lock.json` dependencies downloaded int
258258

259259
#### importNpmLock {#javascript-buildNpmPackage-importNpmLock}
260260

261-
`importNpmLock` is a Nix function that requires the following optional arguments:
261+
This function replaces the npm dependency references in `package.json` and `package-lock.json` with paths to the Nix store.
262+
How each dependency is fetched can be customized with the `fetcherOpts` argument.
262263

263-
- `npmRoot`: Path to package directory containing the source tree
264+
This is a simpler and more convenient alternative to [`fetchNpmDeps`](#javascript-buildNpmPackage-fetchNpmDeps) for managing npm dependencies in Nixpkgs.
265+
There is no need to specify a `hash`, since it relies entirely on the integrity hashes already present in the `package-lock.json` file.
266+
267+
##### Inputs {#javascript-buildNpmPackage-inputs}
268+
269+
- `npmRoot`: Path to package directory containing the source tree.
270+
If this is omitted, the `package` and `packageLock` arguments must be specified instead.
264271
- `package`: Parsed contents of `package.json`
265272
- `packageLock`: Parsed contents of `package-lock.json`
266273
- `pname`: Package name
267274
- `version`: Package version
275+
- `fetcherOpts`: An attribute set of arguments forwarded to the underlying fetcher.
268276

269277
It returns a derivation with a patched `package.json` & `package-lock.json` with all dependencies resolved to Nix store paths.
270278

271-
This function is analogous to using `fetchNpmDeps`, but instead of specifying `hash` it uses metadata from `package.json` & `package-lock.json`.
279+
:::{.note}
280+
`npmHooks.npmConfigHook` cannot be used with `importNpmLock`.
281+
Use `importNpmLock.npmConfigHook` instead.
282+
:::
283+
284+
:::{.example}
285+
286+
##### `pkgs.importNpmLock` usage example {#javascript-buildNpmPackage-example}
287+
```nix
288+
{ buildNpmPackage, importNpmLock }:
289+
290+
buildNpmPackage {
291+
pname = "hello";
292+
version = "0.1.0";
293+
src = ./.;
294+
295+
npmDeps = importNpmLock {
296+
npmRoot = ./.;
297+
};
272298
273-
Note that `npmHooks.npmConfigHook` cannot be used with `importNpmLock`. You will instead need to use `importNpmLock.npmConfigHook`:
299+
npmConfigHook = importNpmLock.npmConfigHook;
300+
}
301+
```
302+
:::
303+
304+
:::{.example}
305+
##### `pkgs.importNpmLock` usage example with `fetcherOpts` {#javascript-buildNpmPackage-example-fetcherOpts}
306+
307+
`importNpmLock` uses the following fetchers:
308+
309+
- `pkgs.fetchurl` for `http(s)` dependencies
310+
- `builtins.fetchGit` for `git` dependencies
311+
312+
It is possible to provide additional arguments to individual fetchers as needed:
274313

275314
```nix
276315
{ buildNpmPackage, importNpmLock }:
277316
278317
buildNpmPackage {
279318
pname = "hello";
280319
version = "0.1.0";
320+
src = ./.;
281321
282322
npmDeps = importNpmLock {
283323
npmRoot = ./.;
324+
fetcherOpts = {
325+
# Pass 'curlOptsList' to 'pkgs.fetchurl' while fetching 'axios'
326+
{ "node_modules/axios" = { curlOptsList = [ "--verbose" ]; }; }
327+
};
284328
};
285329
286330
npmConfigHook = importNpmLock.npmConfigHook;
287331
}
288332
```
333+
:::
289334

290335
#### importNpmLock.buildNodeModules {#javascript-buildNpmPackage-importNpmLock.buildNodeModules}
291336

0 commit comments

Comments
 (0)