-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
.nvmrc for projects #110
Comments
I'm not sure what it would do. NVM doesn't wrap the node executable, it just helps you manage multiple $PATH values. |
Switch to the node version specified in .nvmrc? Why not use the package.json file? |
But what will do the switching? Are you going to run |
As far as I know the rvm checks on every |
I just looked at how rvm implemented this. I am going to do the same approach. |
I like this idea too, except that using the |
There is already an engine version specified in the package.json file. There is zero need to add yet another dotfile to do this. I'd love for nvm to do this automatically as well. But definitely not by adding yet another |
Hi guys, For those interested, I spent some time this week end to write a proof of concept around the idea of parsing the package.json on a cd. Actually I use a ruby script to parse the json file and define what to do, since my bash-jutsu is quite limited. I didn't work much on the version operators either, so there's only a limited support (>=, <= and = are supported actually). The fork can be found here: |
Idea: regarding "auto-switching", would it be better if we just add an nvm command to pick the best version that's suited to the current folder's package.json? something like nvm select # auto-select a local version that best matches what's specified in package.json I have used rvm before and honestly didn't like the cd-stealing approach. I already have a few commands that try to steal my |
@chakrit : The last version includes the nvm select command as you requested and dissociate the change directory hook in a separate file. I also added the support for all the version range expressions (as documented here: https://npmjs.org/doc/json.html#dependencies, minus the git and http ones). I guess there's some edge cases I didn't tested yet, I'll update it as soon as I'll find them. |
@abe33 nice! :) using your fork right now. EDIT: ruby? ಠ__ಠ |
@chakrit : Yes, as I said in my first comment, my skills with shell script aren't that good, and I had hard times to find out how to parse a json file with pure shell - re-writing a json parser wasn't really part of the plan ^^ - and writing the version matching algorithm was beyond my knowledge by far. |
Related feature requst: http://cnodejs.org/topic/516a1f0e6d382773062dfd8a |
I also think it would be nice to add an nvm command that can try to figure out the engine to use by parsing the package.json. I have a lot of projects with different node versions, so it’d be a time saver if I don’t have to go look into package.json myself to figure out which version I want to tell nvm to use. |
But yeah, I’m not sure how easy it is to parse package.json in pure Bash. |
@Hydrozen sounds like you need a new tool. Let's not bloat nvm with complex features like this. Searching recursively through package.json files is non-trivial for a bash function. (parsing a single json file in bash is hard enough) Write a new tool that works in any version of node that outputs a string for the ideal node version to run, and then pass that string to nvm. |
I'm going to close this issue. After thinking about this for a while, it's outside the scope of nvm. Nvm is used to install and switch between versions of node manually. There is an automatic default version, but I really want to keep the tool simple. I do think it would be a great project for someone to create a new tool written in node.js that looks at the package.json files in the current working tree and outputs the ideal node version to run. This tool will need to be installed somewhere outside nvm's tree and in your $PATH so that it will stay in scope as you switch between node versions. It can use |
@creationix No pb for me, the ruby code can easily be ported to js so I'll give it a try when I can find some spare time. |
It will be a cool thing to have! |
I would pay 2$ if somebody implements this feature. |
@potomak you'll pay someone to write a patch I've said I won't accept, or pay someone to write a separate utility as I suggested? (If it's the first, you'll just have to maintain a fork to keep the patch. there won't be hurt feelings) |
@creationix the best would be to get this patch merged inside your repo (note I'd give part of that bounty to you as the maintainer of the project) but I could pay also for the second solution. |
@creationix plugin nag : ) |
The |
@koenpunt right, I was talking about parsing package.json in nvm. That's outside the scope of the project. |
I didn’t even know about the |
@creationix Have you considered something like this?
|
That doesn't work if the engine is defined as |
@koenpunt , depending on the semantics you desire, you can use something like this if you want to use the least-recent supported version in
|
@wbyoung want to PR a link to it in the readme under that section? |
Hello,
I've created a When I open both projects using WebStorm IDE and run a But when I manually And I'm wondering what happens if I have two shells open at the same time in different project, showing a different version. Is nvm able to use one version by directory and somehow manage to use several different versions at the same time? Or is my shell lying and actually using the latest nvm version loaded? Thanks for input. |
@Vadorequest the hook should in fact be executed whenever you cd into a directory. There might be something wrong with your environment but this is out of scope (as stated in the readme) please post that question on stackoverflow or similar. @ljharb close this issue? |
@ohcibi Okay, I just wanted to know if I missed a step or not. What about that part?
Thanks. |
@Vadorequest each shell session has its own environment, but thats way out of scope of this project 8-). |
Oh, I wasn't aware of that. Thank you ;) |
I think this is a must-be feature for nvm. Don't you ever work with multiple projects at the same time period, guys? Do you really love to check 'uhmm.. which node version I had used for this particular project..'? This process should be automated and It's not a fact of simplicity not having this feature. If you would really like that kind of 'simplicity' you wouldn't use a tool like nvm and you would just keep changing the paths manually. I vote +1 for reconsideration. |
@scaryguy we have had |
My solution isn't a solution generic enough but I mixed together the previous suggestions with stackoverflow to add the following for
Now I can do:
Fun right? |
Great to see work on this. Any chance in the absence of |
Also, I made https://www.npmjs.com/package/strict-version to help with forcing version |
@kirkstrobeck that would require parsing JSON in POSIX, as well as handling semver ranges. It's not feasible at this time. |
Sure, but I’ve got to believe that work is already done and just needs to be added as a lib, how does npm handle it? |
|
This supports auto-switching of node versions with a shell hook or other tools. For example: - [zsh hook](nvm-sh/nvm#110 (comment)) - [avn](https://github.com/wbyoung/avn)
Inspired by @jusopi, I wanted to automate setting up the correct node version. I chose to parse function enter_directory {
if [[ -e "package.json" ]]; then
nvm use `cat package.json | jq -r '.engines.node'`;
fi
}
export PROMPT_COMMAND=enter_directory Not super robust, but keeps from having to duplicate node version in different places. We pin down an exact version in our projects anyway. |
nm, I need that directory check, as it runs every time a prompt is shown: function enter_directory {
if [[ $PWD == $PREV_PWD ]]; then
return
fi
PREV_PWD=$PWD
if [[ -e "package.json" ]]; then
nvm use `cat package.json | jq -r '.engines.node'`;
fi
} |
Well, I use the old version of the script and it worked fine e when using cd, but when opening a prompt from an ide such as Vscode, it didn't used the cd command when opening a prompt, leading myself into issues such as installing a package in the wrong version and messing everything up.
|
Would be nice if nvm would understand semver, e.g.:
|
@olalonde Writing a semver parser in posix is not a trivial task. However, |
fish shell: functions cd function cd --description 'Change directory'
set -l MAX_DIR_HIST 25
if test (count $argv) -gt 1
printf "%s\n" (_ "Too many args for cd command")
return 1
end
# Skip history in subshells.
if status --is-command-substitution
builtin cd $argv
return $status
end
# Avoid set completions.
set -l previous $PWD
if test "$argv" = "-"
if test "$__fish_cd_direction" = "next"
nextd
else
prevd
end
return $status
end
builtin cd $argv
set -l cd_status $status
if test $cd_status -eq 0 -a "$PWD" != "$previous"
set -q dirprev[$MAX_DIR_HIST]
and set -e dirprev[1]
set -g dirprev $dirprev $previous
set -e dirnext
set -g __fish_cd_direction prev
end
if test -f .nvmrc
nvm use
end
return $cd_status
end |
@greyhawk |
I'll add my variation of the a Bash alias you can use to perform this automatic switching.
As a one-liner:
To run on your shell:
|
If you're using fish, but don't want to modify the function __use_nvmrc --on-variable PWD --description 'Use .nvmrc if it exists'
# https://github.com/fish-shell/fish-shell/issues/583#issuecomment-13758325
status --is-command-substitution; and return
# https://github.com/creationix/nvm/issues/110#issuecomment-324228987
if test -f .nvmrc
nvm use
end
end @ljharb greyhawk might be using this oh-my-fish plugin for nvm. Easy way to get nvm working in fish shells. |
I've updated my last alias. It's longer but is smarter about how it finds the
Also see https://asciinema.org/a/191898 |
To elaborate on @greyhawk great
function find-up --description 'Find file recursively in parent directory'
set path (pwd)
while test "$path" != "/" ; and not test -f "$path/$argv[1]"
set path (dirname $path)
end
if test -f "$path/$argv[1]"
echo "$path/$argv[1]"
else
echo ""
end
end
function cd --description 'alias cd cd'
set -l MAX_DIR_HIST 25
if test (count $argv) -gt 1
printf "%s\n" (_ "Too many args for cd command")
return 1
end
# Skip history in subshells.
if status --is-command-substitution
builtin cd $argv
return $status
end
# Avoid set completions.
set -l previous $PWD
if test "$argv" = "-"
if test "$__fish_cd_direction" = "next"
nextd
else
prevd
end
return $status
end
builtin cd $argv
set -l cd_status $status
if test $cd_status -eq 0 -a "$PWD" != "$previous"
set -q dirprev[$MAX_DIR_HIST]
and set -e dirprev[1]
set -g dirprev $dirprev $previous
set -e dirnext
set -g __fish_cd_direction prev
end
set loaded_version (node --version)
set nvmrc (find-up '.nvmrc')
if test -f $nvmrc
set nvmrc_version (cat $nvmrc)
if test $loaded_version != $nvmrc_version
nvm use --silent
end
else
if string match -q -- "*/.nvm/*" $PATH
nvm unload
end
end
return $cd_status
end EDITAfter playing with @Jamesford's solution, I think it's even more powerful! Very interesting thread 🎉 |
@b-long no, i would not consider it, because it's been implemented for almost 7 years: #110 (comment) |
It would be nice if we had a ".nvmrc" file for a project, kind of like ".rvmrc" for ruby. I'll start prototyping this later. What do you guys think?
The text was updated successfully, but these errors were encountered: