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

Node interpreter setup documentation #2056

Closed
FossPrime opened this issue Jun 4, 2019 · 10 comments
Closed

Node interpreter setup documentation #2056

FossPrime opened this issue Jun 4, 2019 · 10 comments

Comments

@FossPrime
Copy link

FossPrime commented Jun 4, 2019

  • Operating system and version: Linux 5

  • How did you install nvm?
    install script in readme

  • What steps did you perform?
    nvm install 12

  • What happened?
    Everything worked wonderfully.

But, I have some node scripts that depend on /usr/local/bin/node existing. I can execute them just like any shell script, they have #!/usr/local/bin/node as the interpreter, and I run them with ./my-script.js myparams from the terminal.

  • What did you expect to happen?
    I expected some documentation about where to symlink /usr/local/bin/node to. Instead I get node: bad interpreter: No such file or directory errors when executing those scripts from the shell.

it seems like this is not an expected use case... I can't find any symlinks to the default node anywhere. it would be nice to have a default node symlink in /alias/node. Same for npx and npm. That way we could symlink it to a standard location, or be asked if we want to do that at installation the get go.

  • Is there anything in any of your profile files (.bashrc, .bash_profile, .zshrc, etc) that modifies the PATH?
    no.
@ljharb
Copy link
Member

ljharb commented Jun 5, 2019

If so, those scripts are broken. They should be using a shebang of #!/usr/bin/env node instead, so that the node path isn't hardcoded in those scripts.

@FossPrime
Copy link
Author

FossPrime commented Jun 5, 2019

If so, those scripts are broken. They should be using a shebang of #!/usr/bin/env node instead, so that the node path isn't hardcoded in those scripts.

Yes but that depends on the environment vars, which makes it hard to run in Cron, LaunchDeamons and systemd, where the environment is non existent or rudimentary. I remember having only /bin and /sbin in the path with the default cron shell

@ljharb
Copy link
Member

ljharb commented Jun 5, 2019

The tradeoff there is that you need to hardcode your node path, which means you have to change it for every system and/or every node version.

@FossPrime
Copy link
Author

FossPrime commented Jun 5, 2019

Redhat distros are very consistent about it's location... It's just always outdated. I don't deal with Debian enough to tell where apt-get likes to out it, but I imagine it's near /usr/local/bin/node

The main issue here is there is no easy way to point /usr/local/bin/node to whatever the NVM default is

@ljharb
Copy link
Member

ljharb commented Jun 5, 2019

The problem is that there isn’t an nvm default - there’s only per-shell PATH modifications. So if PATH isn’t available in a context, nvm isn’t really applicable or useful.

@FossPrime
Copy link
Author

I got my inid.d script to work with by loading the bash profile up top... . /etc/profile. Not efficient, but works. I still think that we should add a nvm/current symbolically linked directory on POSIX. That would be more efficient and reliable.

@ljharb
Copy link
Member

ljharb commented Jul 1, 2019

There is no “current” outside of the current shell session.

@FossPrime
Copy link
Author

FossPrime commented Dec 12, 2019

Went with this instead, due to serious slowdowns when network not available. I think I bricked Ubuntu on a previous machine with NVM because I ran it too early in profile.d.

export NVM_DIR="/opt/nvm"
if [ ! -f /usr/local/bin/node ]; then
        [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
        [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
 
        cd /usr/local/bin
        ln -s `which node`
        ln -s `which npm`
        ln -s `which npx`
        cd - &> /dev/null
fi

@ljharb
Copy link
Member

ljharb commented Dec 12, 2019

You definitely do not want to set NODE_PATH; global modules should never be requireable.

@FossPrime
Copy link
Author

FossPrime commented Dec 12, 2019

You're right... I was using it to run node -r esm for scripts with #!/usr/bin/env esm

I updated the esm script to

#!/bin/bash

if [ -x "$(command -v npm)" ]; then
  env NODE_PATH=`npm root -g` node -r esm $*
fi

could be useful for typescript bash replacement scripts too.

These methods turned out to be ... WAY faster and more resilient to network issues. Specially when using in profile.d scripts and sudo su'ing around a lot.

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

No branches or pull requests

2 participants