npm install, and go, right?
Well, for some applications, that’s true. But if you need to compile extensions, you’ll need a few more things. And, of course, with Node.js itself being constantly under development, you’ll want to lock down your development to a version your code can use. In this post, I’ll talk you through how we get our Windows command-line environments set up for the Node.js (actually, Electron) application my team is developing.
First Things First
No one wants to waste time hunting down downloads for a development environment. Instead, install Scoop first, and you’ll get a nice, clean way to add the packages you’ll need without a single web search.
Once you’ve got Scoop installed, it’s time to add some packages. For just Node.js, you’ll want the nodejs package, plus nvm for version management with NVM:
scoop install nodejs nvm
If your project uses Yarn, as ours does, you can grab that from Scoop, as well:
scoop install yarn
If you’re planning on checking out or committing code to GitHub, you’ll also want tools for that:
scoop install openssh git
Finally, in case you want to quickly do things as an administrative user (which you may, later in this post!), I recommend you install Sudo, which knows how to elevate privileges inside a PowerShell session without spawning a brand new one:
scoop install sudo
Managing Node.js versions
The next thing you’ll want to do is make sure you’re on the right version of Node.js for your project. We’re using the latest LTS version for ours, which as of the time of this writing is 8.11.2. So we issue two NVM commands to install and use it:
nvm install 8.11.2 nvm use 8.11.2
If you’re familiar with NVM on Unix-like systems, you’ll find it works a little differently on Windows with Scoop. When you use a new Node.js version, it will update the binaries under
scoop\apps\nvm instead of in
If you use a version and it doesn’t seem to be taking effect, check your
PATH environment variable in the System Properties control panel (search for “environment”); it’s probably been re-ordered. Move the path containing
scoop\apps\nvm to the top, and the NVM-selected version will now take precedence.
We don’t have any of our own extensions that need building in our project, but some of our dependencies (namely, node-sass) do.
Extensions like these are built with node-gyp, and node-gyp needs two things: Python (2… wince) and a C compiler, neither of which are standard equipment on a Windows system. If you don’t have them and you need them to build extensions, you will see a long string of
gyp ERR! messages when you install dependencies.
Thankfully, there’s a reasonably easy way to install them already configured for node-gyp: windows-build-tools.
After you’ve installed the Scoop nodejs package above, and assuming you installed Sudo, you can now run:
sudo npm install --global --production windows-build-tools
Note that we have observed these installers rebooting a system at least once, which effectively aborted the process. We fixed this in this one case by re-running the installer like so:
sudo npm uninstall --global windows-build-tools sudo npm install --global --production windows-build-tools
The Moment of Truth
If all the installations worked, you should be ready to go. For our application, a
yarn install yarn start
was all we needed—of course, you’ll want to start your application however you do normally.
In our case, our application started up and we were off and running. If you have found a more complex case, let me know how you solved it in the comments.