Detalhes do pacote

nvexeca

ehmicky30.7kApache-2.011.0.4

nvm + execa = nvexeca

nodejs, node, nvm, npx

readme (leia-me)

<picture> <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/ehmicky/design/main/nve/nve_dark.svg"/> nvexeca logo </picture>

Node TypeScript Codecov Mastodon Medium

nvm + execa = nvexeca.

Execa improves child processes execution with a promise interface, cross-platform support, local binaries, interleaved output, and more.

nvexeca is a thin wrapper around Execa to run any file or command using any Node.js version.

Unlike nvm exec it:

  • is run programmatically
  • does not need a separate installation step for each Node version
  • can run the major release's latest minor/patch version automatically
  • does not require Bash
  • is installed as a Node module
  • works on Windows. No need to run as Administrator.

nvexeca executes a single file or command. It does not change the node nor npm global binaries. To run a specific Node.js version for an entire project or shell session, please use nvm, nvm-windows, n or nvs instead.

Example

import nvexeca from 'nvexeca'

const { childProcess, versionRange, version } = await nvexeca('8', 'node', [
  '--version',
])
console.log(`Node ${versionRange} (${version})`) // Node 8 (8.16.2)
const { exitCode, stdout, stderr } = await childProcess
console.log(`Exit code: ${exitCode}`) // 0
console.log(stdout) // v8.16.2

Install

npm install nvexeca

node >=18.18.0 must be installed. However the command run by nvexeca can use any Node version (providing it is compatible with it).

This package is an ES module and 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.

To use this as a CLI instead, please check nve.

Usage

nvexeca(versionRange, command, args?, options?)

Executes command ...args with a specific Node.js versionRange.

Arguments

versionRange

Type: string

This can be:

command

Type: string

File or command to execute. Both global and local binaries can be executed.

Must be compatible with the specific Node versionRange. For example npm is only compatible with Node >=6.

args

Type: string[]?

Arguments to pass to the command.

Options

Type: object?

All Execa options are available. Please refer to Execa for the list of possible options.

The preferLocal option is always true.

The following options are also available.

dry

Type: boolean\ Default: false

Do not execute the command. This can be used to cache the initial Node.js binary download.

progress

Type: boolean\ Default: false

Whether to show a progress bar when the Node binary is downloading.

mirror

Type: string\ Default: https://nodejs.org/dist

Base URL to retrieve Node binaries. Can be overridden (for example https://npmmirror.com/mirrors/node).

The following environment variables can also be used: NODE_MIRROR, NVM_NODEJS_ORG_MIRROR, N_NODE_MIRROR or NODIST_NODE_MIRROR.

fetch

Type: boolean\ Default: undefined

The list of available Node.js versions is cached for one hour by default. If the fetch option is:

  • true: the cache will not be used
  • false: the cache will be used even if it's older than one hour

arch

Type: string\ Default: process.arch

Node.js binary's CPU architecture. This is useful for example when you're on x64 but would like to run Node.js x32.

All the values from process.arch are allowed except mips and mipsel.

cwd

Type: string | URL\ Default: process.cwd()

Current working directory of the child process.

When using the local alias, this also starts looking for a Node.js version file from this directory.

Return value

Type: Promise<object>

Promise resolved after the Node.js version has been cached locally (if it has not been cached yet).

If you want to wait for the command to complete as well, you should await the returned childProcess.

const { childProcess } = await nvexeca('8', 'node', ['--version'])
const { exitCode, stdout, stderr } = await childProcess

childProcess

Type: ResultPromise?

childProcess instance. It is also a Promise resolving or rejecting with a Result. The Promise should be awaited if you want to wait for the process to complete.

This is undefined when the dry option is true.

versionRange

Type: string

Node.js version passed as input, such as "v10".

version

Type: string

Normalized Node.js version. For example if "v10" was passed as input, version will be "10.17.0".

command

Type: string

File or command that was executed.

args

Type: string[]

Arguments that were passed to the command.

execaOptions

Type: object

Options that were passed to Execa.

Initial download

The first time nvexeca is run with a new VERSION, the Node binary is downloaded under the hood. This initially takes few seconds. However subsequent runs are almost instantaneous.

Native modules

If your code is using native modules, nvexeca works providing:

  • they are built with N-API
  • the target Node.js version is >=8.12.0 (since N-API was not available or stable before that)

Otherwise the following error message is shown: Error: The module was compiled against a different Node.js version.

See also

Support

For any question, don't hesitate to submit an issue on GitHub.

Everyone is welcome regardless of personal background. We enforce a Code of conduct in order to promote a positive and inclusive environment.

Contributing

This project was made with ❤️. The simplest way to give back is by starring and sharing it online.

If the documentation is unclear or has a typo, please click on the page's Edit button (pencil icon) and suggest a correction.

If you would like to help us fix a bug or add a new feature, please check our guidelines. Pull requests are welcome!


ehmicky

💻 🎨 🤔 📖

Nicolas Goudry

📖

Pedro Augusto de Paula Barbosa

💬

changelog (log de mudanças)

11.0.4

Dependencies

  • Upgrade tar-fs dependency

11.0.3

Documentation

  • Improve documentation in README.md

11.0.2

Security fix

  • Fix vulnerability in tar-fs dependency by upgrading it

11.0.1

Bug fixes

  • Fix EMFILE errors thrown on Windows when nvexeca() is called multiple times at once.

11.0.0

Breaking changes

10.0.0

Breaking changes

  • Minimal supported Node.js version is now 18.18.0

9.2.1

Dependencies

9.2.0

Features

const { childProcess, version } = await nvexeca('/path/to/.nvmrc', 'node', [
  '--version',
])

9.1.1

Bug fixes

9.1.0

Features

  • Make the signal option also cancel downloading the Node.js binary

9.0.0

Breaking changes

  • Minimal supported Node.js version is now 16.17.0

8.5.0

Features

  • Upgrade Execa

8.4.0

Features

  • Improve tree-shaking support

8.3.0

Features

  • Reduce npm package size by 47%

8.2.0

Features

  • Reduce npm package size

8.1.0

Features

  • Add TypeScript types

8.0.0

Breaking changes

  • Minimal supported Node.js version is now 14.18.0

Features

7.0.0

Breaking changes

  • Minimal supported Node.js version is now 12.20.0
  • This package is now an ES module. It can only be loaded with an import or import() statement, not require(). See this post for more information.

Bug fixes

  • Fix support for the latest version of npm

6.0.1

Bug fixes

  • Fix crash when downloading several Node.js binaries in parallel

6.0.0

Breaking changes

Features

  • Add the alias global to target the global Node version, regardless of the current directory

5.0.0

Breaking changes

4.0.0

Breaking changes

  • Aliases c and current renamed to now
  • The alias now now takes into account package.json engines.node field and additional files used by other Node.js version managers.
  • Alias l removed: please use latest instead

Features

  • Added alias lts to target the latest LTS version

3.0.1

Bug fixes

  • Fix aliases shortcuts l and c

3.0.0

Breaking changes

2.3.0

Features

Bug fixes

  • Checksum checks were not working when the mirror option was used

2.2.0

Features

2.1.3

Bug fixes

  • Fix crash when using multiple drives on Windows

2.1.2

Bug fixes

  • Fix terminal color changing on Windows

2.1.1

Bug fixes

2.1.0

Features

2.0.0

Breaking changes

  • Minimal supported Node.js version is now 10.17.0

1.7.0

Features

  • Node.js binary download is now 50% faster on Windows

Bug fixes

  • Fix crash when Node.js binary URL is invalid

1.6.0

Features

  • Node.js binary download is now twice faster on Windows

Bug fixes

  • Fix ARM, PowerPC, S390 support

1.5.5

Bug fixes

  • Fix running npm or npx binaries on Windows

1.5.4

Bug fixes

1.5.3

Bug fixes

  • Fix executing binaries by specifying their file paths on Windows

1.5.2

Bug fixes

  • Fix executing yarn.

1.5.1

Bug fixes

  • Executing npm, yarn and pnpm was not working properly, for example when doing global installs (npm i -g ...).

1.5.0

Features

  • Improve the internal directory structure used to cache the Node.js binary
  • Cleanup temporary files when Node.js download fails

1.4.0

Features

  • Improve the appearance of the progress bar

1.3.0

Features

  • Ensure Node.js binaries are not corrupted by checking their checksums
  • Use cache when offline (no network connection)

1.2.0

Features

  • Make Node.js binary download twice faster on Linux and MacOS
  • Improve error messages

1.1.2

Dependencies

  • Reduce the number of dependencies

1.1.1

Dependencies

  • Reduce the number of dependencies

1.1.0

Features

  • Upgrade get-node to 5.5.0