Skip to content

Instantly share code, notes, and snippets.

@ross-u
Created April 21, 2020 13:29
Show Gist options
  • Save ross-u/3dd456cf9f36e4a79f9eb72e8da84d12 to your computer and use it in GitHub Desktop.
Save ross-u/3dd456cf9f36e4a79f9eb72e8da84d12 to your computer and use it in GitHub Desktop.

NodeJS


CLI

Command Line interface - Program where you type things in your computer rather than clicking - example Bash on Linux, Terminal on Mac, Command Prompt on Windows.

When working with node.js you will be working with it from the Command Line Interface , CLI .

V8 The Javascript engine - (at the heart of Node.js)

Machine language is a language that your computer processor speaks and it is very low level.

  • Google's V8 engine is a JavaScript Engine written in C++.

  • V8 engine converts JavaScript into machine language and gives it to the processor.

  • V8 was initially made to be used in the browser, however V8 can run alone. This means that you can use the V8 code and combine it with another C++ application / program.

  • Node.js is a V8 engine with some additional C++ code added, that enables you to run JavaScript outside of the browser.


NodeJS with V8


Node provides features that are not available in the browser, like:

  • creating a server.
  • ability to accept HTTP requests and send HTTP responses.
  • loading multiple JavaScript files as modules.
  • Working with files (write to the hard drive, load files).
  • Working with databases.

Node.js is a server technology and a JavaScript runtime environment, it is not a framework nor a programming language.


Check if you have Node.js installed

node --version
# Returns the version of the NodeJS if installed
# 12.13.0

npm --version
# Returns the version of the npm

Installing Node.js (if not installed)


Using NodeJS

Create a file test.js
mkdir 00-node-intro && cd 00-node-intro


touch test.js

code .
Add some content to the test.js
test.js
console.log("Hello Node");
Run the file from the terminal:
node test.js

NPM - node package manager and modules


What is node package manager?

  • npm is a Node Package manager that enables us to download and install different JavaScript files called 'packages' or 'modules' into our project.
  • Other alternative package manager is Yarn, however yarn is out of scope of this lecture and we will be using only npm as our package manager.

npm consists of 3 things:

  1. npm CLI used to get the packages and it is also a server for all the packages.

  2. npm registry that is the database used to keep and serve the packages.

  3. npm website - NPM documentation. It aslo contains information for every published package.


Packages (modules)

  • Modules aka packages are bundles of reusable code that are packaged with any of the code they depend on (dependencies).

  • One package can be build of several other packages. We call them dependencies, when one package depend on others to be installed as well.

  • When an npm package that we are using is updated by the package owner, we get the notification that package in our app needs an update. In this way maintaining the code is more sustainable.

Packages aka modules start from silly things such as getting the funny vaca printed on your terminal to an entire framework like React that you can get and install in your project.


To check if your npm was installed with node, in your terminal run :

npm --version


Installing packages

  • In order to install packages in our project we have to create a package.json file.

  • package.json is a manifest file that tells Node and us, what packages (aka dependencies) are installed in our project.

npm init creates a package.json file

mkdir npm-getting-started  &&  npm-getting-started

touch index.js

code .

While in the project folder, run the following command in the terminal:

npm init

npm init will initiate a prompt of questions to be answered for the setup of the package.json file.


npm install --save chalk

What happens when we run the above command ?

  1. npm install grabs the package from the npm servers

  2. --save flag adds adds the package to our package.json file under dependencies .

  3. If not existing already, thenode_moudles folders is created. This is where the acctual code for all packages is stored.

If we want to have the module only for development and not for deployment, we can save it as a Development Dependency, using flag --save-dev.

npm install --save-dev nodemon

Differences between:

npm install,

npm install --save and

npm install --save-dev


  1. npm install will install the npm package to the node_modules folder without adding it to the package.json

  2. npm install --save - package(s) required by the application to run.

  3. npm install --save-dev - package(s) required for development purpose.


dependencies

dependencies are 3rd-party packages that the application needs in order to run or function.

These packages will be deployed with the production version of the app and will be downloaded to the user’s browser cache whenever someone uses the app/website. In case of the back-end apps dependencies are installed during the deployment and are required on the machine instance where the server is running.

devDependencies

devDependencies are used only during the development of the app, only by the developers of the in order to build and test the app. These packages are not deployed in the production version and will not be downloaded to the browser cache of end users or to the server in case of the back-end apps.



NodeJS Modules


Node require() / module.exports

VS

ES6 import / export


NodeJS - require() / module.exports

require is the standard module loading API for the NodeJS, which means that in order to load or export a file/module in NodeJS environment, we have to use the require and module.exports statements.


ES6 - import / export

ES6 introduced import and export syntax, however for this to work on our NodeJS project we have to setup Babel transpilation (translating) of the code from import/export syntax to require syntax that NodeJS uses.


Use the installed package

Let's use our package.

To import a module and use it in our script we use the NodeJS' built inrequire() method.

index.js
const chalk = require('chalk');

// console.log('Hello Node'); 

console.log(chalk.green('Hello ironhackers'));
console.log(chalk.yellow.bgRed.bold('Hello ironhackers'));

Run the script using NodeJS
node index.js


Check out the chalk package npm page - https://www.npmjs.com/package/chalk


Exporting a module module.exports con

Let's create a file that will contain greeting messages that we want to use.

touch greetings.js
greetings.js
const greetings = {
  en: 'Hello Ironhackers',
  es: 'Hola Ironhackers',
  de: 'Hallo Ironhackers',
  fr: 'Salut Ironhackers',
  it: 'Ciao Ironhackers'
};

/* 
We export the greetings object, in order to be able to 
require it from another file.
*/
 module.exports = greetings;

We can now load the exported module/file in our test.js file

index.js
const chalk = require('chalk');
const greet = require('./greetings'); // .js extension is not compulsory
/*

greet = {
  en: 'Hello Ironhackers',
  es: 'Hola Ironhackers',
  de: 'Hallo Ironhackers',
  fr: 'Salut Ironhackers',
  it: 'Ciao Ironhackers'
}

*/

console.log( chalk.whiteBright.bgBlue.bold(greet.en) );

console.log( chalk.yellow.bgRed.bold(greet.es) );

console.log( chalk.bgYellowBright(greet.de) );

console.log(
  chalk.whiteBright.bgBlueBright(greet.fr.split(' ')[0]),
  chalk.whiteBright.bgRed(greet.fr.split(' ')[1]),
);

console.log(
  chalk.whiteBright.bgRed(greet.it.split(' ')[0]),
  chalk.whiteBright.bgGreen(greet.it.split(' ')[1]),
);

Additional Material

Dev Dependencies vs Dependencies (5min read)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment