top of page

Node.js Cheat Sheet

Updated: Jun 28, 2025

What is Node.js?


JavaScript runtime for running JS outside browsers. Built on Chrome's V8 engine.


For QA: Powers Cypress, Playwright, Jest, API testing tools, CI/CD scripts.


Installation


Mac

# Homebrew (preferred)
brew install node

# nvm (for version management)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
nvm install --lts

Windows

  • Download from nodejs.org (LTS version)

  • Or use Chocolatey: choco install nodejs


Verify

node --version
npm --version

Essential Commands


Node.js

node file.js          # Run JS file
node                  # Start REPL
node --version        # Check version

NPM Basics

npm init -y           # Initialize project
npm install           # Install dependencies
npm i package-name    # Install package
npm i -D package-name # Install dev dependency
npm i -g package-name # Install globally
npm uninstall package # Remove package
npm update            # Update packages
npm outdated          # Check outdated packages
npm ls                # List installed packages

NPM Scripts

npm run script-name   # Run custom script
npm start            # Run start script
npm test             # Run test script

Useful Flags

npm install --save-dev    # -D (dev dependency)
npm install --global      # -g (global install)
npm install --force       # Force reinstall
npm cache clean --force   # Clear cache

Quick Project Setup


Basic Test Project

mkdir my-tests && cd my-tests
npm init -y
mkdir tests config test-data

Cypress Setup

npm i -D cypress
npx cypress open      # First time setup
npx cypress run       # Headless tests

Playwright Setup

npm i -D @playwright/test
npx playwright install
npx playwright test
npx playwright test --ui

Sample package.json Scripts

{
  "scripts": {
    "cy:open": "cypress open",
    "cy:run": "cypress run",
    "cy:chrome": "cypress run --browser chrome",
    "pw:test": "playwright test",
    "pw:headed": "playwright test --headed",
    "pw:ui": "playwright test --ui",
    "pw:report": "playwright show-report",
    "test:all": "npm run cy:run && npm run pw:test"
  }
}

Quick Test Examples


Cypress Test

// cypress/e2e/test.cy.js
describe('Test', () => {
  it('works', () => {
    cy.visit('https://example.com')
    cy.get('h1').should('contain''Example')
  })
})

Playwright Test

// tests/test.spec.js
const { test, expect } = require('@playwright/test');

test('works'async ({ page }) => {
  await page.goto('https://example.com');
  await expect(page.locator('h1')).toContainText('Example');
});

Common Issues & Fixes


Permission Errors

sudo chown -R $(whoami) ~/.npm

Clear Everything

rm -rf node_modules package-lock.json
npm cache clean --force
npm install

Version Management

nvm list              # Show installed versions
nvm install 18.17.0   # Install specific version
nvm use 18.17.0       # Switch version
nvm alias default 18  # Set default

Useful Global Packages

npm i -g http-server    # Quick local server
npm i -g json-server    # Mock API server
npm i -g nodemon        # Auto-restart scripts

File Structure

my-project/
├── package.json
├── package-lock.json
├── node_modules/
├── tests/
├── cypress/
├── config/
└── test-data/

Quick Commands Reference

# Package management
npm i package@1.2.3   # Specific version
npm i user/repo        # From GitHub
npm info package       # Package info
npm search keyword     # Search packages

# Running things
node file.js           # Run file
npm run dev           # Run dev script
npm test              # Run tests

# Maintenance
npm audit             # Security audit
npm audit fix         # Fix vulnerabilities
npm outdated          # Check updates
npm update            # Update all

 
 
bottom of page