First Time Node - #1 Getting Started
This is the first of a short series which I am calling First Time Node. It is not meant to be comprehensive, it is not meant to teach best practices, it is simply here for someone who wants to get started and Node and needs help taking their first steps.
Step #1: Install Node
Installers for Windows and OSX can be found at NodeJS.org. If you are installing in a Linux distro with apt-get
, then enter the following from your terminal:
sudo apt-get install curl
curl --silent --location https://deb.nodesource.com/setup_5.x | sudo bash -
sudo apt-get install nodejs
To make sure Node has been installed and properly added to your path, enter node -v
and it should print out the version of Node you have installed. Check npm in the same way by typing npm -v
.
Step #2: Create Your Project
Node comes with a package manager called npm. An easy way to remember this is to thing of it as "Node Package Manager". The makers of npm are very clear that npm does NOT mean that, but if it is helpful for you to remember the letters, then think of it like that. The fist thing we want to do is initialize our project using npm. This will allow us to give our project a name and generate a package.json
file which will eventually record our dependencies and hold important scripts for us.
So, first create a new directory for you Node project and then enter into that directory from your terminal. Now, type in:
npm init
You will be asked a number of questions. Don't worry, what you enter here is not that important right now. Give you project a name (all lowercase with no spaces), but don't bother thinking too much about the others. As for the license, I generally enter Apache-2.0 because I make most everything I write available to the public and I do not want any restrictions on what others can do with my code.
Step #3: Create index.js
Now, since you have created a project folder and initialized a new project, it's time to create your first file! Create a file named index.js
in your project directory.
Open up index.js
and write some JavaScript:
console.log('Welcome to my first Node application!');
To run your script, enter node index.js
from within your project directory. It will print out Welcome to my first Node application!
.
Step #4: Configure Your Start Script
The package.json
file is a JSON formatted text file containing data about your project. Go ahead and open that file up. It's contents should look something like this:
{
"name": "my_project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "Apache-2.0"
}
Notice the "Scripts"
object. Currently it contains a script named test. We don't need that right now, so let's remove the "test"
script and enter one called "start"
with a value of "node index.js"
. So, your package.json
should now look something like this:
{
"name": "my_project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"author": "",
"license": "Apache-2.0"
}
So, the way npm scripts work is that now if we run the start script, it will run node index.js
. Try it out by entering npm start
from the project directory. It will print Welcome to my first Node application!
. From now on, that is how you will start your application.
You can enter as many scripts as you want in your package.json
file, but there are only two that are built in: start
and test
. Any other tests must be run by entering npm run [my-script-name]
. So, imagine in the future you have a build step in your application and you want to be able to run it with one script. You would enter a script called "build"
in your package.json
with a value of whatever commands it takes to build your application. If you wanted to run the script, you would enter npm run build
. Let me say it one more time, npm start
and npm test
can be run by themselves, but any other scripts must be run by entering npm run [my-script-name]
.
Conclusion
So, there you have it! You have created your first Node project. Obviously it doesn't do anything useful right now, but you know enough to get started. Write a few functions, run a few loops, print some random stuff. You can do whatever you want.
Next time I will show you how to import libraries and create and use packages in your application.
This is part one of an ongoing series:
- First Time Node - #1 Getting Started
- First Time Node - #2 Packages
- First Time Node - #3 Setting Up Your Development Environment
- First Time Node - #4 Configuring Your Build Process for ES2015/2016
- First Time Node - #5 Configuring & Writing Tests
- First Time Node - #6 Express Server With Functional Views