Skip to content
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

use_node doesn't work at all #319

Open
zaro opened this issue Dec 16, 2017 · 12 comments
Open

use_node doesn't work at all #319

zaro opened this issue Dec 16, 2017 · 12 comments
Labels

Comments

@zaro
Copy link

zaro commented Dec 16, 2017

Recently upgraded my direnv , and now every .envrc I used that has use_node stopped working with :

direnv: Unable to find NodeJS version (v8.9.1) in (/Users/me/.nvm/versions/node/)!
direnv: error exit status 1

After debugging a bit found that the problem is here :

563   node_prefix=$(
564     # Look for matching node versions in $NODE_VERSIONS path
565     find "$NODE_VERSIONS" -maxdepth 1 -mindepth 1 -type d -name "$node_wanted*" |
566
567     # Strip possible "/" suffix from $NODE_VERSIONS, then use that to
568     # Strip $NODE_VERSIONS/$NODE_VERSION_PREFIX prefix from line.
569     while IFS= read -r line; do echo "${line#${NODE_VERSIONS%!/(MISSING)}/${node_version_prefix}}"; done |
570
571     # Sort by version: split by "." then reverse numeric sort for each piece of the version string
572     sort -t . -k 1,1rn -k 2,2rn -k 3,3rn |
573
574     # The first one is the highest
575     head -1
576   )

specifically

while IFS= read -r line; do echo "${line#${NODE_VERSIONS%!/(MISSING)}/${node_version_prefix}}"; 

No matter what settings I have, the ${line#...} expansion always fails, and leaves line as absolute path. Here are some experiments in the command line:

$ export NODE_VERSIONS=/Users/me/.nvm/versions/node/
$ export node_version_prefix=v
$ echo '/Users/me/.nvm/versions/node/v8.9.1' |  while IFS= read -r line; do echo "${line#${NODE_VERSIONS%/(MISSING)}/${node_version_prefix}}"; done
/Users/me/.nvm/versions/node/v8.9.1
$ echo '/Users/me/.nvm/versions/node/v8.9.1' |  while IFS= read -r line; do echo "${line#${NODE_VERSIONS%/(MISSING)}/}"; done
/Users/me/.nvm/versions/node/v8.9.1
$ echo '/Users/me/.nvm/versions/node/v8.9.1' |  while IFS= read -r line; do echo "${line#${NODE_VERSIONS%/(MISSING)}}"; done
v8.9.1

I have removed the ! before (MISSING) because it expands in interactive console. But what you can see is that

"${line#${NODE_VERSIONS%/(MISSING)}/${node_version_prefix}}"
"${line#${NODE_VERSIONS%/(MISSING)}/}"

Do not trigger expansion correctly while

"${line#${NODE_VERSIONS%/(MISSING)}}"

does. I observe the same behaviour on both macOs/bash-3.2 and ubuntu/bash-4.3 .

@zaro
Copy link
Author

zaro commented Dec 16, 2017

What I found as a workaround for me is to copy the current use_node definition from direnv stdlib , and add it to ~/.config/direnv/direnvrc with

 node_prefix="${NODE_VERSIONS}/${node_version_prefix}${node_prefix}"

line commented.

@zimbatm zimbatm added the Bug label Dec 18, 2017
@zimbatm
Copy link
Member

zimbatm commented Dec 18, 2017

/cc @f440 @mdawaffe and @wilmoore who worked on that specific function

@wilmoore
Copy link
Contributor

Somehow, I did not get the ping @zimbatm. I found this issue just searching around the issue tracker.

@zaro - what's your $SHELL?

@steve-ross
Copy link

I am working on some additional helpers to use direnv with some of our internal projects. We do a lot of node and typically we use NVM to manage versions. Anyway... I finally put it in a repo yesterday. I'm sure linking to this will show how pitiful my bash skills are however, you can find it here if anyone is interested: https://github.com/steve-ross/direnv-helpers

enables you to put stuff like:
requires_nvm in your projects .envrc
Which then reads your .nvmrc file, installs the required node version via NVM if it is missing, runs npm install (if there isn't a node_modules), does a use node etc

@wilmoore
Copy link
Contributor

wilmoore commented Feb 8, 2018

I dig the __prompt_install_nvm hooks. I wonder if the idea could be further extended to just handle the node binary installation directly without installing an installer?

I don't have enough free time to explore this much further ATM, but ultimately, direnv has enough tooling to negate the need to support X number of installers (i.e. nvm, n, ...).

If kept to basic sh scripting, one would need only install direnv and create an .envrc. to have a working Node.js environment.

@steve-ross
Copy link

@wilmoore yeah, I totally get that. Our main reason for creating some of these helpers was that we didn't want to have to copy/paste modify configs for the multiple node projects we work on. It also helps us spin up new developers very quickly.

@speed-of-light
Copy link

this is what worked for me

export NODE_VERSIONS=~/.nvm/versions/node
export NODE_VERSION_PREFIX='v'
use node 10.4

direnv version
2.16.0

@MemoryReload
Copy link

@speed-of-light Yeah, you really save my ass buddy! The use_node() function in stdlib.sh need these environment variables and the default settings are not right. So we must fix this by ourselves
. Thanks again!

@ustun
Copy link

ustun commented Oct 20, 2020

Wanted to mention volta as an alternative way to pin node versions in projects(actually it is an alternative to nvm): https://docs.volta.sh/guide/

@segunadebayo
Copy link

Managed to get this working Volta:

volta install node (grabs the latest version)
export NODE_VERSIONS=~/.volta/tools/image/node/
export NODE_VERSION_PREFIX=''
use node <latest version>

@lee-pai-long
Copy link

This is not working for me still in direnv version 2.32.3.

I set the NODE_VERSIONS correctly and the NODE_VERSION_PREFIX to 'v'. I use the .node-version file and put the exact version installed in it without the 'v'.
When direnv executes it fails with the error:

Unable to find NodeJS version (20.3.0) in (/home/sma/.nvm/versions/node)!

But using ls in /home/sma/.nvm/versions/node shows that there is a v20.3.0 directory. Looking at the use_node method in stdlib.sh file, it seems like the value of the version directory path computated is wrong, but it not shown in the error message so I can't debug it.

The workaround I found was to use the load_prefix function directly like this:

load_prefix $HOME/.nvm/versions/node/v$(cat ./.node-version)

The only caveat is that one need to set the exact version number for it to work.

@lestephane
Copy link

This line looks extremely suspicious:

use_node() {
...
  local node_version_prefix=${NODE_VERSION_PREFIX-node-v}

Specifying NODE_VERSION_PREFIX will NOT work in bash (which the shebang says the stdlib is written in).

The proper syntax is ${NODE_VERSION_PREFIX:-node-v} for bash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

10 participants