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

Added shell option to execute command with specific node version in PATH #148

Closed
wants to merge 1 commit into from

Conversation

FrozenCow
Copy link

This allows people to use n to quickly start a shell with a specific version without altering their system:

$ n shell 0.10.12
$ node --version
v0.10.12
(ctrl+d)
$ node --version
v0.10.18

This also helps a lot with installing packages for a specific node version:

$ n shell 0.10.12 npm install

It all works without write permissions to /usr/local/.

@tjwebb
Copy link
Collaborator

tjwebb commented Jun 28, 2014

How does this functionally supplement n use? It seems like a different way to accomplish the same thing. I'm open to a discussion on this, though.

@FrozenCow
Copy link
Author

This was mostly an addition because I didn't want to depend on the globally installed Node version, but you want to use Node of a specific version. That not just includes node, but also npm.

With n shell, node and npm of a specific version are put in the PATH environment variable. This can be useful when you use shell scripts that depend on node or npm, but you want it to use a specific version (probably the same version you use when you're running your node script):

$ n shell 0.10.12 ./setup_my_project.sh

Where setup_my_project.sh uses node or npm and now will use node and npm from Node 0.10.12, instead of the globally installed Node version.

It's also useful to run npm from a specific version of node. For example running npm install will install dependencies for your project with the node version you depend on. It can be very useful if you really need npm to run with the right node version. (install scripts, native modules, etc):

$ n shell 0.10.12
# Starts a new shell with the right PATH
$ npm install

In this example npm of Node 0.10.12 is used, instead of npm of the globally installed Node version.

@tjwebb
Copy link
Collaborator

tjwebb commented Jun 29, 2014

You could also do this:

$ bash
$ n <version>
$ npm install

or, if you're ok at being stuck at this version:

$ n use <version> /usr/local/bin/npm install

It's more awkward, but it's possible. I do think this could be improved though.

If your goal is to run shell scripts in a particular node version setting, I'd support a feature that makes this possible:

$ NODE_VERSION=<version> n env ./myscript.sh

which would create an ephemeral environment based on NODE_VERSION. I just see limited use cases for the shell command as you've described it. Also, we couldn't just support bash, we'd have to support all the major shells.

What are your thoughts on these alternatives?

@maxrimue
Copy link
Contributor

Closed due to no response

@maxrimue maxrimue closed this Aug 28, 2015
@tandrewnichols
Copy link

Something like this (not necessarily shell . . . the NODE_VERSION idea could work too) would be nice for subshell node processes. Say, for example, with grunt tasks that spawn other grunt sub-processes. You can do something like n use <version> $(which grunt) my-task, but if that task spawns another task, that spawned process runs under the node in your PATH and not the one you are useing.

On a related note, you might have noticed from the bizarre syntax there, running something installed globally with use is quite difficult. You can't just n use <version> grunt as that will look within your pwd and error saying, "Can't find module [pwd]/grunt". I should probably just open a separate issue for that. Seems like you could get around that with npm prefix -g (or which as in my example).

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

Successfully merging this pull request may close these issues.

None yet

4 participants