Node.js
This section aims to establish a best practice JavaScript development environment. To make the best use of this book, it is presumed that you have the following prerequisites fulfilled:
- Awareness of the modern JavaScript ecosystem and tools
- NodeJS's site: https://nodejs.org
- Npm's site: https://www.npmjs.com
- Angular's site: https://angular.io
- Legacy AngularJS's site: https://angularjs.org/
- Yarn's site: https://yarnpkg.com
- React's site: https://facebook.github.io/react
Node.js is JavaScript that runs anywhere. It's an open source project that aimed to run JavaScript on the server, built on Google Chrome's V8 JavaScript engine. In late 2015, Node.js stabilized and announced enterprise-friendly 18 month LTS cycles that brought predictability and stability to the platform, paired with a more frequently updated, but more experimental, Latest branch. Node also ships bundled with npm, the Node package manager, and as of 2018, npm is the largest repository of JavaScript packages in the world.
For a more detailed look into Node's history, read my two-part article on Node at: Bit.ly/NodeJSHistory.
Note
You may have heard of yarn and how it's faster or better than npm. As of npm 5, which ships bundled with Node 8, npm is more feature rich, easier to use and on par with yarn in terms of performance. Yarn is published by Facebook, which also created the React JavaScript UI library. It must be noted that yarn relies on the npm repository, so whichever tool you use, you get access to the same library of packages.
Existing Node.js Installation
If you installed Node.js before, when installing a new version of Node using choco or brew, ensure that you read the command outputs carefully. Your package manager may return caveats or additional instructions to follow, so you can successfully complete the installation.
It is also highly likely that your system or folder permissions have been edited manually in the past, which may interfere with a frustration-free operation of Node. If the following commands do not resolve your issues, use the GUI installer from Node's site as a last resort.
Regardless, you must take care to uninstall all global tools that were installed using npm -g
previously. With every major Node version, there's a chance that native bindings between your tool and Node have been invalidated. Further, global tools rapidly fall out of date and project-specific tools quick go out of sync. As a result, installing tools globally is now an anti-pattern that has been replaced with better techniques, which are covered in the next section and under the Angular CLI section in Chapter 2, Create a Local Weather Web Application.
Note
To see a list of your globally install packages, execute npm list -g --depth 0
. To uninstall a global package, execute npm uninstall -g package-name
. I would recommend that you uninstall all globally installed packages and restart from scratch with the suggestions provided in the next section.
Installing Node.js
This book will presume that you're using Node 8.4 or a later version. Odd numbered versions of Node are not meant to be long lived. 6.x.x, 8.x.x, 10.x.x, and so on are okay, but avoid 7.x.x, 9.x.x, and so on, at all costs.
- Execute the installation command:
For Windows:
PS> choco install nodejs-lts -y
For macOS:
$ brew install node@8
- Verify installation of Node by executing
node -v
- Verify npm by executing
npm -v
Note
Note that you should never upgrade your npm version using npm install -g npm
on Windows, as highlighted in Chapter 4, Staying Up to Date with Angular Updates. It is highly recommended that you use the npm-windows-upgrade
npm package.
The npm repository contains numerous useful and mature CLI commands that are often cross-platform. Listed here are the ones I rely on frequently and choose to install globally for performance reasons:
npx
: Executes CLI tools by downloading the latest version on demand or project-specific localnode_modules
folder. It ships with npm 5 and will allow you to run code generators that frequently update without a global install.rimraf
: The Unix commandrm -rf
, but works on Windows as well. Very useful in deleting thenode_modules
folder, especially when Windows is unable to do so due to the nested folder structure.npm-update
: Analyzes your project folder and reports on which package have newer versions or not, with the option to be able to update all of them, if you so wish.n
: Dead easy to tool to switch between versions of Node quickly, without having to remember the specific version number. Unfortunately, it only works on macOS/Linux.http-server
: Simple, zero-configuration command-line HTTP server, which is a great way to locally test static HTML/CSS pages or even thedist
folder of your Angular or React project.npm-windows-upgrade
: Necessary to upgrade npm on Windows.