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

Feature request: Get node version from packages.json engines.node #260

Open
ddfridley opened this issue Sep 20, 2022 · 4 comments
Open

Feature request: Get node version from packages.json engines.node #260

ddfridley opened this issue Sep 20, 2022 · 4 comments

Comments

@ddfridley
Copy link

I love the idea of being able to use a different version of node with different directories. In my case projects are github repos and the version of node to use is already specified in packages.json engines.node I wish nvs could just use that instead of having me duplicate the information in some other file.

Thanks!

@ljharb
Copy link
Contributor

ljharb commented Sep 20, 2022

the engines field doesn't specify a version of node, it specifies a version range (even if it's a range of 1). It's not appropriate to impose those semantics on engines.node. See #184.

@ddfridley
Copy link
Author

ddfridley commented Sep 20, 2022

Thanks for looking at this. When I push my project to heroku, heroku looks at the node.engine and fetches that latest matching version before building and starting up. So there is one example of it already being done.

Further the problem we are facing is that people are downloading my repo and building it in order to contribute to our project. But we are having more and more problems with different versions of nodes not working. And npm will complain if the version doesn't match but it won't take action. But then when switching to some other project the node version should change to whats appropriate for that.

I see projects pushing docker images and virtual machines that already have the right versions of node built in - but this is a weighty solution to the problem.

I dream of a future where we just tell people to install nvs instead of node and then they can 'cd' from project to project and always be on the right version of node.

If you point out where to get started I might give it a try sometime.

Again thanks this.

@ddfridley
Copy link
Author

In the mean time, I added this to my .bashrc file:

export NODE_VERSION=$(cat package.json | grep '\"node\":' | head -1 | awk -F: '{ print $2 }' | sed 's/[",]//g')
nvs add $NODE_VERSION;
source nvs.sh use $NODE_VERSION

Thanks again for nvs!

@shadowspawn
Copy link

shadowspawn commented Jan 30, 2023

For interest, n supports an auto version target inspired by the auto name in nvs. n does not support changing node versions when changing directories.

I added package.json support to auto, but people reported annoyances with it blocking the use of the more explicit version control files further up the folder hierarchy, particularly in mono-repo layout. I changed the implementation to search the hierarchy twice, once for explicit version files (like .node-version) and only if none found then try a scan for package.json. There have not been any further reports of priority annoyances.

However, I also added an engine target to explicitly target package.json. So I don't know how much the auto support for package.json is used, but at least it is no longer being reported as an issue!

Reference: tj/n#644

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

3 participants