Skip to content

Commit

Permalink
Doc/importNpmLock: general improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
hsjobeki committed Sep 6, 2024
1 parent bba8dff commit 9092f5d
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions doc/languages-frameworks/javascript.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,26 +258,38 @@ It returns a derivation with all `package-lock.json` dependencies downloaded int
#### importNpmLock {#javascript-buildNpmPackage-importNpmLock}
The `importNpmLock` function is a simpler and more convenient alternative to `fetchNpmDeps` for managing NPM dependencies in Nix. Unlike `fetchNpmDeps`, there is no need to specify a `hash` when using `importNpmLock`. This function relies entirely on the integrity hashes already present in the `package-lock.json` file.
Here's what `importNpmLock` does:
- It processes the `package.json` and `package-lock.json` files from your NPM project.
- It replaces the dependency references in these files with paths to the Nix store.
- It allows you to customize how each dependency is fetched. See argument: `fetcherOpts`.
`importNpmLock` is a Nix function that requires the following optional arguments:
- `npmRoot`: Path to package directory containing the source tree
- `npmRoot`: Path to package directory containing the source tree. If this is omited `package` and `packageLock` arguments must be specified instead.
- `package`: Parsed contents of `package.json`
- `packageLock`: Parsed contents of `package-lock.json`
- `pname`: Package name
- `version`: Package version
- `fetcherOpts`: An attribute set of arguments forwarded to the underlying fetcher.
It returns a derivation with a patched `package.json` & `package-lock.json` with all dependencies resolved to Nix store paths.
This function is analogous to using `fetchNpmDeps`, but instead of specifying `hash` it uses metadata from `package.json` & `package-lock.json`.
Note that `npmHooks.npmConfigHook` cannot be used with `importNpmLock`. You will instead need to use `importNpmLock.npmConfigHook`:
:::{.note}
`npmHooks.npmConfigHook` cannot be used with `importNpmLock`. Use `importNpmLock.npmConfigHook` instead.
:::
:::{.example}
##### `pkgs.importNpmLock` usage example {#javascript-buildNpmPackage-example}
```nix
{ buildNpmPackage, importNpmLock }:
buildNpmPackage {
pname = "hello";
version = "0.1.0";
src = ./.;
npmDeps = importNpmLock {
npmRoot = ./.;
Expand All @@ -286,6 +298,28 @@ buildNpmPackage {
npmConfigHook = importNpmLock.npmConfigHook;
}
```
:::
:::{.example}
##### `pkgs.importNpmLock` usage example with `fetcherOpts` {#javascript-buildNpmPackage-example-fetcherOpts}
`importNpmLock` uses the following fetchers:
- `pkgs.fetchurl` for `http(s)` dependencies
- `builtins.fetchGit` for `git` dependencies
It is possible to provide additional arguments as needed:
```nix
# ... omited for clarity
npmDeps = importNpmLock {
npmRoot = ./.;
fetcherOpts = {
{ "node_modules/axios" = { curlOptsList = [ "--verbose" ]; }; }
};
}
```
:::
#### importNpmLock.buildNodeModules {#javascript-buildNpmPackage-importNpmLock.buildNodeModules}
Expand Down

0 comments on commit 9092f5d

Please sign in to comment.