ND

Node.js Cheatsheet

Node.js and npm essentials - package management commands and the core modules used in most projects.

npm commands

npm init -y

Create a package.json with defaults

npm install <pkg>

Install a package (saves to dependencies)

npm install -D <pkg>

Install as a dev dependency

npm run <script>

Run a script defined in package.json

npm uninstall <pkg>

Remove a package

npx <pkg>

Run a package without installing it globally

Core modules

const fs = require("fs")

File system module

fs.readFileSync(path, "utf8")

Read a file synchronously

const path = require("path")

Path manipulation utilities

process.env.VAR_NAME

Read an environment variable

process.argv

Command-line arguments

Modules (ESM)

import x from "module"

Import a default export

import { x } from "module"

Import a named export

export default function() {}

Default export

export const x = 1

Named export