Why does my Node.js version mismatch when using nvm in a workspace?
Last updated: August 11, 2025
Context
When setting up a workspace that uses Node Version Manager (nvm) for Node.js version management, you may encounter issues where the Node.js version does not match the expected version, particularly when running commands like `yarn install`. This can occur due to PATH environment variable conflicts between nvm and other package managers like Linuxbrew.
Answer
If you encounter Node.js version mismatches in your workspace, you can resolve this by using direnv to manage your environment variables. Here's how to fix it:
Instead of using `nvm use && yarn install`, use the following command:
direnv exec . yarn installIf the issue persists, you can try unloading and reloading nvm:
nvm unload . "$NVM_DIR/nvm.sh" nvm use
Note: If you continue to experience version mismatches, this may be due to PATH environment variable ordering where package managers like Linuxbrew are taking precedence over nvm. In such cases, you may need to modify your shell initialization files to ensure proper PATH ordering.
For best results when using nvm in a workspace:
Ensure your required Node.js version is installed before running workspace commands
Use `direnv exec .` to run commands that require specific Node.js versions
Verify the Node.js version being used with `node -v` before running dependency installation commands