NPM.PackageJSON (NPM v0.6.0)

Copy Markdown View Source

Read and write package.json files.

Summary

Functions

Add a dependency to package.json, creating the file if needed.

Expand workspace patterns to actual directories with package.json files.

Check if a dependency range refers to a local file path.

Check if a dependency range refers to a git repository.

Read dependencies from package.json.

Read all dependency groups from package.json.

Read bundleDependencies (or bundledDependencies) from package.json.

Read overrides from package.json.

Read resolutions (Yarn-style) from package.json.

Read scripts from package.json.

Read workspace patterns from package.json.

Remove a dependency from package.json.

Resolve a file dependency path.

Check if a dependency range refers to a URL tarball.

Functions

add_dep(name, range, path \\ "package.json", opts \\ [])

@spec add_dep(String.t(), String.t(), String.t(), keyword()) :: :ok | {:error, term()}

Add a dependency to package.json, creating the file if needed.

Options

  • :dev - when true, adds to devDependencies instead of dependencies

expand_workspaces(patterns, base_dir \\ ".")

@spec expand_workspaces([String.t()], String.t()) :: [String.t()]

Expand workspace patterns to actual directories with package.json files.

Supports glob patterns like packages/* and apps/**.

file_dep?(arg1)

@spec file_dep?(String.t()) :: boolean()

Check if a dependency range refers to a local file path.

Supports file:../path and file:./path references.

git_dep?(range)

@spec git_dep?(String.t()) :: boolean()

Check if a dependency range refers to a git repository.

Supports git+https://, git+ssh://, github:user/repo, and git:// URLs.

read(path \\ "package.json")

@spec read(String.t()) ::
  {:ok, %{required(String.t()) => String.t()}} | {:error, term()}

Read dependencies from package.json.

read_all(path \\ "package.json")

@spec read_all(String.t()) ::
  {:ok,
   %{dependencies: map(), dev_dependencies: map(), optional_dependencies: map()}}
  | {:error, term()}

Read all dependency groups from package.json.

read_bundle_deps(path \\ "package.json")

@spec read_bundle_deps(String.t()) :: {:ok, [String.t()]} | {:error, term()}

Read bundleDependencies (or bundledDependencies) from package.json.

Returns a list of package names that should be bundled in the tarball.

read_overrides(path \\ "package.json")

@spec read_overrides(String.t()) ::
  {:ok, %{required(String.t()) => String.t()}} | {:error, term()}

Read overrides from package.json.

read_resolutions(path \\ "package.json")

@spec read_resolutions(String.t()) ::
  {:ok, %{required(String.t()) => String.t()}} | {:error, term()}

Read resolutions (Yarn-style) from package.json.

read_scripts(path \\ "package.json")

@spec read_scripts(String.t()) ::
  {:ok, %{required(String.t()) => String.t()}} | {:error, term()}

Read scripts from package.json.

read_workspaces(path \\ "package.json")

@spec read_workspaces(String.t()) :: {:ok, [String.t()]} | {:error, term()}

Read workspace patterns from package.json.

remove_dep(name, path \\ "package.json")

@spec remove_dep(String.t(), String.t()) :: :ok | {:error, term()}

Remove a dependency from package.json.

resolve_file_dep(arg, base_dir)

@spec resolve_file_dep(String.t(), String.t()) :: String.t()

Resolve a file dependency path.

Returns the absolute path for a file: reference.

url_dep?(url)

@spec url_dep?(String.t()) :: boolean()

Check if a dependency range refers to a URL tarball.

Supports http:// and https:// URLs ending in .tgz or .tar.gz.