Terminal

toggle vscode terminal: Ctrl + ~

Node

What Is It?

JavaScript runtime environment

Is it installed on my machine?

run on terminal this:

node --version

and this:

npm --version

REPL - Read Evaluate Print Loop

execute:

node

to end the node session press: Ctrl + C

Run Script

create file index.js

console.log('Gimel Yafit');

execute:

node index.js # output: Gimel Yafit

CommonJS - Running Multiple Scripts

my_project
├── index.js
└── nisim.js

simplest

nisim.js

const bestSinger = 'Avi Biter';
module.exports = bestSinger;

index.js

const myVariable = require('./nisim');
myVariable; // 'Avi Biter'

export an object:

nisim.js

const bestSinger = 'Avi Biter';
module.exports = { bestSinger };

index.js

const nisim = require('./nisim');
nisim; // { bestSinger: 'Avi Biter' }
nisim.bestSinger; // 'Avi Biter'

with destructor:

nisim.js

const bestSinger = 'Avi Biter';
module.exports.bestSinger = bestSinger;

index.js

const { bestSinger } = require('./nisim');
bestSinger; // 'Avi Biter'

export multiple stuff:

nisim.js

const bestSinger = 'Avi Biter';

function isBestSinger(singerName) {
	return singerName === 'Avi Biter';
}

module.exports = {
	bestSinger,
	isBestSinger
};

index.js

const { bestSinger, isBestSinger } = require('./nisim');
bestSinger; // 'Avi Biter'
isBestSinger('Aci Biter'); // true
isBestSinger('Shimi Tavori'); // false

npm - Node Package Manager

install packages on your system

comes from: npmjs.org

Quick Start

create package.json file

config by a wizard

npm init

default config

npm init --yes

Add package to your project

npm install lodash

index.js

const lodash = require('lodash');
const arr = ['nisim', 'shlomo', 'david'];
lodash.each(arr, name => console.log(name));
  • dependencies in package.json file
  • node_modules folder

In case not all dependencies appear in node_modules.

run on terminal:

npm install

Uninstall package

npm uninstall lodash