npm 說明
Commans
Dump version
1 | npm -v |
ping npmjs registry
1 | npm ping |
Dump all npm installed models
1 | npm list |
檢查 node_modules 有相關資安漏洞
1 | npm audit |
自動修正相關漏洞
1 | npm audit fix |
更新可更新的 node_modules
1 | npm update |
清理 node_modules 中不需要的檔案
1 | npm prune |
install module
1 | # 安裝最新版本: |
npx : 執行 project 安裝 module
1 | npx jest |
專案 npm 初始化
1 | # 會要求你輸入關於這個專案的相關資訊 |
本地、本機安裝(共用 module)
1 | npm install -g hexo-cli |
專案 modle 安裝
1 | # dependencies (依賴套件): 專案 production or build 之後仍然會使用的套件,例如 jQuery、swiper 等等套件 |
移除套件
1 | npm uninstall -g [套件名稱] |
還原專案套件
1 | npm install |
run script
1 | // index.js |
package.json
1 | { |
1 | $ npm run start |
module version
package.json
- ^version: Compatible with version
鎖住第一碼(即A) 不得變更。如^1.2.2,則安裝範圍是>=1.2.2 且 <2.0.0。即須符合1.*.*。 - ∼version: Approximately equivalent to version
鎖住第二碼(即B) 不得變更。如∼1.2.2,則安裝範圍是>=1.2.2且<1.3.0。即須符合1.2.*。
1 | { |
安裝特定版本
查詢最後版本
1
2npm info express-validator version
6.13.0查詢所有版本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22npm view express-validator versions
[
'0.1.0', '0.1.1', '0.1.2', '0.1.3', '0.2.0', '0.2.1',
'0.2.2', '0.2.3', '0.2.4', '0.3.0', '0.3.1', '0.3.2',
'0.4.0', '0.4.1', '0.5.0', '0.6.0', '0.7.0', '0.8.0',
'1.0.0', '1.0.1', '2.0.0', '2.1.0', '2.1.1', '2.1.2',
'2.2.0', '2.3.0', '2.4.0', '2.5.0', '2.6.0', '2.7.0',
'2.8.0', '2.9.0', '2.9.1', '2.10.0', '2.11.0', '2.12.0',
'2.12.1', '2.12.2', '2.13.0', '2.14.0', '2.14.1', '2.14.2',
'2.15.0', '2.15.1', '2.16.0', '2.17.0', '2.17.1', '2.18.0',
'2.19.0', '2.19.1', '2.19.2', '2.20.1', '2.20.2', '2.20.3',
'2.20.4', '2.20.5', '2.20.6', '2.20.7', '2.20.8', '2.20.9',
'2.20.10', '2.21.0', '3.0.0', '3.1.0', '3.1.1', '3.1.2',
'3.1.3', '3.2.0', '3.2.1', '4.0.0', '4.1.0', '4.1.1',
'4.2.0', '4.2.1', '4.3.0', '5.0.0', '5.0.1', '5.0.2',
'5.0.3', '5.1.0', '5.1.1', '5.1.2', '5.2.0', '5.3.0',
'5.3.1', '6.0.0', '6.0.1', '6.1.0', '6.1.1', '6.2.0',
'6.3.0', '6.3.1', '6.4.0', '6.4.1', '6.5.0', '6.6.0',
'6.6.1', '6.7.0', '6.8.0', '6.8.1', '6.8.2', '6.9.0',
'6.9.1', '6.9.2', '6.10.0', '6.10.1', '6.11.0', '6.11.1',
'6.12.0', '6.12.1', '6.12.2', '6.13.0'
]查詢現在安裝 ndoe module 版本
1
2
3
4
5
6
7
8
9
10
11npm list
express@1.0.0 D:\work\git\li\project\ecommerce\express
+-- body-parser@1.19.0
+-- cookie-parser@1.4.5
+-- dotenv@10.0.0
+-- express@4.17.1
+-- mongodb@4.1.4
+-- mongoose@6.0.12
+-- morgan@1.10.0
+-- nodemon@2.0.14
`-- uuid@8.3.2安裝特定版號 ndoe module
1
2# npm install [package-name]@[version-number]
npm install renovate@20.5.1
Unit test
call function test
1 | // index.js |
1 | $ node index.js |
jest test
example
jest 會自動找 xxx.test.js 測試
1 | // index.js |
1 | // index.test.js |
1 | $ npx jest |
test by script
1 | "scripts": { |
1 | $ yarn run test |
test multi items
1 | // index.test.js |
1 | $ npm run test |
TDD (Test-Driven Development) 測試驅動開發
先寫 test pattern 再寫 code
npm's node module
mathjs
1 | // npm install mathjs |
lodash reference document
1 | // npm install lodash |
left-pad
1 | // index.js |
Request - Simplified HTTP client
1 | npm install request |
1 | const request = require('request'); |
1 | $ node request_1.js |
axios - Promise based HTTP client for the browser and node.js
1 | npm install axios |
NODE_ENV [dotenv] - 環境變數集中在 env 檔
1 | npm install dotenv --save |
.env
1 | CHRIS=chris |
app.js
1 | require('dotenv').config(); |
jest
1 | yarn add --dev jest |
babel-node
- 模擬 ES6 執行
- 效率不好僅測試時使用
- 使用 import 也是要設 “type”: “module”
install
1 | npm install --save-dev @babel/core @babel/node |
add config file - .babelrc
1 | { |
test
1 | // utils.js |
1 | // test1.js |
1 | $ npx babel-node test1.js |
request-debug : monitor HTTP(S) requests performed by the request module
1 | // npm install request-debug |
1 | $ node challeng-2.js |
dotenv : env 設定
.env
1 | PORT=8 |
*.js
1 | require("dotenv").config(); |
nodemon : node 修改自動重啟
1 | "scripts": { |
uuid : create uuid(Universally Unique Identifier) 通用唯一辨識碼
1 | // v4 uuid |
morgan : HTTP request logger middleware for node.js
print log in console for debug
1 | // dev - status 會 show 不同顏色 |
cookie : express 有提供,不需要用cookie-parser :
1 | // set cookie |
express-validator@5.3.1 : express.js validate middlewares
- 6.x 會有 “typeError: express Validator is not a function” error
- document 參考 6.x express-validator
- ./app.js
1
2
3
4
5
6// ./app.js
// express-validator
const expressValidator = require("express-validator");
// app
const app = express();
app.use(expressValidator()); // express-validator - ./routes/user.js
1
2
3
4
5// ./routes/user.js
// valid
const { userSignupValidator } = require("../validator/index");
router.post("/signup", userSignupValidator, signup); - ./validator/index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26// ./validator/index.js
exports.userSignupValidator = (req, res, next) => {
req.check("name", "Name is required").notEmpty();
req
.check("email", "Email must be between 3 to 32 characters")
.matches(/.+\@.+\..+/)
.withMessage("Email must contain @")
.isLength({
min: 4,
max: 32,
});
req.check("password", "Password is required").notEmpty();
req
.check("password")
.isLength({ min: 6 })
.withMessage("password must conatin at least 6 characters")
.matches(/\d/)
.withMessage("Password must contain a number");
const errors = req.validationErrors();
// console.log(errors);
if (errors) {
const firstError = errors.map((error) => error.msg)[0];
return res.status(400).json({ error: firstError });
}
next();
};
express-jwt : 驗證 JWT
jsonwebtoken : 產生 JWT token
1 | const jwt = require("jsonwebtoken"); // to generate signed token |
crypto : node.js 提供,加密編碼
formidable : parsing data, 特別是有關 file
1 | const formidable = require("formidable"); |
cors : enable cors
1 | // Simple Usage (Enable All CORS Requests) |
query-string:query string 處理
1 | const queryString = require('query-string'); |
moment: script data library
1 | // show 距現在多久 |
braintree : Braintree Node library
1 | var braintree = require('braintree'); |
npm config
1 | # list config |