Get the preferred Node.js version of a user or project.
This looks for (from highest to lowest priority):
- Any .n-node-version,
.naverc, .node-version, .nodeenvrc .nvmrc or package.json (engines.node field) in the current directory, parent directories, or home directory
- Any
NODEVERSION, NODISTNODEVERSION environment variable
nvm aliases (like current or lts/erbium) and version ranges (like 12 or =12) are resolved to regular "major.minor.patch" version strings.
Examples
import preferredNodeVersion from 'preferred-node-version'
// Look for any `.nvmrc` or `package.json` (`engines.node` field)
const { version } = await preferredNodeVersion()
console.log(version) // 12.10.0// Search only the home directory and environment variables
await preferredNodeVersion({ global: true })// Start looking for a Node.js version file from this directory instead
await preferredNodeVersion({ cwd: '/path/to/cwd' })// Search for specific files
await preferredNodeVersion({
files: ['/path/to/.nvmrc', '/path/to/package.json'],
})Install
npm install preferred-node-versionThis package works in Node.js =18.18.0.
This is an ES module. It must be loaded using an import or import() statement, not require(). If TypeScript is used, it must be configured to output ES modules, not CommonJS.