@jchip/nvm
Advanced tools
+47
-31
@@ -26,32 +26,36 @@ "use strict"; | ||
| begin, | ||
| `export NVM_HOME="${varNvmHome}"`, | ||
| `NVM_SH="\$\{NVM_HOME}/bin/nvm.sh"`, | ||
| `if [ -s "\$\{NVM_SH}" ]; then`, | ||
| ` export NVM_LINK="${varNvmLink}"`, | ||
| `# Only initialize if not already done`, | ||
| `if ! type _jchip_universal_nvm >/dev/null 2>&1; then`, | ||
| ` export NVM_HOME="${varNvmHome}"`, | ||
| ` export PATH="\$\{NVM_HOME}/bin:\$PATH"`, | ||
| ` NVM_SH="\$\{NVM_HOME}/bin/nvm.sh"`, | ||
| ` if [ -s "\$\{NVM_SH}" ]; then`, | ||
| ` export NVM_LINK="${varNvmLink}"`, | ||
| mirrorEnv, | ||
| ` source "\$\{NVM_SH}"`, | ||
| ` alias nvx="\$\{NVM_HOME}/bin/nvx"`, | ||
| `else`, | ||
| ` unset NVM_HOME`, | ||
| ` NVM_ERROR="\$\{NVM_SH} is not valid"`, | ||
| ` source "\$\{NVM_SH}"`, | ||
| ` alias nvx="\$\{NVM_HOME}/bin/nvx"`, | ||
| ` else`, | ||
| ` unset NVM_HOME`, | ||
| ` NVM_ERROR="\$\{NVM_SH} is not valid"`, | ||
| ` fi`, | ||
| ` unset NVM_SH`, | ||
| `fi`, | ||
| `unset NVM_SH`, | ||
| end | ||
| ].filter(x => x); | ||
| const profileFile = process.argv[2] || path.join(homeDir, ".bash_profile"); | ||
| let profile = fs.existsSync(profileFile) ? fs.readFileSync(profileFile, "utf8").split("\n") : []; | ||
| function updateShellProfile(profileFile) { | ||
| let profile = fs.existsSync(profileFile) ? fs.readFileSync(profileFile, "utf8").split("\n") : []; | ||
| const beginIx = profile.indexOf(begin); | ||
| const beginIx = profile.indexOf(begin); | ||
| let firstPart = profile; | ||
| let secondPart = []; | ||
| let firstPart = profile; | ||
| let secondPart = []; | ||
| if (beginIx >= 0) { | ||
| firstPart = profile.slice(0, beginIx); | ||
| const endIx = profile.indexOf(end); | ||
| if (endIx < beginIx) { | ||
| secondPart = profile.slice(beginIx + 1); | ||
| console.log( | ||
| `WARNING: | ||
| if (beginIx >= 0) { | ||
| firstPart = profile.slice(0, beginIx); | ||
| const endIx = profile.indexOf(end); | ||
| if (endIx < beginIx) { | ||
| secondPart = profile.slice(beginIx + 1); | ||
| console.log( | ||
| `WARNING: | ||
| nvm install found begin marker but not end marker in your ${profileFile} | ||
@@ -62,15 +66,27 @@ please check these markers in the file and clean it up: | ||
| ` | ||
| ); | ||
| } else { | ||
| secondPart = profile.slice(endIx + 1); | ||
| ); | ||
| } else { | ||
| secondPart = profile.slice(endIx + 1); | ||
| } | ||
| } | ||
| let updateProfile = firstPart.concat(commands, secondPart); | ||
| // remove last line if it's empty | ||
| const lastIx = updateProfile.length - 1; | ||
| if (updateProfile[lastIx].trim().length === 0) { | ||
| updateProfile = updateProfile.slice(0, lastIx); | ||
| } | ||
| fs.writeFileSync(profileFile, updateProfile.concat("").join("\n")); | ||
| } | ||
| let updateProfile = firstPart.concat(commands, secondPart); | ||
| // remove last line if it's empty | ||
| const lastIx = updateProfile.length - 1; | ||
| if (updateProfile[lastIx].trim().length === 0) { | ||
| updateProfile = updateProfile.slice(0, lastIx); | ||
| // Export for reuse | ||
| if (typeof module !== "undefined" && module.exports) { | ||
| module.exports = { updateShellProfile, nvmHome, shellName }; | ||
| } | ||
| fs.writeFileSync(profileFile, updateProfile.concat("").join("\n")); | ||
| // Run as script if called directly | ||
| if (require.main === module) { | ||
| const profileFile = process.argv[2] || path.join(homeDir, ".bash_profile"); | ||
| updateShellProfile(profileFile); | ||
| } |
+6
-1
@@ -1,2 +0,2 @@ | ||
| function nvm() { | ||
| function _jchip_universal_nvm() { | ||
| if [ -z "$NVM_HOME" ]; then | ||
@@ -48,2 +48,7 @@ NVM_HOME=~/nvm | ||
| # Create nvm function that calls the internal function | ||
| function nvm() { | ||
| _jchip_universal_nvm "$@" | ||
| } | ||
| # If a version is linked, then automatically add it to PATH | ||
@@ -50,0 +55,0 @@ |
+1
-1
@@ -6,3 +6,3 @@ param ( | ||
| $nvmVersion = "1.8.0" | ||
| $nvmVersion = "1.8.1" | ||
| $nvmVersionV = "v$nvmVersion" | ||
@@ -9,0 +9,0 @@ $DisableInstallNvmFromTgz = $false |
+17
-3
@@ -0,3 +1,4 @@ | ||
| #!/usr/bin/env bash | ||
| NVM_VERSION="1.8.0" | ||
| NVM_VERSION="1.8.1" | ||
| NVM_VERSION_V="v${NVM_VERSION}" | ||
@@ -25,3 +26,3 @@ | ||
| curl=$(which curl) | ||
| if [ "$?" == "0" ]; then | ||
| if [ "$?" = "0" ]; then | ||
| curl --fail -L $1 -o $2 | ||
@@ -32,3 +33,3 @@ return $? | ||
| wget=$(which wget) | ||
| if [ "$?" == "0" ]; then | ||
| if [ "$?" = "0" ]; then | ||
| wget "$1" --output-document="$2" | ||
@@ -192,2 +193,3 @@ return $? | ||
| ZSH_RC="${ZD}/.zshrc" | ||
| ZSH_ENV="${ZD}/.zshenv" | ||
@@ -198,2 +200,9 @@ if [ ! -f "${ZSH_RC}" ]; then | ||
| if [ ! -f "${ZSH_ENV}" ]; then | ||
| touch "${ZSH_ENV}" | ||
| fi | ||
| # Add to both .zshenv (for non-interactive shells) and .zshrc (for interactive shells) | ||
| # The guard in the script prevents double initialization | ||
| ${NVM_NODE_BIN} ${NVM_HOME}/bin/install_bashrc.js "${ZSH_ENV}" zsh | ||
| ${NVM_NODE_BIN} ${NVM_HOME}/bin/install_bashrc.js "${ZSH_RC}" zsh | ||
@@ -207,1 +216,6 @@ } | ||
| # Run nvx --install-to-user to set up LaunchAgent on macOS | ||
| if [ -f "${NVM_HOME}/bin/nvx" ]; then | ||
| "${NVM_HOME}/bin/nvx" --install-to-user 2>/dev/null || true | ||
| fi | ||
+1
-1
| { | ||
| "name": "@jchip/nvm", | ||
| "description": "nvm: a universal node.js version manager for Windows (no admin) and Unix", | ||
| "version": "1.8.0", | ||
| "version": "1.8.1", | ||
| "author": "Tom.Huang <hzlhu.dargon@gmail.com>", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
+100
-12
@@ -29,2 +29,5 @@ # @jchip/nvm | ||
| - [Installing from jsdelivr.net](#installing-from-jsdelivrnet-1) | ||
| - [Shell Initialization on Unix](#shell-initialization-on-unix) | ||
| - [Zsh (macOS default)](#zsh-macos-default) | ||
| - [Bash](#bash) | ||
| - [Usage](#usage) | ||
@@ -38,2 +41,3 @@ - [Environments](#environments) | ||
| - [Install to System PATH (all users)](#install-to-system-path-all-users) | ||
| - [How it Works](#how-it-works) | ||
| - [Contributing and Release](#contributing-and-release) | ||
@@ -68,3 +72,3 @@ - [Development](#development) | ||
| cd $Env:USERPROFILE; | ||
| Invoke-WebRequest https://raw.githubusercontent.com/jchip/nvm/v1.8.0/install.ps1 -OutFile install.ps1; | ||
| Invoke-WebRequest https://raw.githubusercontent.com/jchip/nvm/v1.8.1/install.ps1 -OutFile install.ps1; | ||
| .\install.ps1 -nvmhome $Env:USERPROFILE\nvm; | ||
@@ -80,3 +84,3 @@ del install.ps1 | ||
| cd $Env:USERPROFILE; | ||
| Invoke-WebRequest https://unpkg.com/@jchip/nvm@1.8.0/install.ps1 -OutFile install.ps1; | ||
| Invoke-WebRequest https://unpkg.com/@jchip/nvm@1.8.1/install.ps1 -OutFile install.ps1; | ||
| .\install.ps1 -nvmhome $Env:USERPROFILE\nvm; | ||
@@ -92,3 +96,3 @@ del install.ps1 | ||
| cd $Env:USERPROFILE; | ||
| Invoke-WebRequest https://cdn.jsdelivr.net/npm/@jchip/nvm@1.8.0/install.ps1 -OutFile install.ps1; | ||
| Invoke-WebRequest https://cdn.jsdelivr.net/npm/@jchip/nvm@1.8.1/install.ps1 -OutFile install.ps1; | ||
| .\install.ps1 -nvmhome $Env:USERPROFILE\nvm; | ||
@@ -138,4 +142,4 @@ del install.ps1 | ||
| 1. Download the package zip file from https://github.com/jchip/nvm/archive/v1.8.0/.zip | ||
| 1. Extract this file to your home directory. You will get a new directory `nvm-1.8.0`. | ||
| 1. Download the package zip file from https://github.com/jchip/nvm/archive/v1.8.1/.zip | ||
| 1. Extract this file to your home directory. You will get a new directory `nvm-1.8.1`. | ||
| 2. Rename it to `nvm`, for example: `C:\Users\<username>\nvm` | ||
@@ -171,3 +175,3 @@ 2. Download the zipfile https://nodejs.org/dist/v20.12.1/node-v20.12.1-win-x64.zip | ||
| ```bash | ||
| NVM_HOME=~/nvm curl -o- https://raw.githubusercontent.com/jchip/nvm/v1.8.0/install.sh | bash | ||
| export NVM_HOME=~/nvm; curl -o- https://raw.githubusercontent.com/jchip/nvm/v1.8.1/install.sh | bash | ||
| ``` | ||
@@ -178,3 +182,3 @@ | ||
| ```bash | ||
| NVM_HOME=~/nvm wget -qO- https://raw.githubusercontent.com/jchip/nvm/v1.8.0/install.sh | bash | ||
| export NVM_HOME=~/nvm; wget -qO- https://raw.githubusercontent.com/jchip/nvm/v1.8.1/install.sh | bash | ||
| ``` | ||
@@ -189,3 +193,3 @@ | ||
| ```bash | ||
| NVM_HOME=~/nvm curl -o- https://unpkg.com/@jchip/nvm@1.8.0/install.sh | bash | ||
| export NVM_HOME=~/nvm; curl -o- https://unpkg.com/@jchip/nvm@1.8.1/install.sh | bash | ||
| ``` | ||
@@ -196,3 +200,3 @@ | ||
| ```bash | ||
| NVM_HOME=~/nvm wget -qO- https://unpkg.com/@jchip/nvm@1.8.0/install.sh | bash | ||
| export NVM_HOME=~/nvm; wget -qO- https://unpkg.com/@jchip/nvm@1.8.1/install.sh | bash | ||
| ``` | ||
@@ -207,3 +211,3 @@ | ||
| ```bash | ||
| NVM_HOME=~/nvm curl -o- https://cdn.jsdelivr.net/npm/@jchip/nvm@1.8.0/install.sh | bash | ||
| export NVM_HOME=~/nvm; curl -o- https://cdn.jsdelivr.net/npm/@jchip/nvm@1.8.1/install.sh | bash | ||
| ``` | ||
@@ -214,5 +218,59 @@ | ||
| ```bash | ||
| NVM_HOME=~/nvm wget -qO- https://cdn.jsdelivr.net/npm/@jchip/nvm@1.8.0/install.sh | bash | ||
| export NVM_HOME=~/nvm; wget -qO- https://cdn.jsdelivr.net/npm/@jchip/nvm@1.8.1/install.sh | bash | ||
| ``` | ||
| ### Shell Initialization on Unix | ||
| The nvm installation automatically updates your shell profile files to initialize nvm. The behavior differs between zsh and bash: | ||
| #### Zsh (macOS default) | ||
| For zsh users, the installer updates both `.zshenv` and `.zshrc`: | ||
| - **`.zshenv`**: Sourced for ALL shells (interactive and non-interactive) | ||
| - **`.zshrc`**: Sourced for interactive shells only | ||
| This means: | ||
| - ✅ Terminal sessions have nvm available | ||
| - ✅ Non-interactive scripts have nvm available | ||
| - ✅ GUI applications (like VS Code) have nvm available (when combined with `nvx --install-to-user`) | ||
| #### Bash | ||
| For bash users, the installer updates `.bashrc` or `.bash_profile`: | ||
| - **`.bash_profile`**: Sourced for login shells (macOS default) | ||
| - **`.bashrc`**: Sourced for interactive non-login shells (Linux default) | ||
| - **Non-interactive shells**: NOT sourced by default | ||
| This means: | ||
| - ✅ Terminal sessions have nvm available | ||
| - ❌ Non-interactive bash scripts do NOT have nvm by default | ||
| **Using nvm in bash scripts:** | ||
| If you need nvm in a bash script, you have three options: | ||
| 1. **Source the profile explicitly** in your script: | ||
| ```bash | ||
| #!/bin/bash | ||
| source ~/.bashrc | ||
| node --version | ||
| ``` | ||
| 2. **Use a login shell** with the shebang: | ||
| ```bash | ||
| #!/bin/bash -l | ||
| node --version | ||
| ``` | ||
| 3. **Set BASH_ENV** to automatically source a file for non-interactive shells: | ||
| ```bash | ||
| export BASH_ENV=~/.bashrc | ||
| ``` | ||
| You can add this to your `.bash_profile` to make it permanent, but be aware this will affect all bash scripts system-wide. | ||
| **Note:** This is standard bash behavior by design - non-interactive shells have a minimal environment for performance and predictability. | ||
| ## Usage | ||
@@ -339,4 +397,34 @@ | ||
| **Note for Windows users:** On Windows, the nvm bin directory is automatically added to your PATH during installation, so these commands are not needed. | ||
| **Note for Windows users:** On Windows, the installation script automatically adds both `%NVM_HOME%\bin` and `%NVM_LINK%` to your user PATH in the Windows registry. This makes nvm, node, and npm immediately available to all applications (terminal, GUI apps like VS Code, etc.) without needing to run any additional commands. The `nvx --install-to-user` and `nvx --install-to-system` commands are therefore not needed on Windows. | ||
| #### How it Works | ||
| The `nvx --install-to-user` command uses platform-specific mechanisms to make nvm available to GUI applications: | ||
| **macOS:** | ||
| Creates a LaunchAgent at `~/Library/LaunchAgents/com.jchip.universal-nvm.plist` that runs at login to set environment variables for the user session. This makes nvm available to: | ||
| - All GUI applications (VS Code, editors, etc.) | ||
| - Terminal windows | ||
| - Background processes | ||
| The LaunchAgent adds both paths to your environment: | ||
| - `~/nvm/bin` - nvm commands (nvm, nvx) | ||
| - `~/nvm/nodejs/bin` - Node.js executables (node, npm) - only available after running `nvm link <version>` | ||
| **Linux:** | ||
| Creates a systemd user environment file at `~/.config/environment.d/10-jchip-universal-nvm.conf`. This file is read by systemd-based desktop environments at login and makes nvm available to: | ||
| - All GUI applications launched from the desktop | ||
| - Applications started by systemd user services | ||
| - Any process in your user session | ||
| The environment file adds both paths: | ||
| - `~/nvm/bin` - nvm commands (nvm, nvx) | ||
| - `~/nvm/nodejs/bin` - Node.js executables (node, npm) - only available after running `nvm link <version>` | ||
| **Important:** The `~/nvm/nodejs/bin` directory is a symlink created by `nvm link <version>` that points to the default Node.js version. You must run `nvm link` to set up a default Node.js version before GUI applications can access node and npm. The install script does this automatically. | ||
| **Note:** On Linux systems not using systemd, this feature may not work. In that case, you can manually add the paths to your `~/.profile` or consult your distribution's documentation for setting user environment variables. | ||
| ## Contributing and Release | ||
@@ -343,0 +431,0 @@ |
Sorry, the diff of this file is not supported yet
AI-detected potential code anomaly
Supply chain riskAI has identified unusual behaviors that may pose a security risk.
Found 1 instance in 1 package
1470331
0.47%1310
1.08%454
24.04%57
1.79%