Copyright 2017 Lynn Beighley. All rights reserved.
Published by
Maker Media, Inc.
1700 Montgomery Street, Suite 240
San Francisco, CA 94111
Maker Media books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (safaribooksonline.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com.
Publisher: Roger Stewart
Editor: Patrick DiJusto
Copy Editor: Elizabeth Welch, Happenstance Type-O-Rama
Proofreader: Scout Festa, Happenstance Type-O-Rama
Interior Designer and Compositor: Maureen Forys, Happenstance Type-O-Rama
Cover Designer: Maureen Forys, Happenstance Type-O-Rama
December 2017: First Edition
Revision History for the First Edition
2017-12-13 First Release
See oreilly.com/catalog/errata.csp?isbn=9781680454239 for release details.
Make:, Maker Shed, and Maker Faire are registered trademarks of Maker Media, Inc. The Maker Media logo is a trademark of Maker Media, Inc. Jumpstarting JavaScript and related trade dress are trademarks of Maker Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Maker Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-68045-423-9
Safari Books Online
Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the worlds leading authors in technology and business. Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training. Safari Books Online offers a range of plans and pricing for enterprise, government, education, and individuals. Members have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like OReilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and hundreds more. For more information about Safari Books Online, please visit us online.
How to Contact Us
Please address comments and questions to the publisher:
Maker Media, Inc.
1700 Montgomery Street, Suite 240
San Francisco, CA 94111
You can send comments and questions to us by email at books@makermedia.com.
Maker Media unites, inspires, informs, and entertains a growing community of resourceful people who undertake amazing projects in their backyards, basements, and garages. Maker Media celebrates your right to tweak, hack, and bend any Technology to your will. The Maker Media audience continues to be a growing culture and community that believes in bettering ourselves, our environment, our educational systemour entire world. This is much more than an audience, its a worldwide movement that Maker Media is leading. We call it the Maker Movement.
To learn more about Make: visit us at makezine.com. You can learn more about the company at the following websites:
Maker Media: makermedia.com
Maker Faire: makerfaire.com
Maker Shed: makershed.com
Maker Share: makershare.com
Preface: What Is Node.js?
J avaScript is a programming language that web browsers can run. Browsers like Firefox and Chrome are built with engines that can understand and execute programs written in the JavaScript language. Firefox has an engine called SpiderMonkey, and Chromes engine is called V8.
Running JavaScript in a browser limits what you can do with it. For example, with JavaScript in a browser, you are limited to interacting with web pages. You can detect errors when people enter information in a form. You can open browser windows and alert boxes. But you cant control anything outside of a browser.
Fortunately, you have another option. When you install and use the JavaScript extension Node.js, your JavaScript code can run independently of a web browser. (Youll often see Node.js simply called Node, and thats what well do for the rest of this book.)
Node is the V8 JavaScript engine bundled together with libraries that handle input/output and networking. This means that Node lets you use JavaScript outside of a browser to run shell scripts, manage back-end services, and run directly on devices.
What You Bring to the Book
- Basic familiarity with programming concepts
- Ability to set up and connect a Raspberry Pi
- Ability to access and use a console
- Understanding of sudo, directories, and file creation/editing
Chapter 1
Meet JavaScript and Node.js!
T his chapter takes you through the steps to install Node on your Mac, Windows, or Linux OS. Then youll check to see that its working.
NOTE
As you install Node, you may notice that something called npm is also being installed. This program is the Node Package Manager.
Node is really popular, and it has a whole new ecosystem of useful Node-based code packages other developers have created for you to use. When you want to use one of these packages in Node, you need an easy way to install and manage them. Thats what npm does for youit installs additional Node packages you want to use.
Follow the instructions for your system then jump to the last section, Node Is Installed Now What?
Install Node on Mac
Although you could build Node from the source code, this guide is all about getting you going quickly. The easy way is to visit the official Node website, Nodejs.org, and use an automated installer. Here are the steps involved:
Step 1. Visit ).
Nodejs.org download page
Step 2. Find the file you downloaded and double-click it. Youll see the installation dialog (). Click Continue to go through the installation. Stick with the default settings.
Step 3. When the installation finishes, youll see a summary screen with installation information ().
Node installer
Next page