0.2.17 • Published 10 months ago

@axetroy/virtual-node-env v0.2.17

Weekly downloads
-
License
Anti-996
Repository
github
Last release
10 months ago

English | 中文简体

Build Status Go Report Card Latest Version 996.icu Repo Size

Background

When developing NodeJS projects, we often need to switch NodeJS versions. For example, project A requires 16.x.y, while project B uses 20.x.y.

However, global version management tools like nvm cannot meet the requirements. It has the following problems:

  1. nvm is not cross-platform, and it is not very convenient to use on Windows.
  2. nvm needs to install the specified version in advance to switch, which is not very friendly to the CI/CD environment.
  3. In a Monorepo, there may be a situation where package A requires 16.x.y, while package B requires 20.x.y. In this case, nvm cannot solve this problem well.

So I developed this tool to solve this problem.

It will run the command with the appropriate NodeJS version according to the NodeJS version constraint in packages.json.

Usage

# Automatically select the NodeJS version to run the command
$ virtual-node-env node -v

# Specify the NodeJS version and run the specified command
$ virtual-node-env use ^18 node -v

Integrate into your NodeJS project

  1. Add NodeJS version constraint in package.json.
+  "engines": {
+    "node": "^20.x.x"
+  },
  "scripts": {
    "dev": "vite dev"
  }
  1. Run the script with virtual-node-env command.
- yarn dev
+ virtual-node-env yarn dev

Run with --help to see more options.

$ virtual-node-env --help
virtual-node-env - A virtual node environment for node.js, node version manager for projects.

USAGE:
  virtual-node-env [OPTIONS] <ARGS...>
  virtual-node-env [OPTIONS] run <ARGS...>
  virtual-node-env [OPTIONS] use <CONSTRAINT> <ARGS...>
  virtual-node-env [OPTIONS] rm <CONSTRAINT> <ARGS...>
  virtual-node-env [OPTIONS] clean
  virtual-node-env [OPTIONS] ls
  virtual-node-env [OPTIONS] ls-remote

COMMANDS:
  run <ARGS...>               Automatically select node version to run commands
  use <CONSTRAINT> <ARGS...>  Use the specified version of node to run the command
  rm|remove <CONSTRAINT>      Remove the specified version of node that installed by virtual-node-env
  clean                       Remove all the node version that installed by virtual-node-env
  ls|list                     List all the installed node version
  ls-remote|list-remote       List all the available node version
  <ARGS...>                   Alias for 'run <ARGS...>' but shorter

OPTIONS:
  --help|-h                   Print help information
  --version|-v                Print version information

ENVIRONMENT VARIABLES:
  NODE_MIRROR                 The mirror of the nodejs download, defaults to: https://nodejs.org/dist/
                              Chinese users defaults to: https://registry.npmmirror.com/-/binary/node/
  NODE_ENV_DIR                The directory where the nodejs is stored, defaults to: $HOME/.virtual-node-env
  DEBUG                       Print debug information when set DEBUG=1

EXAMPLES:
  virtual-node-env node -v
  virtual-node-env run node -v
  virtual-node-env use v14.17.0 node -v

SOURCE CODE:
  https://github.com/axetroy/virtual-node-env

Installation

  1. Install via Cask (Mac/Linux/Windows)
$ cask install github.com/axetroy/virtual-node-env
$ virtual-node-env --help
  1. Install via npm
$ npm install @axetroy/virtual-node-env -g
$ virtual-node-env --help
# or use the alias
$ vnode --help

Uninstall

$ virtual-node-env clean
# then remove the binary file or uninstall via package manager

NodeJS version selection algorithm

This section explains what happens when you run virtual-node-env and how it selects the node version.

  1. Check for the presence of package.json.
  2. If package.json exists:
    1. If the engines.node field is specified, use the indicated version.
      1. If the currently installed version matches engines.node, the command is run using the currently installed version.
      2. Otherwise, select the latest matching version from the remote list, install it, and run the command.
    2. Otherwise, run the command directly.
  3. Otherwise, run the command directly.

Similar Projects

https://github.com/jdx/mise

https://github.com/gvcgo/version-manager

https://github.com/version-fox/vfox

License

The Anti-996 License

0.2.17

10 months ago

0.2.16

10 months ago

0.2.15

10 months ago

0.2.14

10 months ago

0.2.13

10 months ago

0.2.12

10 months ago

0.2.11

10 months ago

0.2.10

10 months ago

0.2.1

10 months ago

0.2.7

10 months ago

0.2.6

10 months ago

0.2.9

10 months ago

0.2.8

10 months ago

0.2.3

10 months ago

0.2.2

10 months ago

0.2.5

10 months ago

0.2.4

10 months ago

0.2.0

10 months ago

0.1.18

10 months ago

0.1.19

10 months ago

0.1.14

1 year ago

0.1.15

1 year ago

0.1.16

1 year ago

0.1.17

1 year ago

0.1.13

1 year ago

0.1.12

1 year ago

0.1.11

1 year ago

0.1.10

1 year ago

0.1.9

1 year ago

0.1.8

1 year ago

0.1.7

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago