Node.js: Tools and Skills, 2nd Edition
Copyright 2020 SitePoint Pty. Ltd.
Ebook ISBN: 978-1-925836-39-4
- Product Manager: Simon Mackie
- Project Editor Editor: James Hibbard
- English Editor: Ralph Mason
- Cover Designer: Alex Walker
Notice of Rights
All rights reserved. No part of this book may be reproduced, stored in a retrieval system or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews.
Notice of Liability
The author and publisher have made every effort to ensure the accuracy of the information herein. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors and SitePoint Pty. Ltd., nor its dealers or distributors will be held liable for any damages to be caused either directly or indirectly by the instructions contained in this book, or by the software or hardware products described herein.
Trademark Notice
Rather than indicating every occurrence of a trademarked name as such, this book uses the names only in an editorial fashion and to the benefit of the trademark owner with no intention of infringement of the trademark.
Published by SitePoint Pty. Ltd.
Level 1, 110 Johnston St, Fitzroy
VIC Australia 3065
Web: www.sitepoint.com
Email: books@sitepoint.com
About SitePoint
SitePoint specializes in publishing fun, practical, and easy-to-understand content for web professionals. Visit http://www.sitepoint.com/ to access our blogs, books, newsletters, articles, and community forums. Youll find a stack of information on JavaScript, PHP, design, and more.
About Craig Buckler
Craig is a freelance developer, author, and speaker who never shuts up about the web.
He started coding in the 1980s when applications had to squeeze into a few kilobytes of RAM. His passion for the Web was ignited in the mid 1990s when 28K modems were typical and 100KB pages were considered extravagant.
Over the past decade, Craig has written 1,200 tutorials for SitePoint as web standards evolved. Despite living in a technically wondrous future, he has never forgotten what could be achieved with modest resources.
Preface
While there have been quite a few attempts to get JavaScript working as a server-side language, Node.js (frequently just called Node) has been the first environment that's gained any traction. It's now used by companies such as Netflix, Uber and Paypal to power their web apps. Node allows for blazingly fast performance; thanks to its event loop model, common tasks like network connection and database I/O can be executed very quickly indeed.
In this book, we'll take a look at a selection of the related tools and skills that will make you a much more productive Node developer.
Who Should Read This Book?
This book is for anyone who wants to start learning server-side development with Node.js. Familiarity with JavaScript is assumed, but we don't assume any previous back-end development experience.
Conventions Used
Code Samples
Code in this book is displayed using a fixed-width font, like so:
A Perfect Summer's Day
It was a lovely day for a walk in the park.The birds were singing and the kids were all back at school.
Where existing code is required for context, rather than repeat all of it, will be displayed:
function animate() { new_variable = "Hello";}
Some lines of code should be entered on one line, but weve had to wrap them because of page constraints. An indicates a line break that exists for formatting purposes only, and should be ignored:
URL.open("http://www.sitepoint.com/responsive-web-design-real-user-testing/?responsive1");
Youll notice that weve used certain layout styles throughout this book to signify different types of information. Look out for the following items.
Tips, Notes, and Warnings
Hey, You!
Tips provide helpful little pointers.
Ahem, Excuse Me ...
Notes are useful asides that are relatedbut not criticalto the topic at hand. Think of them as extra tidbits of information.
Make Sure You Always ...
... pay attention to these important points.
Watch Out!
Warnings highlight any gotchas that are likely to trip you up along the way.
Supplementary Materials
- https://www.sitepoint.com/community/ are SitePoints forums, for help on any tricky problems.
- books@sitepoint.com is our email address, should you need to contact us to report a problem, or for any other reason.
Chapter 1: Installing Multiple Versions of Node.js Using nvm
by Michael Wanyoike and James Hibbard
When working with Node.js, you might encounter situations where you need to install multiple versions of the runtime.
For example, maybe you have the latest version of Node set up on your machine, yet the project youre about to start working on requires an older version. Or maybe youre upgrading an old Node project to a more modern version and it would be handy to be able to switch between the two while you make the transition.
Without a good tool, this would mean spending a lot of time and effort manually uninstalling and reinstalling Node versions and their global packages. Fortunately, theres a better way!
Introducing nvm
nvm stands for Node Version Manager. As the name suggests, it helps you manage and switch between different Node versions with ease. It provides a command-line interface where you can install different versions with a single command, set a default, switch between them and much more.
OS Support
nvm supports both Linux and macOS, but thats not to say that Windows users have to miss out. Theres a second project named nvm-windows that offers Windows users the option of easily managing Node environments. Despite the name, nvm-windows is not a clone of nvm, nor is it affiliated with it. However, the basic commands listed below (for installing, listing and switching between versions) should work for both nvm and nvm-windows.
Installation
Lets first cover installation for Windows, macOS and Linux.
Windows
First, we need to do a little preparation:
- uninstall any existing versions of Node.js
- delete any existing Node.js installation directories (such as
C:\Program Files\nodejs
) - delete the existing npm install location (such as
C:\Users\\AppData\Roaming\npm
)
After this, download and run the latest stable installer and you should be good to go!
macOS/Linux
Unlike Windows, removing previous Node and npm installations in macOS and Linux is optional. If this is something you want to do, there are plenty of good resources available online. For example, heres how to remove Node on macOS and on Linux. And heres how you can remove any previous npm installation you might have.
You can install nvm using cURL or Wget. On your terminal, run the following:
With cURL:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.35.2/install.sh | bash