Node.js

An Open Source, cross-platform JavaScript runtime environment

  • event driven and asynchronous
  • can be used to develop server-side applications
  • also useful for webdev toolchains, transpiling, dev server, etc.

Intall options

  • download installer from https://nodejs.org/en
  • use package manager (e.g. HomeBrew on MacOS)
  • use nvm (Node Version Manager)

npm

  • Installed with Node.js
  • A library / registry for JavaSript software packages
    • name comes from “Node Package Manager”, but does more now
  • Command-line tools to:
    • install packages
    • manage dependencies
    • manage development environment by running scripts

Install a package from the npm library / registry

npm install <package>
  • install options
    • --save-dev: package is for development only
    • -g: install package globally (i.e. in your system)

Listed installed packages

npm list # or npm list -g . to list global packages

Initialize Node project

npm init # npm create (for Vite)

Run script

npm run <script-name>

Execute package

npm exec <package>

Update a package

npm update <package> # or npm up <package>

Node project files

package.json

  • list of all packages installed in project
  • every npm install adds to this file, often with many dependent packages as well
  • has information to re-create installed packages
    • node init: if package.json exists, installs all packages
  • add to your repo

package-lock.json

  • information to more precisely reproduce /node_modules
  • add to your repo

node_modules/

  • quickly becomes very large, 1000s of small files
  • 🔥 important to ignore node_modles/ (must be in .gitignore)
  • avoid synching with Dropbox, GDrive, etc.
  • can just delete it, then run npm install to re-install all packages

npx

Execute an arbitrary command from an npm package (either one installed locally, or fetched remotely), in a similar context as running it via npm exec

npx some-package

  • If some-package is in your path (i.e. it was installed using npm), then it runs the local version of the package
  • If some-package is not in your path (i.e. not installed), then it downloads the latest version of the package and runs it