15

My OS is win7, and I using MSYS2(version:MSYS_NT-6.1), Please give advice how to install nodejs and npm on this terminal, Thanks!

Ivan
  • 459
  • 1
  • 3
  • 9

5 Answers5

20

I found a solution for resolving the problem,

64bit env.

pacman -S mingw-w64-x86_64-nodejs

32bit env.

pacman -S mingw-w64-i686-nodejs

after installed, Open terminal

$ node -v
v6.11.0
Ivan
  • 459
  • 1
  • 3
  • 9
  • 3
    In addition, `npm` is distributed with Node.js which means that when you download Node.js, you automatically get npm installed. Check with `$ npm -v ` – Alejandro Blasco May 24 '18 at 13:35
  • 3
    For those wondering why node.exe is not in the msys2 path, you need to use the appropriate mingw console. (32/64-bit) – Alex Barker Dec 18 '18 at 17:44
  • 3
    nodejs package is not available in msys packages (https://packages.msys2.org/search?t=pkg&q=nodejs) – Chris P Nov 19 '21 at 15:29
11

As of 2020, the package mingw-w64-x86_64-nodejs is not available any more. The simplest way to have Node.js, npm and git installed on a Windows machine is using the official Windows installers:

After installation, open a command prompt (click on start, and then type cmd and [ENTER]) and verify that all three tools are there:

git --version
node --version
npm --version

Later on, to update Node.js, simply reinstall it from the same source.

jmgonet
  • 1,134
  • 18
  • 18
  • If you use another terminal such as Windows Terminal, you have to add `-full-path` option when starting `msys2_shell.cmd`. The `-full-path` option was not suggested for some reason in the official documentation at https://www.msys2.org/docs/terminals/ – Cerlancism May 31 '21 at 06:15
  • Did you use the Windows installer for this? – jpaugh Aug 19 '21 at 15:55
  • This doesn't work. My MSYS terminal is "different" from windows cmd, and running cmd in the MSYS terminal, then npm within _it_ and still the same issue... – MathCrackExchange Sep 03 '22 at 17:10
2

I wasted a lot of time on this. My solution is:

mido
  • 24,198
  • 15
  • 92
  • 117
  • While with this solution, the .npmrc file would be generated in C:\Users\tom instead of in the $HOME of msys2 shell, how to remedy it? – Iceberg Jul 21 '21 at 06:33
2

It does work to use the Windows installer, and Node.js helpfully provides bash-script versions of npm and npx in C:\Program Files\nodejs\ to help streamline the process.

However, contrary to Cerclanism's comment @ jmgonet's answer, you should not use --full-path with MinGW, no matter what terminal you're using, since that will by default bring the entire Windows path into your MinGW environment.

(Assuming you're a typical Windows developer with things like MSVC, Windows Python, and etc. install dirs on your path, containing plenty of names that clash with MinGW path members, you can see how that might bite you at some point down the road. My full Windows CMD.exe %PATH% is 1236 characters! I don't want all that sucked into MinGW.)

Instead, you should add the nodejs install dir to your MinGW shell $PATH, say by using everyone's favorite ~/.profile/~/.zprofile $PATH-munging trick:

# Append node.js to path
case ${PATH} in
  *"/c/program files/nodejs"*)
    ;;
  *)
    export PATH="$PATH:/c/program files/nodejs:"
    ;;
esac

You'll probably also want to set some configuration, since by default Windows npm will use ${APPDATA}/npm for prefix, ${LOCALAPPDATA}/npm-cache for cache, C:\Windows\system32\cmd.exe for shell, etc.

# To view the full config including all defaults and overrides
npm config ls -l
# To view the active config for the specified environment
npm config list -L {global,user,project}

Maybe I was just confused, but to me it seemed, from what the configs show/say, that setting prefix= in my user config would override even local installs. (The project-specific ones where you npm install without --global, directly into a node_modules subdir of the current dir.) But after testing, happily I can report that's not the case, so it's safe to override the builtin prefix= from your $HOME/.npmrc.

Whether or not you move the cache= or let it stay at C:\Users\<you>\AppData\Local\npm-cache\ is your call. I'm sure it'll work that way. (Well, maybe not from an MSYS shell, but from MinGW it should be fine.)

There are minor differences I haven't overcome, but the only one that comes to mind right now is:

  1. npm help <command> opens a browser window to HTML documentation, instead of displaying man page content directly in the terminal like it does on Linux. (Makes sense, as I don't think the manpages are even installed on Windows. Still disconcerting, though.)
FeRD
  • 1,699
  • 15
  • 24
0

You can simply install nvm, then install nodejs through there. In your MSYS2 shell just run the following to download and install nvm. Its better to go directly here and copy the download commands as the version numbers will change in the url.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash

or

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash

then run the following to setup nvm on your bash path:

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

After running those commands you can use nvm install 16 or whatever node major version number you want. Type just nvm to get a list of available commands.

nenchev
  • 1,998
  • 28
  • 16