Setup a Node.js environment with the right package manager and package caching. 🧰
- name: Set up Node.js
uses: Systemcluster/actions@setup-node-v0
with:
node-version: lts
package-manager: pnpm
install: true
update: true
cache: true
- Installs Node.js detecting the desired version by reading the
NODE_VERSION
environment variable,.nvmrc
and.node-version
files, and theengines.node
field inpackage.json
- Installs the right package manager by checking for existing lock files and the
packageManager
field inpackage.json
- Installs dependencies using the installed package manager
- Caches the package manager store to speed up dependency installation
- Adds problem matchers for
eslint
andtsc
Name | Type | Description | Default | Required |
---|---|---|---|---|
node-version |
String | Version of Node.js to install. Can be a specific version like 18.x , 16.20.x or a version tag like lts or latest . |
Read from the NODE_VERSION environment variable, .nvmrc and .node-version files, the engines.node field in package.json , and falling back to lts . |
✗ |
package-manager |
String | Package manager to install. Can be a name like pnpm or a name and version like npm@9 . |
Read from the packageManager field in package.json if present, detected from a lockfile if available, and falling back to npm . |
✗ |
directory |
String | Directory containing the Node project. Used for detecting node version and package manager configuration. | . |
✗ |
install |
Boolean | Whether to install Node project dependencies. | false |
✗ |
cache |
Boolean | Whether to cache and restore the Node package manager store. | true |
✗ |
cache-key-job |
Boolean | Whether to consider the Job ID when generating the cache key. Set to true to prevent sharing the cache across jobs. Requires cache to be true . |
false |
✗ |
cache-key-env |
String[] | List of environment variables to consider when generating the cache key. Requires cache to be true . |
✗ |
Name | Type | Description |
---|---|---|
node-version |
String | Version of Node.js that was installed. |
package-manager |
String | Name of the package manager that was installed. |
cache-hit |
Boolean | Whether the Node package manager store cache was restored. |
Additionally, the PATH
environment variable is updated to include the Node.js installation directory, and the NODE_VERSION
environment variable is set to the installed Node.js version.
The desired Node.js version is detected in the following order:
- Using the
node-version
input if specified - Read from the
NODE_VERSION
environment variable if present - Read from a
.nvmrc
file if present indirectory
or any parent directory - Read from a
.node-version
file if present indirectory
or any parent directory - Read from the
engines.node
field in if present in thepackage.json
indirectory
or any parent directory - Falling back to
lts
The detected version is then resolved to a specific version using node-version-alias.
The desired package manager is detected in the following order:
- Using the
package-manager
input if specified - Read from the
packageManager
field in thepackage.json
indirectory
if present - Inferred from a lockfile if present in
directory
or any parent directory - Read from the
packageManager
field if present in thepackage.json
in any parent directory - Falling back to
npm
Lockfiles checked for inferring the package manager include package-lock.json
, npm-shrinkwrap.json
, yarn.lock
, and pnpm-lock.yaml
.
When the cache
input is true
, the package manager store is cached and restored using actions/cache.
The package manager store is resolved via the following commands:
npm config get cache
fornpm
pnpm store path
forpnpm
yarn cache dir
foryarn@1
yarn config get cache-folder
foryarn@>1
The cache key is generated based on the following inputs:
- The
dependencies
,devDependencies
andpeerDependencies
fields in thepackage.json
indirectory
and all subdirectories - The package manager-specific lockfiles in
directory
and all subdirectories - The
GITHUB_JOB
environment variable ifcache-key-job
istrue
- The environment variables specified in
cache-key-env
- The
os.platform()
andos.arch()
of the runner
In case of a cache key miss, a cache matching the os.platform()
and os.arch()
of the runner is restored.
The package manager cache is always pruned before the cache is saved.