NPM.Linker (NPM v0.6.0)

Copy Markdown View Source

Creates node_modules from the global cache.

Supports multiple linking strategies:

  • :symlink (default) — symlinks from node_modules/pkg to cache
  • :copy — full file copy

Uses a hoisted layout where packages are placed as high in the tree as possible, only nesting when version conflicts occur.

Summary

Functions

Hoist packages for a flat node_modules layout.

Create node_modules/.bin/ symlinks for packages with bin entries.

Link nested packages into parent package node_modules/ subdirectories.

Remove packages from node_modules that are not in the expected set.

Types

nested_info()

@type nested_info() :: %{required(String.t()) => term()}

resolved()

@type resolved() :: %{required(String.t()) => NPM.Lockfile.entry()}

strategy()

@type strategy() :: :symlink | :copy

Functions

hoist(lockfile)

@spec hoist(resolved()) :: [{String.t(), String.t()}]

Hoist packages for a flat node_modules layout.

Returns a list of {name, version} tuples representing the top-level packages. When multiple versions of a package exist, the most commonly depended-on version gets hoisted.

link(lockfile, node_modules_dir \\ "node_modules", strategy \\ default_strategy())

@spec link(resolved(), String.t(), strategy()) :: :ok | {:error, term()}

Link all resolved packages into node_modules.

First populates the global cache, then creates the node_modules tree.

prune(node_modules_dir, expected_names)

@spec prune(String.t(), MapSet.t()) :: :ok

Remove packages from node_modules that are not in the expected set.

Handles both regular and scoped packages (@scope/pkg).