Getting started with NodeJS, nvm, npm.
I’ve recently been playing with NodeJS, and wanted to share my findings. This is just a quick ‘hello world’ tutorial on getting started. I (as of writing) code perl for a living, so you should definitely take my word on nodejs ;). Ok, enough joking around, let’s get to it!
nvm
First of all, I can thoroughly recommend using the node version manager, nvm – even if you are only working on a single, small project. nvm manages different versions of nodejs installations on your system. While you don’t have to use it and can use pre-compiled binaries (or packages) or compile node yourself, my personal advice is to use it. The reason for this is that nodejs moves very quickly, and you may sooner or later run into having to run two applications that support different major versions of nodejs. While, in an ideal world, everyone would upgrade their dependencies, reality is often different. By using nvm, you can easily install and manage different versions of node in your system, potentially saving yourself headaches from these problems.
You can install nvm with a single command, curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.6/install.sh | bash
. If you’re alergic to magic curl | sh
scripts, you can also perform a manual installation
nodejs
With nvm
If you’ve got nvm, nodejs is really easy to install! just run nvm install --lts
to retrieve the latest long term support version.
Without nvm
(Skip this if you used nvm)
If you can’t or don’t want to install nvm, you can install nodejs from nodejs.org.
npm
npm is the node package manager, and you get it automatically when you install nodejs. It’s how dependencies are managed in the node.js world – if your software depends on a third-party library (such express) you will usually find a package that you can install from the npm registry. This gives an easy and standard way of managing third-party dependencies.
Dependencies
Your app’s dependencies are kept in package.json. You can tell npm to install and automatically save something to this file. For example:
npm install --save express
Will install express and save it as a dependency in package.json
. Now, every time someone downloads your software, they can automatically install all of your dependencies by just doing npm install
.
Hello world
Now that you finally have everything you need, you can start writing code!
For example, here’s a very simple hello-world.js
:
console.log("Hello world!"); // Very simple!
You can run this with node hello-world.js
Using modules
Remember npm from earlier? You may be wondering: “Where do the modules I install go? How do I use them”?
npm installs modules in the ./node_modules
subdirectory of your project root.
To use a module installed by npm you can just require
it – for example const express = require("express")
will load express. You don’t need to tell node where to look – it already knows to look in ./node_modules
.
Express
Now, this is a simple express app, let’s call it hello-express.js
// Load express const express = require('express'); const app = express(); // Start listening on port 3000 app.listen(3000, () => console.log('App started')); // Set up your routes. app.get('/', (req, res) => res.send('Hello World!')); app.get('/page', (req, res) => res.send('Second page'));
You can run your app like the previous one: node hello-express.js
You can now visit your server at http://localhost:3000/. Magic!
errietta@Moltres [2] ~/hello % curl http://localhost:3000 Hello World! errietta@Moltres [2] ~/hello % curl http://localhost:3000/page Second page
A bit more express
You can also define your routes in separate files – this makes things cleaner. For example, consider routes/account.js
const express = require('express'); const router = express.Router(); router.get('/', function (req, res) { res.send({ users: [1, 2, 3] }); }); router.get('/:user_id', function(req, res) { const user_id = req.params.user_id; const users = { 1: { name: 'John', surname: 'Doe' }, 2: { name: 'Jane', surname: 'Doe' }, 3: { name: 'Best', surname: 'User' }, }; if (!users[user_id]) { res.json({ error: `User id ${user_id} not found` }); } else { res.json(users[user_id]); } }); module.exports = router;
Now in your main file, add:
const accountRoute = require('./routes/account'); app.use('/account', accountRoute);
This will allow your app to serve /account and /account/(user id)
$ curl http://localhost:3000/account/ {"users":[1,2,3]} curl http://localhost:3000/account/2 {"name":"Jane","surname":"Doe"}
Next steps
Now that you know the basics, what’s next?
- Check out nodemon if you’re fed up of restarting your express app!
- Read the nodejs docs
- Find modules on npm
- Check out this list of awesome projects
I also want to make a tutorial for a simple Typescript + express app, since I’ve been working on that, so watch this space 🙂
Until next time!
Really this is very informative post, thanks for sharing with us.
Thanks much for the brief as well as resourceful coverage.