nve

Run any command on specific Node.js versions

README

nve logo

Run any command on specific Node.js versions.

Unlike [nvm exec](https://github.com/nvm-sh/nvm/blob/master/README.md#usage)
it:

- can run multiple Node.js versions at once
- can be run programmatically
- does not need a separate installation step for each Node version
- can run the major release's latest minor/patch version automatically
- works on Windows. No need to run as Administrator.
- does not require Bash
- is installed as a Node module (as opposed to a
  downloaded with curl)

nve 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](https://github.com/nvm-sh/nvm),
[nvm-windows](https://github.com/coreybutler/nvm-windows),
[n](https://github.com/tj/n) or [nvs](https://github.com/jasongin/nvs)
instead.

Examples


  1. ```bash
  2. # Same as `node` but with Node 12
  3. $ nve 12 node
  4. Welcome to Node.js v12.22.12.
  5. Type ".help" for more information.
  6. > .exit

  7. # Same as `node file.js` but with Node 8
  8. $ nve 8 node file.js

  9. # Any command can be used
  10. $ nve 12 npm test

  11. # Execute a local binary
  12. $ nve 8 ava

  13. # Run a specific version
  14. $ nve 8.10.0 npm test

  15. # Use a version range
  16. $ nve "<8" npm test

  17. # Run the latest Node.js version
  18. $ nve latest npm test

  19. # Run the latest LTS version
  20. $ nve lts npm test

  21. # Run the Node version from `~/.nvmrc` or the current process version
  22. $ nve global npm test

  23. # Run the current directory's Node.js version using its `.nvmrc` or `package.json` (`engines.node` field)
  24. $ nve local npm test

  25. # Run the Node version using a file like `.nvmrc` or `package.json`
  26. $ nve /path/to/.nvmrc npm test

  27. # Use a different mirror for the Node binaries
  28. $ nve --mirror=https://npmmirror.com/mirrors/node 8 npm test

  29. # Do not use the cached list of available Node.js versions
  30. $ nve --fetch 8 npm test

  31. # Always use the cached list of available Node.js versions even if it's more
  32. # than one hour old
  33. $ nve --no-fetch 8 npm test

  34. # Use a different CPU architecture for the Node binaries
  35. $ nve --arch=x32 8 npm test

  36. # Chaining commands
  37. $ nve 8 npm run build && nve 8 npm test

  38. # Cache Node 8 download
  39. $ nve 8 node --version
  40. ```

Examples (multiple versions)


  1. ```bash
  2. # Run multiple versions
  3. $ nve 12,10,8 npm test

  4.   Node 12.22.12

  5.   105 tests passed
  6.   Finished 'test' after 3.8 s

  7.   Node 10.24.1

  8.   105 tests passed
  9.   Finished 'test' after 4.2 s

  10.   Node 8.17.0

  11.   105 tests passed
  12.   Finished 'test' after 4.5 s

  13. # Do not abort on the first version that fails
  14. $ nve --continue 12,10,8 npm test

  15. # Run all versions in parallel
  16. $ nve --parallel 12,10,8 npm test

  17. # Cache multiple Node downloads
  18. $ nve 12,10,8 node --version
  19. ```

Examples (list versions)


  1. ```bash
  2. # Prints latest Node.js version
  3. $ nve latest
  4. 20.4.0

  5. # Prints latest Node.js 8 version
  6. $ nve 8
  7. 8.17.0

  8. # Prints latest Node.js 12, 10 and 8 versions
  9. $ nve 12,10,8
  10. 12.22.12
  11. 10.24.1
  12. 8.17.0
  13. ```

Demo


You can try this library:

- either directly in your browser.
- or by executing the [examples files](examples/README.md) in a terminal.

Install


  1. ```bash
  2. npm install -g nve
  3. ```

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

To use this programmatically (from Node.js) instead, please check
[nvexeca](https://github.com/ehmicky/nvexeca).

Usage


  1. ```bash
  2. nve [OPTIONS...] VERSION,... [COMMAND] [ARGS...]
  3. ```

This is exactly the same as:

  1. ```bash
  2. COMMAND [ARGS...]
  3. ```

But using a specific Node VERSION. Several comma-separated VERSION can be
specified at once.

VERSION can be:

- any version range such as12, 12.6.0
  or <12
- latest: Latest available Node version
- lts: Latest LTS Node version
- global: Global Node version
  - Using the home directory [.nvmrc](https://github.com/nvm-sh/nvm#nvmrc) or
    [package.json (engines.node field)](https://docs.npmjs.com/files/package.json#engines)
    used by other Node.js version managers are also searched for
  - If nothing is found, defaults to the current process's Node version
- local: Current directory's Node version
  - Using the current directory or parent directories
    [.nvmrc](https://github.com/nvm-sh/nvm#nvmrc),
    [package.json (engines.node field)](https://docs.npmjs.com/files/package.json#engines)
    or
  - Defaults to the global version
- a file path towards a [.nvmrc](https://github.com/nvm-sh/nvm#nvmrc),
  [package.json (engines.node field)](https://docs.npmjs.com/files/package.json#engines)
  or

COMMAND must be compatible with the specific Node VERSION. For example npm
is [only compatible with Node >=6](https://github.com/npm/cli#important).

Both global and local binaries can be executed.

Options


--continue


_Alias_: -c\
_Type_: boolean\
_Default_: false

By default, when running multiple Node versions and one of those versions fails,
the others are aborted. This option disables this.

--parallel


_Alias_: -p\
_Type_: boolean\
_Default_: false

When running multiple Node versions, run all of them at the same time. This is
faster. However this does not work if the command:

- requires some interactive CLI input (for example using a prompt)
- is not concurrency-safe

--progress


_Type_: boolean\
_Default_: true

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

--mirror


_Alias_: -m\
_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


_Alias_: -f\
_Type_: boolean\
_Default_: undefined

The list of available Node.js versions is cached for one hour by default. With:

- --fetch: the cache will not be used
- --no-fetch: the cache will be used even if it's older than one hour

The default value is undefined (neither of the above). When no COMMAND is
specified (only printing the Node.js version), the default value is --fetch
instead.

--arch


_Alias_: -a\
_Type_: string\
_Default_:
[process.arch](https://nodejs.org/api/process.html#process_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](https://nodejs.org/api/process.html#process_process_arch) are
allowed except mips and mipsel.

Initial download


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

COMMAND can be omitted in order to cache that initial download without
executing any commands.

Difference with nvm


nve is meant for one-off command execution. Examples include:

- running tests with an older Node.js version
- checking if an older Node.js version supports a specific syntax or feature
- benchmarking different Node.js versions

Tools like [nvm](https://github.com/nvm-sh/nvm),
[nvm-windows](https://github.com/coreybutler/nvm-windows),
[n](https://github.com/tj/n) or [nvs](https://github.com/jasongin/nvs) are
meant to execute a specific Node.js version for an entire machine, project or
shell session.

nve can (and probably should) be used alongside those tools.

Native modules


If your code is using native modules, nve 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.

Benchmarks


The following benchmarks compare the average time to run
nve, [nvm exec](https://github.com/nvm-sh/nvm) and
[npx node](https://github.com/aredridel/node-bin-gen/blob/master/node-bin-README.md#use-with-npx):

  1. ```
  2. nve:      261ms
  3. nvm exec: 619ms
  4. npx node: 994ms
  5. ```

See also


- [nvexeca](https://github.com/ehmicky/nvexeca): Like nve but programmatic
  (from Node.js)
- [execa](https://github.com/sindresorhus/execa): Process execution for humans
- [get-node](https://github.com/ehmicky/get-node): Download Node.js
- [preferred-node-version](https://github.com/ehmicky/preferred-node-version):
  Get the preferred Node.js version of a project or user
- [node-version-alias](https://github.com/ehmicky/node-version-alias): Resolve
  Node.js version aliases like latest, lts or erbium
- [normalize-node-version](https://github.com/ehmicky/normalize-node-version):
  Normalize and validate Node.js versions
- [all-node-versions](https://github.com/ehmicky/all-node-versions): List all
  available Node.js versions
- [fetch-node-website](https://github.com/ehmicky/fetch-node-website): Fetch
  releases on nodejs.org
- [global-cache-dir](https://github.com/ehmicky/global-cache-dir): Get the
  global cache directory

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!

Thanks go to our wonderful contributors:

ehmicky
ehmicky

💻 🎨 🤔 📖
Scott Warren
Scott Warren

💬
Charlike Mike Reagent
Charlike Mike Reagent

💬 🤔
Hongarc
Hongarc

🤔
Pedro Augusto de Paula Barbosa
Pedro Augusto de Paula Barbosa

🐛
Adrien Becchis
Adrien Becchis

💻 ⚠️ 🤔
Eric Cornelissen
Eric Cornelissen

🐛 🤔