Node.js issue

npm WARN npm does not support Node.js v14.16.0 (windows)

刪除目錄

1
2
C:\Users\yourname\AppData\Roaming
目錄 npm and npm-cache

安裝npm

1
npm install -g npm@latest 

Cannot use import statement outside a module (node.js)

參考 Node.js v16.0.0 document type

package.json 加入 “type”: “module”,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"name": "node_test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"hexo": "^5.4.0",
"hexo-theme-next": "^8.3.0",
"lodash": "^4.17.21",
"mathjs": "^9.3.2"
}
}

require is not defined (node.js)

package.json 移除 “type”: “module”,

process 參數含 & 要加 “ 才不會有問題

1
2
3
// index.js
console.log("---------")
console.log(process.argv[2])
1
2
3
4
5
6
7
8
9
10
11
$ node index.js &01
[35] 3164
bash: 01: command not found

$ node index.js &
[36] 15224

$ node index.js "&01"
---------
&01
[35]+ Stopped winpty node.exe index.js

process 參數含 ! 使用 “ 會有問題 要改為 ‘

1
2
3
4
5
6
node challeng.js "lv15?token={ILOVELIdemy!!!}"
bash: !}: event not found

node challeng.js 'lv15?token={ILOVELIdemy!!!}'
還真的是我猜的那樣...不過還是要謝謝你幫我們完成這麼多任務!
......

npm install 一直處於 sill install loadAllDepsIntoIdealTree

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 不知何時設定 https-proxy, 刪除即 ok
npm config get https-proxy
http://103.253.27.108:80/
npm config get proxy
null
npm config delete https-proxy
npm config list
; "builtin" config from C:\Users\win10\AppData\Roaming\npm\node_modules\npm\npmrc
prefix = "C:\\Users\\win10\\AppData\\Roaming\\npm"
; "cli" config from command line options
omit = []
user-agent = "npm/7.6.3 node/v14.17.0 win32 x64"
; node bin location = D:\app\nodejs\node.exe
; cwd = D:\work\git\test-npm
; HOME = C:\Users\win10
; Run `npm config ls -l` to show all defaults.

node.js server 中文亂碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 'Content-Type': 'text/html;charset=utf-8' 修正中文亂碼
// demo_module.js
var http = require('http');
var dt = require('./myfirstmodule');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'});
res.write("The date and time are currently: " + dt.myDateTime());
res.end();
}).listen(8080);

// myfirstmodule.js
exports.myDateTime = function () {
return Date();
};

found vulnerabilities-漏洞

  • 未找到一致解決方法

    9 vulnerabilities (4 low, 5 moderate)
    9 high severity vulnerabilities

body-parser 被標記為棄用(body-parser as deprecated)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 原來
const express = require("express");
const bodyParser = require("body-parser");
// app
const app = express();

app.use(bodyParser.json());


// 更新
const express = require("express");
// app
const app = express();

app.use(express.json());