This is a practical course using node.js and a localhost development environment.

INSTALL THE LOCAL ENVIRONMENT

For local development I use Laragon (https://laragon.org/), it is really well organized, easy to use and portable.

Download Laragon – Full, is has already the latest node.js stable edition inside, double click the Laragon icon and Start all services, if you are under Windows you will see a Windows Command Processor message, ignore it.

WHAT IS NODE JS MODULES AND NPM

Node.js is organized in modules, a module is like a javascript library that do something.
The core of Node.js has only few modolus (built-in module), to do foundamental operations like:
– HTTP transfer data
– File System management
– URL parsing
– Event fire and listen
etc…

If you need more you have to install other modules, for example to upload files, send emails, connect to a database. To live happy you have to use NPN (Node Package Manager https://www.npmjs.com/) that is the official ‘repository’ for Node.js modules, NPM works great because it install all dependencies too 😀

USE OF NPM – TEST INSTALLATION

1. Start Laragon (or your local environment), and run all services

2. Create a folder inside laragon/www/myapp

3. Start the terminal in Laragon ‘Cmder’, put the terminal inside myapp folder you will see in the command prompt: laragon\www\myapp then you are inside the right folder

4. type: npm -h
If you see the help instructions of npm it means that npm is installed and runs correctly, nice!

USE OF NPM – SEARCH MODULES

5. type: npm search mysql
You will see all the list of modules to manage mysql, it is clear, the syntax is npm search [keyword]

USE OF NPM – INSTALL MODULES

6. type: npm istall mysql
Go inside laragon/www/myapp npm has created:

a. folder ‘node_modules’ and inside it you can find mysql module and all dependencies.
NOTICE: the module is installed ONLY in the local folder of your app, if you have another app you have to install again for every app you create.

b. package-lock.json that contains the informations about downloaded modules

USE OF NPM – CODE WITH MODULES

7.

// load modules in code
var mysql = require('mysql'); 

// use modules functions
var con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

nodejs

USE OF NPM – UPDATE MODULES

Super easy! Type: npm update
NOTICE: this command delete the old module and install the new one, pay attention to compatibility between old and new packages

USE OF NPM – PUBLIC YOUR MODULES

You can publish for free your own modules and become famous :) give a look to https://docs.npmjs.com/cli/publish