In this section we will set up Node, NPM, Yarn, and a basic package.json
file.
First, we need to install Node, which is not only used for back-end JavaScript, but all the tools we need to build a modern Front-End stack.
Head to the download page for macOS or Windows binaries, or the package manager installations page for Linux distributions.
For instance, on Ubuntu / Debian, you would run the following commands to install Node:
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install -y nodejs
You want any version of Node > 6.5.0.
npm
, the default package manager for Node, comes automatically with Node, so you don't have to install it yourself.
Note: If Node is already installed, install nvm
(Node Version Manager), make nvm
install and use the latest version of Node for you.
Yarn is another package manager which is much faster than NPM, has offline support, and fetches dependencies more predictably. Since it came out in October 2016, it received a very quick adoption and is becoming the new package manager of choice of the JavaScript community. We are going to use Yarn in this tutorial. If you want to stick to NPM you can simply replace all yarn add
and yarn add --dev
commands of this tutorial by npm install --save
and npm install --dev
.
-
Install Yarn by following the instructions. You can likely install it with
npm install -g yarn
orsudo npm install -g yarn
(yeah, we're using NPM to install Yarn, much like you would use Internet Explorer or Safari to install Chrome!). -
Create a new folder to work in, and
cd
in it. -
Run
yarn init
and answer the questions (yarn init -y
to skip all questions), to generate apackage.json
file automatically. -
Create an
index.js
file containingconsole.log('Hello world')
. -
Run
node .
in this folder (index.js
is the default file Node looks for in the current folder). It should print "Hello world".
Running node .
to execute our program is a bit too low-level. We are going to use an NPM/Yarn script to trigger the execution of that code instead. That will give us a nice abstraction to be able to always use yarn start
, even when our program gets more complicated.
- In
package.json
, add ascripts
object to the root object like so:
"scripts": {
"start": "node ."
}
package.json
must be a valid JSON file, which means that you cannot have trailing commas. So be careful when editing manually your package.json
file.
-
Run
yarn start
. It should printHello world
. -
Create a
.gitignore
file and add the following to it:
npm-debug.log
yarn-error.log
Note: If you take a look at the package.json
files I provide, you will see a tutorial-test
script in every chapter. Those scripts let me test that the chapter works fine when running yarn && yarn start
. You can delete them in your own projects.
Next section: 2 - Installing and using a package
Back to the table of contents.