NVM for windows - ability to specify specific version without switching - node.js

I've installed NVM for windows, so I can switch between different versions of node when I have to. However, I have a need to be able to call one version of node / npm WITHOUT having to switch
For example, right now I have 2 versions of node installed: 10.17.0 and 8.9.1. I can easily switch between them using nvm use 10.17.0 or nvm use 8.9.4, and after either of those commands, when I run node ./app.js or npm install package, it uses the expected version of node / npm to do what I'm asking
But I want to create a situation where I can avoid switching, and instead I can just type node10 ./app.js or npm10 install package, and it uses the version I specify
Now, I've done a bit of research myself about what it would take to do this, so I'll lay that out and hope that someone has some advice because they've done something similar.
First of all: nvm installs both versions of node in a specific place, and that place should stay static and unchanging. I can find the 8.9.1 versions of node and npm here: C:\Users\SamuelR\AppData\Roaming\nvm\v8.9.1
I can find the 10.17.0 versions here: C:\Users\SamuelR\AppData\Roaming\nvm\v10.17.0
When I use nvm use 10.17.0, what it does is it changes a symlink at C:\Program Files\nodejs, and it points it to whichver of those two folders I specify
So my idea to make node8 / node10 and npm8 / npm10 commands work was this:
In some folder in my PATH, I was going to create 4 .bat files, node8.bat npm8.bat node10.bat npm10.bat. For node8.bat, I was going to follow the example here:
C:\Users\SamuelR\AppData\Roaming\nvm\v8.9.1\node.exe %*, so that it always forwards all my command line arguments to the correct installation.
For npm, the file npm8.bat would have C:\Users\SamuelR\AppData\Roaming\nvm\v8.9.1\npm.cmd %*.
Maybe I'd do the same for npx. And then I'd repeat those steps for the node10.bat npm10.bat files.
Is this the right way to go about this? Does it even make sense?

Welp, nobody's giving any answers, but I've tried it myself and as far as I can tell, it works!
I'll just lay it out for total clarity here: once you have nvm working as you like, find the install directory, in my case it was C:\Users\SamuelR\AppData\Roaming\nvm
For each version of node in there that you want to make these shortcuts to, make the following files,
node{v}.bat (eg node10.bat)
npm{v}.bat (eg npm10.vat)
npx{v}.bat (eg npx10.vat)
Put them all in a file that is accessible in your PATH environment variable (I made a new folder called C:\Program Files\nodelinks and put it in my path myself)
inside node10.bat:
C:\Users\SamuelR\AppData\Roaming\nvm\v10.17.0\node.exe %*
inside npm10.bat:
C:\Users\SamuelR\AppData\Roaming\nvm\v10.17.0\npm %*
inside npx10.bat:
C:\Users\SamuelR\AppData\Roaming\nvm\v10.17.0\npx %*
As far as I can tell, it passes all arguments through as expected, there's no apparent permissions issue, it works exactly as expected.

Related

Why does my computer keep forgetting node and npm installation?

My computer keeps forgetting that I have node installed despite the fact that all of the files are still there. So far, I have tried installing through the node installer on the nodejs website, using brew, using nvm, and also several of the suggestions in this gist. No matter what I try, within a couple of hours or a couple of days, I am working away and then I am suddenly throwing node errors, and when I check node -v or npm -v, I get the error command not found: node or command not found: npm. The only thing that I can figure out to get node working again is to uninstall whatever I most recently tried and then to reinstall.
It seems like I might be having some sort of env or path issue, but I don't know where to begin to start troubleshooting anything like that.
EDIT
Based on the suggestion below, I added these to my .bash_profile and it seems to have fixed my issue. Not sure why the install wasn't adding them here or in .bashrc, but adding it manually fixed the issue:
export PATH="$HOME/.npm-packages/bin:$PATH"
export PATH="$HOME/.node_modules/bin:$PATH"
maybe you forgot to add node & npm binaries to your path that's why when you open a new instance of your terminal it says that it doesn't know node and npm
Simply add the line that register the binaries into your path within your .profile file that might be located in your home directory

Specify the path to node when running executable npm moduels

I'm fairly new to node and npm so imagine this might be the way I'm trying to use it. My understanding so far and questions are as follows.
When installing modules with npm if they have executables they are created in node_moadules/.bin
For example npm install istanbul creates
node_moadules/.bin/istanbul.cmd with the following contents
#IF EXIST "%~dp0\node.exe" (
"%~dp0\node.exe" "%~dp0\..\istanbul\lib\cli.js" %*
) ELSE (
#SETLOCAL
#SET PATHEXT=%PATHEXT:;.JS;=;%
node "%~dp0\..\istanbul\lib\cli.js" %*
)
This seems to be roughly similar for other npm modules installed.
1) When would there be a node.exe in the bin directory (%~dp0 is the path of the batch file - so node_moadules/.bin)? When using npm istanbul it will use the above script but I don't understand when it would use the IF block, it seems it would always go into the ELSE which uses node on the path to run it. This will work as intended when I have node installed and on my path. However this would seem to be a problem if you don't have it on your path or need to use a specific version in a project (different from the version on your path).
2) I can fix this by manually editing the above script to point to the specific node version but don't want to have to do this for all module executables installed with npm. Is there a way to configure this in npm so it uses a specific path to node when it generates these executable files?
A bit more background
We're using the gradle-node-plugin in our build so we don't have to manage node and npm on our build machines, this does it for us. However this means we don't have node on the path.
The plugin allows me to install npm modules but there is no clear way to then run the executables, using the version of node unpacked by the plugin, without manually editing the generated executable scripts as in 2), or instead of calling npm istanbul calling something like this:
<path_to>\node <path_to>\node_modules\istanbul\lib\cli.js
This also doesn't seem right as I'm hardcoding to the cli.js executable path within the module which could presumably change in future versions of that module.
If anyone could explain this, or tell me where the specific part of the docs that do are that would be helpful. I've tried to search but probably not using the right terms.

Functions may not be defined within control directives or other mixins

In my nodeJS project suddenly it give error.I do not any upgrade or changes.
When I write NPM start in command line it give that error
ERROR in ./~/css-loader?sourceMap!./~/postcss-loader!./~/sass-loader?
sourceMap&outputStyle=expanded&includePaths[]=c:/TFS/rc1/app/scss&includePaths[]
=c:/TFS/rc1/~/compass-mixins/lib&sourceMap&sourceMapContents=true!./app/scss/_toolkit.scss
Module build failed:
undefined
^
Functions may not be defined within control directives or other mixins.
in c:\TFS\rc1\node_modules\compass-mixins\lib\compass\functions\_lists.scss (line 81, column 3)
# ./app/scss/_toolkit.scss 4:14-337
I reinstall compass-mixins package but it still give same error.Then I looked _lists.scss that file in 81 line there is a code .I deleted that bu it give same error.What should I do?
#if not(function-exists(compact)) {
#function compact($vars...) {
$list: ();
#each $var in $vars {
#if $var {
$list: append($list, $var, comma);
}
}
#return $list;
}
}
I also face the similar problem. And my project is using gulp-sass and compass-mixins. As heart.cooks.mind points out that, gulp-sass is depending on node-sass. And node-sass upgrade one of its dependence libsass to version libsass 3.3.3 Delorean since node-sass 3.5.1.
However, one of libsass 3.3.3 Delorean changes is related to this problem:
'Disallow functions to be defined in control directives or mixins (#mgreter, #1550)'
Obviously, _lists.scss in compass-mixins break this rule. Seems someone raise an issue to compass-mixins and they have idea on fixing it.
Before compass-mixins release the issue fixed version, my temporary workaround is to delete node_modules/node-sass manually and npm install node-sass#3.4.2
I had the same issue,please refer to node sass release 3.5.3 is breaking build and force lock gulp-sass to use the specific node sass library using shrinkwrap and avoid using the buggy version of node sass
I am using Gulp. Version 2.3.0 of gulp-sass breaks it. Go back to Version 2.2.0 and you are all fixed.
Edit:
The real culprit is the node module inside the "gulp-sass" node module known as "node-sass". You can see inside "gulp-sass"'s package.json file that it simply pulling version greater than ^3.5.3.
Even if you go back and reinstall "gulp-sass" to 2.2.0, as I suggested earlier, the package.json file in there will still pull "node-sass" greater than ^3.5.3.
If I use the older version of "node-sass" 3.4.2 that error goes away. I don't know how to fix that in an automated way. In a pinch I was able to fix the problem by copying that folder (that is using 3.4.2) from a different project that works. Now it compiles.
Can someone smarter than me figure out a better way to accomplish this result?
What I (temporarily) did, was to install globally the node-sass v3.4.2, and then replace the gulp-sass version of node-sass (it is located within gulp-sass/node_modules) with this older one.
sudo npm install -g node-sass#3.4.2;
sudo cp -r /usr/lib/node_modules/node-sass/ /usr/lib/node_modules/gulp-sass/node_modules/;
Btw, there is a PR waiting to be merged for this. But if you want to use this today then there's a fork of the merge too.
If you want to use latter then just put compass-mixins: tjenkinson/compass-mixins in your package.json and all will be good.
Update:- There's also an npm package for the latter mentioned in the PR now
Update 2:- This should no longer be a problem with v0.12.8 now

Error after packaging the app with electron-packager

I'm new to Electron, and I really love it so far, but I'm unable to package any of mine apps, at first I thought that it's maybe something related to my code, then I download "https://github.com/atom/electron-quick-start" run npm install and then I run "electron-packager . FooBar --platform=darwin --arch=x64 --version=0.28.2" it build the app but when I try to open it I get
so I didn't touch any code from the example, just wanted to build it and I got an error, what am I doing wrong? Thanks!
The versions of electron are moving very very fast.
And some times, they don't respect the "old" ways to do things (for example, declaring the app).
I advise you to not use the 0.28.2 version of electron but the most recent one.
It is very likely that the version of electron-prebuilt you are using to develop is much much much more recent than the 0.28.2 version. So, you are developing with something much newer, and then you are building with 0.28.2. This would cause the exact error that you are seeing, as older versions may not have had the electron module, which your code explicitly is importing. So... that is my suggestion. Change the version in your electron-packager command from 0.28.2 to something like 0.36.0. See if that works. Or better yet, use the same version as electron-prebuilt in your package.json.
This could be a combination of factors.
First, as others stated, the version of electron that you have might be newer than the one referenced in your build command. Locate the 'electron_prebuilt' folder inside your 'node_modules' folder, and examine the package.json file and make sure the version # is the same as what you are declaring in your build command.
If they are the same, then the issue might be that you have another version of electron on your computer that node is trying to use. If you installed electron via the -g option (global), check your home folder to see if there is another different version of electron. If you find one, either delete it or rename the 'electron_prebuilt' folder you find to something else. Try your build command again, and it should work now that you've eliminated the other versions of electron_prebuilt on your computer that node was referencing.
What worked for me was to move the "electron" module from "dev-dependencies" to "dependencies" in package.json. Try this and see if it works.

When using nvm to manage your node versions, should you point webstorms node interpreter to it?

When you switch to the nvm to manage your node versions, should you point your node interpreter to that one?
ie from /usr/bin/node to the ~/.nvm/v.xxx one
I ask because I'm running into some issues with webstorm when trying to debug node stuff now.
Best thing I've found so far is to add each version to Default 'Node.js and NPM' Settings (rather than in individual Project settings). Then when creating new projects, or updating existing ones, you can select that version from the dropdown instead of having to recreate it for each project.
But it would be nice if the NodeJS Plugin would look for nvm and integrate with it. Or if nvm would create symlink for /usr/bin/node and update that when you change the version.
Yes, you need to explicitly set the Node Interpreter to the version you'd like to use
You should not need to do that, it seems it was a bug that was identified and reported. A fix will be released in the coming 2016.1.3 EAP version of WebStorm.
https://youtrack.jetbrains.com/issue/WEB-21558
As #zenw0lf stated above, WS does now look for installed node versions under .nvm but since .nvm does not yet add an alias for the active version of node, the specified value is fragile as it's version-based.
I've attempted to resurrect the appropriate issue in NVM to alleviate this. Please follow that for updates.

Resources