Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 504 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to write a .nvmrc file which automatically change node version

#1
Hi I have two projects one in angularjs 4.4.7 and another in angular 6 version. I need to switch between node version for this. I tried using NVM which is working manually. How to handle the version change inside the angularjs program to change the node version when automatically the latest angular page gets loaded. Is there a possible way like that. I went through the #avn also but how to create the .node-version file. Can someone help with any link or correct sample steps
Reply

#2
Check the [README](

[To see links please register here]

) from nvm's repo on GitHub. Solutions have been given there.

## Shell Integraton

For **bash**, put the following at the end of your `$HOME/.bashrc`, the shell will change the node version according to the `.nvmrc` file under the dir.

```
find-up () {
path=$(pwd)
while [[ "$path" != "" && ! -e "$path/$1" ]]; do
path=${path%/*}
done
echo "$path"
}

cdnvm(){
cd "$@";
nvm_path=$(find-up .nvmrc | tr -d '[:space:]')

# If there are no .nvmrc file, use the default nvm version
if [[ ! $nvm_path = *[^[:space:]]* ]]; then

declare default_version;
default_version=$(nvm version default);

# If there is no default version, set it to `node`
# This will use the latest version on your machine
if [[ $default_version == "N/A" ]]; then
nvm alias default node;
default_version=$(nvm version default);
fi

# If the current version is not the default version, set it to use the default version
if [[ $(nvm current) != "$default_version" ]]; then
nvm use default;
fi

elif [[ -s $nvm_path/.nvmrc && -r $nvm_path/.nvmrc ]]; then
declare nvm_version
nvm_version=$(<"$nvm_path"/.nvmrc)

declare locally_resolved_nvm_version
# `nvm ls` will check all locally-available versions
# If there are multiple matching versions, take the latest one
# Remove the `->` and `*` characters and spaces
# `locally_resolved_nvm_version` will be `N/A` if no local versions are found
locally_resolved_nvm_version=$(nvm ls --no-colors "$nvm_version" | tail -1 | tr -d '\->*' | tr -d '[:space:]')

# If it is not already installed, install it
# `nvm install` will implicitly use the newly-installed version
if [[ "$locally_resolved_nvm_version" == "N/A" ]]; then
nvm install "$nvm_version";
elif [[ $(nvm current) != "$locally_resolved_nvm_version" ]]; then
nvm use "$nvm_version";
fi
fi
}
alias cd='cdnvm'
```

Cause there's no hook support in Bash, the solution above is ugly.

For **zsh**, put this into your `$HOME/.zshrc`

```shell
# place this after nvm initialization!
autoload -U add-zsh-hook
load-nvmrc() {
local node_version="$(nvm version)"
local nvmrc_path="$(nvm_find_nvmrc)"

if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")

if [ "$nvmrc_node_version" = "N/A" ]; then
nvm install
elif [ "$nvmrc_node_version" != "$node_version" ]; then
nvm use
fi
elif [ "$node_version" != "$(nvm version default)" ]; then
echo "Reverting to nvm default version"
nvm use default
fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc
```

## A Better Solution
A better solution is to use [nodenv](

[To see links please register here]

). I'm not kidding, `nodenv` is very different from nvm, and n.

`nodenv` is a member of the `rbenv` family. These version managers have big advantages over the others.

1. It changes node version without modifying environment variable `PATH` time by time, because it uses **shim executables**. This makes it having a builtin support to **switch node version automatically**.
2. Auto version switch in `nodenv` doesn't have to be hooked on `chpwd` to do periodical check for directory change. The version selection is delayed to when `node` command is executed.
3. The commands in `nodenv` are implemented in scripts. While, commands from `nvm` are implemented in functions, which means all the 4000+ line of code have to be parsed on shell startup and increases the shell init time dramatically. `nodenv` initializes much faster.

References

- [nodenv/nodenv](

[To see links please register here]

)
Reply

#3
After you create the .nvmrc file at the root of your project with the node version you need in that project, something like

v12.20.0

You should be able to cd into the project folder and run `nvm use`. This will print something like this:

Found '/Users/you/myproject/.nvmrc' with version <v12.20.0>
Now using node v12.20.0 (npm v6.14.8)

There is no automated way AFAIK, provided out of the box by NVM except by creating a bash script that does this for you which the NVM documentation covers in detail [here][1]


[1]:

[To see links please register here]

Reply

#4
As @Aditya-M-P has already mentioned you can run the following command inside your projects root directory to generate the `.nvmrc` to set a desired NodeJS version for you project to work properly:
```sh
node -v > .nvmrc
```
It will generate something like this inside your `.nvmrc` file:
```
v10.16.2
```
Also using `10.16.2` without the `v` letter will work just fine.

However, in the official documentation in the [.nvmrc][1] section it never mentions that once you get this file created, the specified node version will be loaded automatically.
So that's not enough, you need to run the command below so that `nvm` can look for the `.nvmrc` file to load the specified version:
```
nvm use
```
Here it is a gif for demoing purpose:
[![enter image description here][2]][2]

> **To autoload the specified node version:**

You need to add something else to your shell configuration depending on what you use `bash` or `zsh`

To get the exact configuration for each of them, please follow the instructions in the corresponding [shell config section][3].

In my case I'm using `zsh` so I do need to add this at the end of my `.zshrc` file and here is the image that confirms it works like a charm:
```sh
# place this after nvm initialization!
autoload -U add-zsh-hook
load-nvmrc() {
local node_version="$(nvm version)"
local nvmrc_path="$(nvm_find_nvmrc)"

if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")

if [ "$nvmrc_node_version" = "N/A" ]; then
nvm install
elif [ "$nvmrc_node_version" != "$node_version" ]; then
nvm use
fi
elif [ "$node_version" != "$(nvm version default)" ]; then
echo "Reverting to nvm default version"
nvm use default
fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc
```
[![enter image description here][4]][4]

I hope it could be useful for anyone else facing the same question! 😎


[1]:

[To see links please register here]

[2]:

[3]:

[To see links please register here]

[4]:
Reply

#5
As pointed out in the [GitHub issue thread](

[To see links please register here]

) related to this on the `nvm` repository, you may run the following command in each of your Angular project folders:

$ node -v > .nvmrc

Note that you need to first switch to the right version of node in each of your projects, *before* running the command above.

**What's happening in the command**:

- `node -v` will out the current version of `node` to `stdout`.
- The `>` symbol will then `redirecting` the output to a file called `.nvmrc` (it will overwrite if something already exists with the same file name).
- Read more bash redirections under the *REDIRECTION* section under the bash man page:

[To see links please register here]


When you `cd` into your target directories, `nvm` will now first read the file, and auto-switch to the correct version.
Reply

#6
For something less involved than a shell hook, you could add a `preinstall` or `prebuild` script to your package.json so every time you try to install or build your project uses the correct version defined in your `.nvmrc` file.

Example `.nvmrc` file:

18.10.0

Example scripts in `package.json` file:

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"preinstall": "nvm use",
"prebuild": "nvm use",
"build": "tsc"
},

I have not tested this yet but plan to add it to my own project soon. I'll report back how it works.

Note: Using the `preinstall` hook is only called when doing `npm install` and not when installing a specific module.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through