node - GradedJestRisk/js-training GitHub Wiki

Table of Contents

Overview

Handful:

Links

Timeline

More here List:

Install

using nvm

Node Version Manager (NVM) allow easy version switching for:

  • node;
  • node package manager (npm).

install nvm

Install for:

  • bash
    • curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash
    • activate auto-completion npm completion >> ~/.zshrc
  • zsh
    • https://github.com/lukechilds/zsh-nvm , section "As an Oh My ZSH! custom plugin"
  • upgrade: nvm upgrade

install node/npm

Latest:

  • check who is the latest:
    • nvm ls-remote | tail
    • OR nvm ls
  • install latest: nvm install node
  • activate: nvm use node
  • check installed (green arrow) : nvm ls
Specific (eg. 14.4.0):
  • install latest: nvm install 14.4.0
  • activate: nvm use 14.4.0
  • check installed (green arrow) : nvm ls
Include npm:

automatic switching

Expect you to create a config file (.nvmrc) per repository, as nvm does not read "engine key" from package.json as for now

enable switching

Configure your terminal

describe required version (.nvmrc)

Use the current version: node -v > .nvmrc

Or create the .nvmrc file manually




test

Steps:

  • restart your terminal
  • list available versions and choose an old one: nvm ls
  • switch to this version: nvm use <VERSION_NUMBER>
  • enter the folder with .nvmrc file, you get the following message
Found '.nvmrc' with version <13.8.0>
Now using node v13.8.0 (npm v6.13.6)

Hello, world

Create helloWorld.js

#! /usr/bin/env node
let people = 'node';
console.log("\"Hello, world ! \" says", people);

Execute node helloWorld.js , uou'll get "Hello, world ! " says node

Folder structure

SO

List:

  • source in src
  • test:
    • unit in test
    • BDD in spec
  • resources in public
    • images
    • css
    • js for client-side, not compiled

API

http

all headers are made lowercase

Entry-point

From here

If a standalone script, if you want to have a single entry-point

const mainFunction = function(people){
   console.log('Hello, ' + people + ' !');
}

if (require.main === module) {
   mainFunction('world');
}

module

Each file is a module. As each module hide its content by default (encapsulation), you should explicitly defined which element should be exposed. This is done through module's export property.

export (module.exports)

module.exports is a hook on which you can add any properties:

  • function (standard & class)
  • values (const / let)
  • object
prototype.js
const makeAgedBrie = function (agedBrieParams) {(..)};

// export
module.exports.Item = Item;
module.exports.makeAgedBrie = makeAgedBrie;

OR (shorthand)

module.exports = {
    Item,
    makeAgedBrie
}

If using exports instead of module.exports , make sure you know what you do

import (require)

Import is intended to expose another module interface into the current module, so you can call it.

prototype.test.js

// import
const {Item, makeAgedBrie} = require('./prototype');

// use
const standardItem = new Item(standardItemParams) ;

caution

require:

  • is synchronous;
  • use cache : when two module use same one, its state is shared see here
Imported object behave like a singleton if:
  • OS is case-sensitive (eg. Linux)
  • use npm 3 or higher (since 2016)
  • use same module & dependencies version
All details here

require also execute the code from the exported module.

Usually, the exported module does nothing by itself, but defining functions and exposing them in module.exports. However, the following is allowed:

  • the required module execute code (if not already imported in another module, because of cache)
  • the requiring module discard the returned reference
An example of starting an HTTP server here

Network

Allow SLL connection with self-signed certificate NODE_TLS_REJECT_UNAUTHORIZED='0' node

List:

Launch

To launch an async function on node REPL, you'll need an async IIFE

const messageBus = require('./message-bus');
( async() => {messageBus.sendEmailChange({id: 0, newEmail: '[email protected]'}) })();

One-liner: ( async() => {require('./message-bus').sendEmailChange({id: 0, newEmail: '[email protected]'}) })();

Debug

To get additional informations, use `NODE_DEBUG` environment variable:

  • NODE_DEBUG=* node : verbose
  • NODE_DEBUG=net node : HTTP call
  • NODE_DEBUG=module node : module resolution (require trace)

exit

Node should exit after executing all functions If not, you may want to use process.exit(EXIT_CODE) to force exit

Better use this package to track what prevents node to exit

⚠️ **GitHub.com Fallback** ⚠️