Skip to content

Latest commit

 

History

History
309 lines (213 loc) · 6.21 KB

File metadata and controls

309 lines (213 loc) · 6.21 KB

JavaScript

It's nothing to do with Java.

NodeJS

NodeJS or Node.js is a server-side JavaScript engine built on Google Chrome's V8 JavaScript engine that can be used for both frontend and backend development.

Uses a non-blocking, event-driven architecture, making it ideal for building scalable and high-performance applications that handle a large number of simultaneous connections.

NodeJS operates on a single-threaded event loop model, but it uses non-blocking asynchronous I/O operations to handle concurrency efficiently

Like Python, NodeJS has a vast ecosystem of libraries, which you can search at npmjs.com.

NodeJS is used for everything from web development, to APIs and microservices, to CLI tools (although there are better languages for CLI like Golang, Python, Perl and Bash).

NPM

NodeJS Package Manager.

Install it using your system package manager and then use it to install your NodeJS libraries.

brew install npm

On any Mac or Linux, from DevOps-Bash-tools:

install_packages.sh npm

NPM install package:

npm install "$package"

Remember to commit the resulting package.json and package-lock.json files to Git.

Both files as in JSON format.

  • package.json - lists packages you've installed directly
  • package-lock.json - lists the exact versions of all package dependencies so you can reproduce without annoying library version change related issues

Install all packages of the exact versions from ``packages-lock.json`:

npm install

NVM - Node Version Manager

:octocat: nvm-sh/nvm

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.2/install.sh | bash

Clones to ~/.nvm.

Reload shell to pick up the addition to .bashrc.

Show current node version:

nvm ls
->       system
iojs -> N/A (default)
node -> stable (-> N/A) (default)
unstable -> N/A (default)

List available versions:

nvm ls-remote

Install latest version of NodeJS:

nvm install node

Install specific version:

nvm install v23.11.0
nvm use node
node --version
v23.11.0
nvm ls
->     v23.11.0
         system
default -> node (-> v23.11.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v23.11.0) (default)
stable -> 23.11 (-> v23.11.0) (default)
...

REPL

To start an interactive Javascript shell, just type:

node

Then behold this horror:

> "11" + 1
'111'
> "11" - 1
10

Javascript string add minus

Which brings us on to... strict variable typing...

TypeScript

Type safe superset of JavaScript with optional static type checking.

Transpiles (converts) to JavaScript eg. src/*.ts => dist/index.js.

Not compiles since it doesn't translate to binary or byte code.

Example variable type enforcement, just suffix the : <type> to a variable:

let name: string = "Hari";

name = 1; // type error

Install TypeScript

npm install -g typescript

Convert TypeScript to JavaScript

tsc example.ts

Generates example.js.

Run it:

node example.js

TypeScript Config

Create a tsconfig.json for project settings:

tsc --init

TypeScript REPL

tsx

npm install -g tsx
tsx
Welcome to Node.js v23.7.0.
Type ".help" for more information.
> 1 + 1
2
>

ts-node

npm install -g ts-node

Start the REPL:

ts-node

List the packages:

npm ls -g ts-node typescript
/opt/homebrew/lib
└─┬ ts-node@10.9.2
  └── typescript@5.8.3

If you get an issue with REPL commands resulting in ... line continuation instead of returning the result.

ts-node
> 1 + 1
...
... ;
... ;
...

Use tsx instead.

Libraries

Repos

Readme Card

Memes

JavaScript - The Good Parts

JavaScript the Good Parts

JavaScript TypeScript Friends

JavaScript TypeScript Friends

Men Who Take Risks

Men Who Take Risks

Imported Package Tariffs

Imported Package Tariffs

Why Are You Crying Son

Why Are You Crying Son, You've Been Coding in JavaScript