Skip to content

Latest commit

 

History

History
executable file
·
269 lines (238 loc) · 5.78 KB

nodejs-cheat-sheet.md

File metadata and controls

executable file
·
269 lines (238 loc) · 5.78 KB

NodeJS cheat sheet

installation

tools

run different node version

run different node versions locally

sudo npm install -g n 
n ls
sudo n install 13.8
n exec 13.8 node --version
n exec 20.3 node --version
nvm install 12;
nvm use 12;
node -v

run node in docker, docker running, run node with version

node docker tags

# NODE_VERSION=16.15.0
NODE_VERSION=14.21.1-alpine
docker run --volume $PWD:/app -it node:$NODE_VERSION /bin/bash
cd /app
node --version

command line arguments

javascript REPL

node 

RUNNING YOUR CODE

# Evaluates the current argument as JavaScript
node --eval
# Checks the syntax of a script without executing it
node --check
# Opens the node.js REPL (Read-Eval-Print-Loop)
node --interactive
# Pre-loads a specic module at start-up
node --require
# Silences the deprecation warnings
node --no-deprecation
# Silences all warnings (including deprecations)
node --no-warnings
# Environment variable that you can use to set command line options
echo $NODE_OPTIONS

CODE HYGIENE

# Emits pending deprecation warnings
node --pending-deprecation
# Prints the stack trace for deprecations
node --trace-deprecation
Throws error on deprecation
node --throw-deprecation
Prints the stack trace for warnings
node --trace-warnings

INITIAL PROBLEM INVESTIGATION

# Generates node report on signal
node --report-on-signal
# Generates node report on fatal error
node --report-on-fatalerror
# Generates diagnostic report on uncaught exceptions
node --report-uncaught-exception

CONTROLLING/INVESTIGATING MEMORY USE

# Sets the size of the heap
--max-old-space-size
# Turns on gc logging
--trace_gc
# Enables heap proling
--heap-prof
# Generates heap snapshot on specied signal
--heapsnapshot-signal=signal

CPU PERFORMANCE INVESTIGATION

# Generates V8 proler output.
--prof
# Process V8 proler output generated using --prof
--prof-process
# Starts the V8 CPU proler on start up, and write the CPU prole to disk before exit
--cpu-prof

DEBUGGING

# Activates inspector on host:port and break at start of user script
--inspect-brk[=[host:]port]
# Activates inspector on host:port (default: 127.0.0.1:9229)
--inspect[=[host:]port]

npm

config

npm config ls
npm config list

npm registry

# how to set registry
# npm config set strict-ssl false
npm config set registry https://registry.npmjs.org/
npm config delete registry

or adjust environment

NPM_CONFIG_REGISTRY=https://registry.npmjs.org/

proxy

npm config set proxy [url:port]
npm config set https-proxy [url:port]

npm config get proxy
npm config get https-proxy

proxy in .npmrc

vim ~/.npmrc
proxy=http://user:[email protected]:8080/
https-proxy=http://user:[email protected]:8080/
;prefix=~/.npm-global

npm version

npm info coa
npm info coa versions

check installation

npm bin -g

package control

# best practice
# package-lock.json must have be present in root ( under git control )
npm ci

permission denied for folder /usr/lib

# create new folder where node will place all packages
mkdir ~/.npm-global

# Configure npm to use new folder
npm config set prefix '~/.npm-global'

# update your settings in ```vim ~/.profile```
export PATH=~/.npm-global/bin:$PATH

print high level packages

npm list -g --depth=0

reinstall package globally

npm search @angular

# full package name
npm uninstall -g @angular/cli
# uninstall by name 
# npm uninstall -g fx

npm cache clear --force
npm install -g @angular/cli 

show package version

npm show styled-components@* version

install package version

npm install [email protected]
# if you don't know certain version
npm install styled-components@^3.0.0

install with package registry

npm install [email protected] --registry=https://artifactory.ubs.net/artifactory/api/npm/external-npmjs-org/ --force

build project ( install dependencies )

npm install
npm start

start from different folder, start with special marker

npm start --prefix /path/to/api "special_app_marker_for_ps_aux"

start with different port

  • package.json solution
 "scripts": {"start": "PORT=3310 node ./bin/www"},
  • npm solution
PORT=$PORT npm --prefix $PROJECT_HOME/api start 
PORT=$PORT npm --prefix $PROJECT_HOME/api run start 

eject configuration to static files

npm run eject
# config/webpack.config.dev.js
# config/webpack.config.prod.js
# config/webpackDevServer.config.js
# config/env.js
# config/paths.js
# config/polyfills.js

yarn package manager

yarn config list

NextJS

npx create-next-app my-app

start nextjs

npm run-script build
npm run-script start
# or ( the same for debug )
node server.js

nextjs in real environment

nextjs-how-to