Giter Site home page Giter Site logo

nve's Introduction

nve logo

Node Codecov Mastodon Medium

Run any command on specific Node.js versions.

Unlike nvm exec it:

  • can run multiple Node.js versions at once
  • can be run programmatically
  • is much faster
  • 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 Bash installation script 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, nvm-windows, n or nvs instead.

Hire me

Please reach out if you're looking for a Node.js API or CLI engineer (11 years of experience). Most recently I have been Netlify Build's and Netlify Plugins' technical lead for 2.5 years. I am available for full-time remote positions.

Examples

# Same as `node` but with Node 12
$ nve 12 node
Welcome to Node.js v12.22.12.
Type ".help" for more information.
> .exit

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

# Any command can be used
$ nve 12 npm test

# Execute a local binary
$ nve 8 ava

# Run a specific version
$ nve 8.10.0 npm test

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

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

# Run the latest LTS version
$ nve lts npm test

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

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

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

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

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

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

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

# Chaining commands
$ nve 8 npm run build && nve 8 npm test

# Cache Node 8 download
$ nve 8 node --version

Examples (multiple versions)

# Run multiple versions
$ nve 12,10,8 npm test

 ⬢  Node 12.22.12

  105 tests passed
  Finished 'test' after 3.8 s

 ⬢  Node 10.24.1

  105 tests passed
  Finished 'test' after 4.2 s

 ⬢  Node 8.17.0

  105 tests passed
  Finished 'test' after 4.5 s

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

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

# Cache multiple Node downloads
$ nve 12,10,8 node --version

Examples (list versions)

# Prints latest Node.js version
$ nve latest
20.4.0

# Prints latest Node.js 8 version
$ nve 8
8.17.0

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

Demo

You can try this library:

Install

npm install -g nve

node >=18.18.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.

Usage

nve [OPTIONS...] VERSION,... [COMMAND] [ARGS...]

This is exactly the same as:

COMMAND [ARGS...]

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

VERSION can be:

COMMAND must be compatible with the specific Node VERSION. For example npm is only compatible with Node >=6.

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

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.

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 almost instantaneous.

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
  • programmatic usage or child processes

Tools like nvm, nvm-windows, n or 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 and npx node:

nve:       295ms
nvm exec:  741ms
npx node: 1058ms

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!

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

🐛 🤔

nve's People

Contributors

allcontributors[bot] avatar dependabot[bot] avatar ehmicky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nve's Issues

Silent failure when 1-n Node.js versions don't have a binary for a given OS+architecture

Guidelines

  • Please search other issues to make sure this bug has not already been reported.
  • If this is related to a typo or the documentation being unclear, please click on the relevant page's Edit button (pencil icon) and suggest a correction instead.

Describe the bug

The CLI will fail without an error message if at least one of multiple of the specified Node.js version don't have a binary available for the current platform. Example:

$ nve 10.13.0,12,14,16,18,19,20 some-command

  ⬢  Node.js 10.13.0  ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  ⬢  Node.js 14.21.3  ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

$ echo $?
1

Steps to reproduce

  1. Figure out your platform (OS + architecture)
  2. Find a Node.js release that isn't available on your platform
  3. Try to run nve with that release and at least one other release

Configuration

  • Command: nve 10.13.0,12,14,16,18,19,20 some-command
  • No configuration file

Environment

npx envinfo --system --binaries --browsers --npmPackages nve --npmGlobalPackages nve

  System:
    OS: macOS 13.4
    CPU: (10) arm64 Apple M1 Pro
    Memory: 5.12 GB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.0.0 - ~/.nvm/versions/node/v20.0.0/bin/node
    npm: 9.6.4 - ~/.nvm/versions/node/v20.0.0/bin/npm
  Browsers:
    Chrome: 113.0.5672.126
    Firefox: 113.0.2
    Safari: 16.5
  npmPackages:
    nve: 16.0.0 => 16.0.0 

Pull request (optional)

  • I can submit a pull request.

This does not work with global binaries on Windows

When running global binaries on Windows, the system node is used instead of the one specified by nve:

$ nve 8 myGlobalBinary

This is because global binaries on Windows are triggered through a shim file (available both as .cmd file or as a Bash script). That shim file is in the same directory as the system node and it explicitly uses it:

@ECHO off
SETLOCAL
CALL :find_dp0

IF EXIST "%dp0%\node.exe" (
  SET "_prog=%dp0%\node.exe"
) ELSE (
  SET "_prog=node"
  SET PATHEXT=%PATHEXT:;.JS;=;%
)

"%_prog%"  "%dp0%\node_modules\myGlobalBinary\build\src\bin.js" %*
ENDLOCAL
EXIT /b %errorlevel%
:find_dp0
SET dp0=%~dp0
EXIT /b

In the code above, %dp0%\node.exe explicitly refer to the node.exe in the same directory as the global binary. This bypasses nve which relies on the PATH environment variable.

Question about releases like 4.0.1

Hello 😬 sorry for not following the issue template; this is just a question. I watch this repository and noticed that there are a few releases (bumping the patch version) to which your release notes is simply 'internal changes'. What is the point of these releases? (This is not criticism, just a question). When such a release is made, why should a consumer update? How to decide between updating / not updating? From a consumer point of view, nothing changed, right? Thanks for the attention :)

Run all versions specified in `engines.node`

Guidelines

  • Please search other issues to make sure this feature has not already been requested.

Which problem is this feature request solving?

I started using nve to run tests and all Node.js version supported by a library I maintain. The library specifies the supported Node.js versions in package.json as:

"engines" {
  "node": "^10.13.0 || ^12 || ^14 || ^16 || ^18 || ^19 || ^20"
},

and I would like nve to run against each of these versions when using nve local. Instead, as of v16.0.0, nve local node --version would run only on Node.js 20.X.Y.

Describe the solution you'd like

For my particular use case running each version separated by a || would suffice.

However, I'm not sure that solution works well in the generalized case (for all valid values of engines.node). For example, if "node": "^18 || ^20" runs on both Node.js v18 and v20, then what should "node": ">=18" do? Run only Node.js v20, or also v18 (and v19)?

Pull request (optional)

  • I can submit a pull request.

Add option to explicitly choose `.nvmrc` or `engines.node`

Guidelines

  • Please search other issues to make sure this feature has not already been requested.

Which problem is this feature request solving?

Currently, the local option works as:

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

which, based on testing, in practice first looks for .nvmrc and uses that (even if package.json#engines.node is present) and then looks at package.json#engines.node to choose a Node.js version.

In library projects I use both of these, but for different purposes. package.json#engines.node is there to tell users of the library which versions of Node.js the library promises to work in. .nvmrc is used to specify the version of Node.js that should be used during development (to make sure all development dependencies work).

With nve local I can't choose which one I want to use (plus it's not clear which one will be used). But I would like to use something like nve local to avoid repeating what's in (in my particular case) package.json#engines.node.

Describe the solution you'd like

Either a flag or new subcommand to be able to explicitly select the local source to use for the Node.js version.

Some ideas:

# Using a flag with "local"
$ nve local --source .nvmrc npm test
$ nve local --source package.json npm test

# Using a new subcommand
$ nve nvmrc npm test
$ nve package.json npm test
$ nve engines npm test

I'd personally deprecate nve local since it's ambiguous, but that decision up to the maintainers of the project 🙂

Pull request (optional)

  • I can submit a pull request.

Show progress bar downloading

In first time download new node.js version, it is so tedious, just black screen.

Let user know that nve is downloading node.js(I thought I type wrong command)

Suggestions for "See also" section from similar project: @bevry/testen

Guidelines

  • Please search other issues to make sure this feature has not already been requested.

Which problem is this feature request solving?

I've been maintaining a similar tool https://github.com/bevry/testen since 2018, so maybe you'd find some of its dependencies and related projects useful:

Describe the solution you'd like

Feel free to add whatever you think are useful to the see also section. I'll do the same for testen. I think there's enough room in the universe for both of them.

Pull request (optional)

  • I can submit a pull request.

npm lifecycle warning to use --scripts-prepend-node-path

Hello, first of all thanks for all the work on this :) I have one silly question, sorry to open an issue. This is my setup:

// run.js
console.log('process.version', process.version);
$ nve 8 npm start
npm WARN lifecycle The node binary used for scripts is C:\Users\me\AppData\Local\nve\Cache\8.16.2\node.exe but npm is using C:\Program Files\nodejs\node.exe itself. Use the `--scripts-prepend-node-path` option to include the path for the node binary npm was executed with.

> [email protected] start C:\Users\me\Desktop\testing_nve
> node run.js

process.version v8.16.2

What does the warning shown above by npm really mean?

npm WARN lifecycle The node binary used for scripts is C:\Users\me\AppData\Local\nve\Cache\8.16.2\node.exe but npm is using C:\Program Files\nodejs\node.exe itself. Use the `--scripts-prepend-node-path` option to include the path for the node binary npm was executed with.

Thanks!

Self-contained CLI (package it, no need to have node installed)

Because, yes, you usually will have node installed anyway, but I think it will be useful to be able to don't depend on having nodejs. For example, you can package it using pkg or ncc or something like them.

So in environments that can't/don't have nodejs, they can just download the nve and use it.

Allow running binaries

Global and Local binaries could not previously be used. This was reported by @luxwarp.

This has been fixed by the major release 3.0.0. For example nve 8 npm test will work.

Please note that the binary must support the specific Node.js version. In particular npm only supports Node >=6.

My console text turned green

Describe the bug

After using nve on windows' command prompt, my console text turned green:

image

Steps to reproduce

I am not sure if this is easy to reproduce, but as shown in the screenshot above I just ran nve --arch=x32 12 node.

Expected behavior

nve should not alter the text color 😅

Environment

  • OS: Windows 7 6.1.7601
  • Node: 12.14.1 - C:\NodeJS\node.EXE
  • npm: 6.13.6 - C:\NodeJS\npm.CMD

Can you submit a pull request?

No... No time to investigate, sorry...

Node version aliases interpeted as command

  • Bug Description: non whitelisted aliases are interpreted as command

  • Steps to reproduce

    1. install nve@12
    2. run nve 12 default node --version
  • Expected behavior: node version being outpted for node 12, and nvm default node version

  • Current behavior: Crash interpreting default as the command:

     $ nve 10 default node --version
     spawn default ENOENT
    
  • Configuration: none specific

  • Environment:

    System:
      OS: macOS 10.15.4
      CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
      Memory: 3.38 GB / 16.00 GB
      Shell: 5.7.1 - /bin/zsh
    Binaries:
      Node: 10.20.1 - ~/.nvm/versions/node/v10.20.1/bin/node
      Yarn: 1.19.1 - ~/.nvm/versions/node/v8.16.2/bin/yarn
      npm: 6.14.4 - ~/.nvm/versions/node/v10.20.1/bin/npm
    Browsers:
      Chrome: 81.0.4044.113
      Safari: 13.1
    npmGlobalPackages:
      nve: 12.0.0
     
  • Can you submit a pull request?: Yes, but it's better to agree what is best strategy to address the issue.

  • Possible options to solve this problem (probably non exhaustive):

    1. prefix the aliases with some special character, so that they are not interpreted as commands.
      • : could do the trick
      • example: nve 12 :default node --version
    2. use an optional separator to separate versions from commands, something similar to -- with flags
      • : could work too
      • example: nve 12 default : node --version
    3. checking if nve arg is resolvable to a node version or if it's a bash available command

I would rather go for option 1 or 2. And possibly both as the two command syntax are not incompatible.
Tell me what you think about it @ehmicky :)

Incorrect behaving?

It probably can be fixed with #11.

Describe the bug

Not sure exactly if it's a bug. I'm trying to emulate that I'm using nve mainly (like after fresh system install), instead of NVM, to test what some paths will be.

Steps to reproduce

I'm on node 12.13 through nvm. I installed, for example pnpm, globally through nve 10 'npm i -g pnpm' and expect to be able to run pnpm through that nve node 10, like nve 10 'pnpm bin --global'. Instead it says that the pnpm command is not found.

❯ nve --shell 10 'npm install --global pnpm'
/home/charlike/.cache/nve/bin/pnpm -> /home/charlike/.cache/nve/lib/node_modules/pnpm/bin/pnpm.js
/home/charlike/.cache/nve/bin/pnpx -> /home/charlike/.cache/nve/lib/node_modules/pnpm/bin/pnpx.js
+ [email protected]
updated 1 package in 2.595s

❯ nve --shell 10 'pnpm bin --global'        
/bin/sh: pnpm: command not found

~/github/tunnckoCore/hela next*
❯ nve --shell 10 'npm bin --global' 
/home/charlike/.cache/nve/bin
(not in PATH env variable)

If I have pnpm installed normally (through nvm, eg. nvm's node/npm) it works, meaning it nve --shell 10 'pnpm bin --global' shows the nvm place of pnpm which is /home/charlike/.nvm/versions/node/v12.13.0/bin

Expected behavior

I expect what's installed through nve to be able to be called through nve, and exclusively.

Current behaving means that if i'm on a fresh machine and just use nve (and put in my shell) as my main daily driver instead of nvm it probably will show/do wrong things.

Environment

Enter the following command in a terminal and copy/paste its output:

Node: 12.13.0 - ~/.nvm/versions/node/v12.13.0/bin/node
Yarn: 1.17.3 - ~/.nvm/versions/node/v12.13.0/bin/yarn
npm: 6.13.0 - ~/.nvm/versions/node/v12.13.0/bin/npm
nve: 7.2.0

Changes in the directory structure of NVE dir(s)

Which problem is this feature request solving?

Doesn't work with global-dirs and probably other such libraries.

Describe the solution you'd like

Currently the node bin is the nve/<VERSION>/node, while it's supposed to be inside a /bin directory inside the version. E.g. nve/12.13.0/bin/node.

Currently, with NVM

{
  npm: {
    prefix: '/home/charlike/.nvm/versions/node/v12.13.0',
    packages: '/home/charlike/.nvm/versions/node/v12.13.0/lib/node_modules',
    binaries: '/home/charlike/.nvm/versions/node/v12.13.0/bin'
  }
}

Currently, with NVE

{
  npm: {
    prefix: '/home/charlike/.cache/nve',
    packages: '/home/charlike/.cache/nve/lib/node_modules',
    binaries: '/home/charlike/.cache/nve/bin'
  }
}

The problem comes because global-dirs is doing double path.dirname on process.execPath which for NVE is /home/charlike/.cache/nve/12.13.0/node and that's why we are getting /home/charlike/.cache/nve

Describe alternatives you've considered

Alternative would be to go to global-dirs and suggest addition of one if there. But I don't think that is the correct way and place to fix this. Instead, this module should adjust to the convention.

Can you submit a pull request?

Yes, probably.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.