# JavaScript - Setup NodeJS
###### tags: `JavaScript` `NodeJS` `Windows`
# Reference
* [How to Install Node.js and Create a Local Development Environment](https://www.digitalocean.com/community/tutorial_series/how-to-install-node-js-and-create-a-local-development-environment)
* [How To Install Node.js on Ubuntu 18.04](https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-18-04)
---
# Install NodeJS on the Mac OS
```
# Update Homebrew
brew update
# Install Node.js (includes npm)
brew install node
# Verify installation
node -v
npm -v
# Optional
brew cleanup
```
---
# [在 Windows 上安裝 NodeJS](https://docs.microsoft.com/zh-tw/windows/dev-environment/javascript/nodejs-on-windows)
## 安裝 nvm-windows、node.js 和 npm
### [coreybutler / nvm-windows](https://github.com/coreybutler/nvm-windows#node-version-manager-nvm-for-windows)
#### 2021/12/11 v1.1.9
https://github.com/coreybutler/nvm-windows/releases
1. 下載最新版本的 nvm-setup.zip 檔案。
2. 下載之後,開啟 zip 檔案,然後開啟 nvm-setup.exe 檔案。
3. NVM Windows 安裝 wizard 將逐步引導您完成設定步驟,包括選擇要安裝 NVM Windows 和 Node.js 的目錄。


```
nvm list
No installations recognized.
```
```
nvm list available
| CURRENT | LTS | OLD STABLE | OLD UNSTABLE |
|--------------|--------------|--------------|--------------|
| 17.4.0 | 16.13.2 | 0.12.18 | 0.11.16 |
| 17.3.1 | 16.13.1 | 0.12.17 | 0.11.15 |
| 17.3.0 | 16.13.0 | 0.12.16 | 0.11.14 |
| 17.2.0 | 14.18.3 | 0.12.15 | 0.11.13 |
| 17.1.0 | 14.18.2 | 0.12.14 | 0.11.12 |
| 17.0.1 | 14.18.1 | 0.12.13 | 0.11.11 |
| 17.0.0 | 14.18.0 | 0.12.12 | 0.11.10 |
| 16.12.0 | 14.17.6 | 0.12.11 | 0.11.9 |
| 16.11.1 | 14.17.5 | 0.12.10 | 0.11.8 |
| 16.11.0 | 14.17.4 | 0.12.9 | 0.11.7 |
| 16.10.0 | 14.17.3 | 0.12.8 | 0.11.6 |
| 16.9.1 | 14.17.2 | 0.12.7 | 0.11.5 |
| 16.9.0 | 14.17.1 | 0.12.6 | 0.11.4 |
| 16.8.0 | 14.17.0 | 0.12.5 | 0.11.3 |
| 16.7.0 | 14.16.1 | 0.12.4 | 0.11.2 |
| 16.6.2 | 14.16.0 | 0.12.3 | 0.11.1 |
| 16.6.1 | 14.15.5 | 0.12.2 | 0.11.0 |
| 16.6.0 | 14.15.4 | 0.12.1 | 0.9.12 |
| 16.5.0 | 14.15.3 | 0.12.0 | 0.9.11 |
| 16.4.2 | 14.15.2 | 0.10.48 | 0.9.10 |
```
```
nvm install 16.13.2
Downloading node.js version 16.13.2 (64-bit)...
Extracting...
Complete
Installation complete. If you want to use this version, type
nvm use 16.13.2
```
```
nvm use 16.13.2
exit status 1: �s���Q�ڡC
```
https://blog.csdn.net/qq_41715885/article/details/120449480
vm use 版本报错,出现 __exit status 1后面跟一堆乱码__
此时是因为无权限,打开c盘C:\Windows\System32找到cmd
以管理员身份打开,再次使用nvm use 版本报错命令,成功切换
```
nvm ls
* 16.13.2 (Currently using 64-bit executable)
```
```
nvm list
* 16.13.2 (Currently using 64-bit executable)
```
uninstall:
```
nvm uninstall 16.13.2
```
```
node -v
v16.13.2
```
```
npm -v
8.1.2
```
```
npm install -g yarn
added 1 package, and audited 3 packages in 2s
found 0 vulnerabilities
npm notice
npm notice New minor version of npm available! 8.1.2 -> 8.4.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.4.0
npm notice Run npm install -g npm@8.4.0 to update!
npm notice
```
```
yarn add electron --dev --exact
```
---
# [教學課程:適用於初學者的 Node.js](https://docs.microsoft.com/zh-tw/windows/dev-environment/javascript/nodejs-beginners-tutorial)
## 嘗試搭配 Visual Studio Code 使用 NodeJS
如果您尚未安裝 Visual Studio Code,請返回上述的必要條件一節,
並遵循針對 Windows 或 WSL 連結的安裝步驟。
1. 開啟命令列並建立新目錄: ``mkdir HelloNode``,
然後輸入目錄: ``cd HelloNode``
2. 建立名為 ``app.js`` 的 JavaScript 檔案,並在其中包含名為 ``msg`` 的變數:
```
echo var msg > app.js
```
3. 使用下列命令,在 VS Code 中開啟目錄和您的 ``app.js`` 檔案:code .
4. 新增簡單的字串變數 ("Hello World"),然後在您的 ``app.js`` 檔案中輸入此字串,
以將字串的內容傳送至您的主控台:
```
var msg = 'Hello World';
console.log(msg);
```
5. 使用 Node.js 執行 ``app.js`` 檔案。
選取 [ Viewterminal ] (或選取 Ctrl + ' (使用倒引號字元) ),
在 VS Code 內部開啟您的終端機。 如果您需要變更預設終端機,
選取下拉式功能表,然後選擇 [選取預設殼層]。
6. 在終端機中輸入``node app.js``。 您應該會看到以下輸出:"Hello World"。
---
```
npm install && npm start
npm WARN old lockfile
npm WARN old lockfile The package-lock.json file was created with an old version of npm,
npm WARN old lockfile so supplemental metadata must be fetched from the registry.
npm WARN old lockfile
npm WARN old lockfile This is a one-time fix-up, please be patient...
npm WARN old lockfile
added 86 packages, and audited 87 packages in 43s
5 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
> electron-quick-start@1.0.0 start
> electron .
```
Hello World!
We are using Node.js 16.9.1, Chromium 96.0.4664.110, and Electron 16.0.8.
https://github.com/electron/electron-quick-start
---
# Update Node JS
## Ubuntu
* [How can I update my nodeJS to the latest version?](https://askubuntu.com/questions/426750/how-can-i-update-my-nodejs-to-the-latest-version)
* [Why n throws error? Error: sudo required (or change ownership, or define N_PREFIX)](https://stackoverflow.com/questions/61677951/why-n-throws-error-error-sudo-required-or-change-ownership-or-define-n-prefi)
```
sudo npm cache clean -f
npm install -g npm
npm install -g n
sudo mkdir -p /usr/local/n
sudo chown -R $(whoami) /usr/local/n
n install 16.17.0
```
## Mac OS
#### [Upgrade Node.js to the latest version on Mac OS](https://stackoverflow.com/questions/11284634/upgrade-node-js-to-the-latest-version-on-mac-os)
``sudo npm cache clean -f`` (force) clear you npm cache
``sudo npm install -g n`` install n (this might take a while)
``sudo n stable`` upgrade to the current stable version
``
sudo npm i -g npm@latest
``
---
# NPM 指令
### 安裝 module:
``npm install`` or ``npm i``
``--save-dev`` 與 ``--save``
分別把 module 置入到 ``package.json`` 中的
"devDependencies" 與 "dependencies"
```
npm install --save @types/google-apps-script
npm install --save-dev eslint typescript @typescript-eslint/parser @typescript-eslint/eslint-plugin
```
### 瀏覽安裝的 modules
``npm list`` 是看目前安裝的套件
```
project.gas.erp.sunlike.dicon@0.9.11 D:\GitHub\KofIvAppsScript\012_GAS
├── @types/google-apps-script@1.0.53
├── @typescript-eslint/eslint-plugin@5.33.1
├── @typescript-eslint/parser@5.33.1
├── eslint@8.22.0
└── typescript@4.7.4
```
``-g`` 代表 global
像 ``npm i @google/clasp -g``
會把 clasp 安裝到 global 中
```
npm list -g
C:\Program Files\nodejs -> .\
├── @google/clasp@2.4.1
├── corepack@0.10.0
├── npm@8.4.1
└── sql-cli@0.6.2
```
#### alternative:
```
npm ls -g
npm ls
```
e.g.,:
```
PS C:\Users\elite> npm ls -g
C:\Program Files\nodejs -> .\
+-- @angular/cli@17.0.8
+-- corepack@0.22.0
`-- npm@10.2.5
```
### 移除 Module
``npm un ``
```
npm un @google/clasp -g
PS D:\GitHub\KofIvAppsScript\012_GAS> npm list -g
C:\Program Files\nodejs -> .\
├── corepack@0.10.0
├── npm@8.4.1
└── sql-cli@0.6.2
```
### Local dependency
``npm init`` 建立 ``package.json``
```
{
"name": "typescript.study.project1",
"version": "1.0.0",
"description": "For TypeScript Study and Learning",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"study",
"typescript"
],
"author": "elite_lin <elite_lin@goglobal.com.tw>",
"license": "ISC"
}
```
---
## [npx @ npm](https://www.npmjs.com/package/npx)
Executes ``<command>`` either from a local ``node_modules/.bin``,
or from a central cache, __installing any packages needed in order for ``<command>`` to run__.
By default, __``npx`` will check whether ``<command>`` exists in ``$PATH``__,
or in the local project binaries, and execute that.
__If ``<command>`` is not found, it will be installed prior to execution__.
```
$ npx bower
Need to install the following packages:
bower
Ok to proceed? (y)
Usage:
bower <command> [<args>] [<options>]
Commands:
```
### 觀察
npx 有另一個好處: 若沒安裝, 會安裝在 local cache 中, 然後執行
---
# [npm-check-updates @ npm](https://www.npmjs.com/package/npm-check-updates/v/2.11.1)
npm-check-updates is a command-line tool that allows you to upgrade your package.json or bower.json dependencies to the latest versions, regardless of existing version constraints.
npm-check-updates maintains your existing semantic versioning policies, i.e., it will upgrade your "express": "^4.11.2" dependency to "express": "^5.0.0" when express 5.0.0 is released.
## Installation
```
npm install -g npm-check-updates
```
## Upgrade
https://docs.npmjs.com/try-the-latest-stable-version-of-npm
```
npm install -g npm@latest
```
## Usage
Show any new dependencies for the project in the current directory:
```
$ ncu
No package.json
Please add a package.json to the current directory, specify the --packageFile or --packageData options, or pipe a package.json to stdin.
```
```
$ ncu
Checking D:\TypeScriptProjects\TSProj001\package.json
[====================] 5/5 100%
@typescript-eslint/eslint-plugin ^5.10.2 → ^5.34.0
@typescript-eslint/parser ^5.10.2 → ^5.34.0
eslint ^8.8.0 → ^8.22.0
typescript ^4.5.5 → ^4.7.4
Run ncu -u to upgrade package.json
```
Run with ``-u`` to upgrade your ``package.json``
Upgrade a project's package file:
__Make sure your package file is in version control
and all changes have been committed. This will overwrite your package file__.
```
$ ncu -u
Upgrading D:\TypeScriptProjects\TSProj001\package.json
[====================] 5/5 100%
@typescript-eslint/eslint-plugin ^5.10.2 → ^5.34.0
@typescript-eslint/parser ^5.10.2 → ^5.34.0
eslint ^8.8.0 → ^8.22.0
typescript ^4.5.5 → ^4.7.4
```
Works with ``mysql``:
```
$ ncu -u mysql
Upgrading D:\TypeScriptProjects\TSProj001\package.json
[====================] 1/1 100%
All dependencies match the latest package versions :)
```
---
### [How to update each dependency in package.json to the latest version?](https://stackoverflow.com/questions/16073603/how-to-update-each-dependency-in-package-json-to-the-latest-version)
I'm not sure if should involve
``npm install -g npm-check-updates`` first
but I'm sure the command ``ncu -u`` can update the ``package.json``:
```
ncu -u
Upgrading D:\Proj_Srcs_js\ElectronFiilexTool\package.json
[====================] 4/4 100%
electron ^20.1.1 → ^25.3.2
node-gyp ^9.1.0 → ^9.4.0
serialport ^10.4.0 → ^11.0.1
Run npm install to install new versions.
```
---
[What is the difference between "npm install" and "npm ci"?](https://stackoverflow.com/questions/52499617/what-is-the-difference-between-npm-install-and-npm-ci)
[Difference between npm i and npm ci in Node.js](https://www.geeksforgeeks.org/difference-between-npm-i-and-npm-ci-in-node-js/)