Sun, Oct 24, 21, install nvm node npm and other packages, covered by Web Development with Node and Express by Ethan Brown
This is a draft, the content is not complete and of poor quality!

book nodejs npm

web development with node and express

As of version of 5 of npm, an additional file, package-lock.json, will be created. Whereas package.json can be “loose” in its specification of dependency versions (with the ^ and ~ version modifiers), package-lock.json records the exact versions that were installed, which can be helpful if you need to re-create the exact dependency versions in your project. I recommend you check this file into source control and don’t modify it by hand. See the package-lock.json documentation for more information.
Beaware Node modules (also called CJS) use a different syntax than ECMAScript modules(ESM), and you may have to switch between the two syntaxes when you go between frontend and backend code. It’s a good idea to be familiar with both.
goodPoint! Whenever you have a dependency, you have something that needs to be mocked(simulated) for effective testing. For example, our primary dependency is Express, which is already thoroughly tested, so we don’t need or want to test Express itself, just how we use it. The only way we can determine if we’re using Express correctly is tosimulate Express itself.

Ref: meet puppeteer.md

creating a simple test code

Eslint

  • Broadly speaking, a linting error has one of three causes: • It’s a legitimate problem, and you should fix it. It may not always be obvious, in which case you may need to refer to the ESLint documentation for the particular error. • It’s a rule you don’t agree with, and you can simply disable it. Many of the rules in ESLint are a matter of opinion. I’ll demonstrate disabling a rule in a moment. • You agree with the rule, but there’s an instance where it’s infeasible or costly to fix in some specific circumstance. For those situations, you can disable rules for only specific lines in a file, which we’ll also see an example of.

Thi

Install NodeJS & NPM

Install multiple versions

First, need to install nvm. Run the line of curl and then run/add-to-zsh the line of export.

::: warning Below commands are mostly for Linux/MacOS users. :::

::: col-2-equal

# FIRST INSTALL: the most recent lts release
nvm install --lts
# install a specific version
nvm install 12.13.0
# install latest version
nvm install node
# list all installed versions
nvm ls
# set default version of node
nvm alias default 12.13.0
# full list of available versions
# be careful, it's too long!!!
nvm ls-remote
# switch between versions
nvm use 12.13.0
# or (more quickly)
nvm use v15
# uninstall some version
nvm uninstall 12.13.0

:::

Single version

:point_right: Install NodeJS and NPM: Windows & MacOS, Linux.

::: col-2-equal

# UPDATE npm
npm cache clean -f # clear the cache first
sudo npm install -g npm
# UPDATE node
sudo npm install -g n
sudo n stable
# refresh the shell
source ~/.zshrc # if using zsh
source ~/.bashrc # is using bash
# Check version
npm -v
node -v

:::

Shorthand CLI options

  • i: install
  • -D: --save-dev (devDependencies)
  • -P: --save-prod (default), --save
  • -g: --global
  • -f: --force
  • ls: list

Install package

👉 Official documentation.

npm install package_name # if it's in package.json, the indicated version will be installed
                         # otherwise, the newsest version will be installed
npm install --global package_name # global package
# install all package in package.json
npm install
# install + save to package.json
npm install --save package_name # save to package.json
npm install --save-dev package_name # save to package.json, in devDependencies
npm install --no-save package_name # don't save
# install with version
npm install express@4.16.1
# install a local package
npm install /path/to/package
# from github repository
npm i git+https://github.com/abc/xyz.git # https
# or
npm i git+ssh://git@github.com/abc/xyz.git # ssh
# list all installed packages (current project only)
ls node_modules
# list all local (installed) packages
npm list # -g for globel # or use "ls"
npm list --depth=0 # without dependencies

# Check the current version of a (installed) package
npm list package_name # with "-g" for global

# Check the latest (not current) version of a package
npm view package_name version
# Set python2 by default when installing npm packages
npm config set python python2

Update package

::: col-2-equal

# which global packages need to be updated?
npm outdated -g --depth=0

# update all global packages
npm update -g
# update a package
npm update package_name # -g for global

:::

Remove package

npm uninstall package

Run scritps

# Install first
npm i --save npm-run-all

::: col-2-equal

// Run sequentially,
// package.json
"scripts": {
	"build": "run-s prod:*", // "run-s" = "npm-run-all -s"
	"prod:eleventy": "eleventy",
	"prod:parcel": "parcel build ./ -o ./",
}
// Run parallely,
// package.json
"scripts": {
	"start": "npm-run-all --parallel dev:*",
	"dev:eleventy": "eleventy --serve",
	"dev:parcel": "parcel watch ./ -o ./",
}

:::

simple test

간단한 자바스크립트 테스트 코드 만들어보기

간단한 테스트 코드 작성을 위해서 아래와 같이 두 숫자의 합을 구하는 함수를 정의합니다.

function sum(a, b) {
	return a + b;
}

이제 이 함수의 결과를 확인하는 테스트 코드를 작성해보겠습니다. 함수의 결과 값을 result라 하고, 기대 값을 expected라고 하겠습니다.

var result = sum(1, 2);
var expected = 5;

if (result !== expected) {
	throw new Error(result + ' is not equal to ' + expected);
}

두 개의 합을 더한 결과(result)는 3이고 기대 값(expected)는 5이기 때문에 아래와 같은 오류가 발생합니다.

error

간단한 테스트 함수 만들어보기

앞의 코드를 API 형태로 사용할 수 있게 함수로 변환해보겠습니다. 아래의 API 형태는 일반적인 테스트 라이브러리에서 흔하게 찾아볼 수 있습니다.

expect(result).toBe(expected)

위와 같은 API를 사용하기 위해 앞에서 살펴본 테스트 코드를 expect()라는 함수에 포함합니다. 테스트 함수의 코드는 다음과 같습니다.

function sum(a, b) {
	return a + b;
}

function expect(result) {
  return {
    toBe: function(expected) {
      if (result !== expected) {
      	throw new Error(result + ' is not equal to ' + expected);
      }
    }
  }
}

위의 함수를 아래와 같이 실행하면 아까와 같이 동일한 에러가 발생합니다.

expect(sum(1,2)).toBe(5);

image

그럴싸한 테스트 함수 만들어보기

앞에서 작성한 테스트 함수는 몇 번째 줄에서 오류가 났는지 추적하기가 어렵다는 단점이 있습니다. 또한, 각 테스트 함수의 역할이 구분되지 않죠. 이번엔 좀 더 의미 있는 테스트 함수를 작성해보겠습니다.

function test(title, testCode) {
  try {
    testCode();
  } catch (error) {
    console.error(error);
  }
}

function expect(result) {
  return {
    toBe: function(expected) {
      if (result !== expected) {
      	throw new Error(result + ' is not equal to ' + expected);
      }
    }
  }
}

위 코드의 모양은 전형적인 테스트 라이브러리의 API 형태와 비슷합니다. 위 함수를 이용하면 다음과 같이 테스트를 할 수 있습니다.

test('sum(1, 2) is not equal 5', function() {
  expect(sum(1, 2)).toBe(5);
});

앞의 예제와 마찬가지로 동일한 오류를 발생시키지만 이번엔 좀 더 추적하기가 쉽습니다. image

The following wiki, pages and posts are tagged with

Title Type Excerpt
Nodejs and NPM install on different platforms post Sun, Oct 24, 21, install nvm node npm and other packages, covered by Web Development with Node and Express by Ethan Brown
serverless application with nodejs post Thu, Nov 25, 21, three-tier architecture in user interface(presentation), functional process logic(business rules), computer data storage/access
node api firebase page summary.