# 關於 Node.js
早上去市場時經過圖書館把 "不一樣的 Node.js" 這本書借回來, 這本書寫得不錯, 非常適合初學者, 已經出第二版了 (2015) :
Source : Taaze
檢查我的 Raspbian Pixel 發現已經安裝 Node.js :
pi@raspberrypi:~ $ node -v
v0.10.29
但官網可下載的 Windows 版本卻是 9.3.0 版 :
# https://nodejs.org/en/
會不會差太多啊! 我參考下面這篇試圖去更新 Node.js :
# Upgrading to more recent versions of Node.js on the Raspberry Pi
首先是更新套件清單 :
pi@raspberrypi:~ $ sudo apt update
Get:1 http://mirrordirector.raspbian.org jessie InRelease [14.9 kB]
Get:2 http://archive.raspberrypi.org jessie InRelease [22.9 kB]
Get:3 http://mirrordirector.raspbian.org jessie/main armhf Packages [9,536 kB]
Get:4 http://archive.raspberrypi.org jessie/main armhf Packages [171 kB]
Get:5 http://archive.raspberrypi.org jessie/ui armhf Packages [58.9 kB]
Get:6 http://archive.raspberrypi.org jessie/ui armhf Packages [58.9 kB]
Ign http://archive.raspberrypi.org jessie/main Translation-en_US
Ign http://archive.raspberrypi.org jessie/main Translation-en
Ign http://archive.raspberrypi.org jessie/ui Translation-en_US
Ign http://archive.raspberrypi.org jessie/ui Translation-en
Get:7 http://mirrordirector.raspbian.org jessie/contrib armhf Packages [43.3 kB]
Get:8 http://mirrordirector.raspbian.org jessie/non-free armhf Packages [84.2 kB]
Get:9 http://mirrordirector.raspbian.org jessie/rpi armhf Packages [1,356 B]
Ign http://mirrordirector.raspbian.org jessie/contrib Translation-en_US
Ign http://mirrordirector.raspbian.org jessie/contrib Translation-en
Ign http://mirrordirector.raspbian.org jessie/main Translation-en_US
Ign http://mirrordirector.raspbian.org jessie/main Translation-en
Ign http://mirrordirector.raspbian.org jessie/non-free Translation-en_US
Ign http://mirrordirector.raspbian.org jessie/non-free Translation-en
Ign http://mirrordirector.raspbian.org jessie/rpi Translation-en_US
Ign http://mirrordirector.raspbian.org jessie/rpi Translation-en
Fetched 9,916 kB in 2min 41s (61.5 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
198 packages can be upgraded. Run 'apt list --upgradable' to see them.
然後檢查套件清單中最新的 Node.js 版本 :
pi@raspberrypi:~ $ apt list nodejs
Listing... Done
nodejs/oldstable,now 0.10.29~dfsg-2 armhf [installed,automatic]
咦? 跟我已安裝的一樣都是 0.10.29? 不管它, 直接下 install 指令去更新 Node.js (已安裝的話就是更新) :
pi@raspberrypi:~ $ sudo apt install nodejs
Reading package lists... Done
Building dependency tree
Reading state information... Done
nodejs is already the newest version.
nodejs set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 198 not upgraded.
pi@raspberrypi:~ $
還真的已經是最新版, 原來樹莓派最新的套件清單還是 0.10.29 版啊! 如果要更新到 9.3.0 版可能要用該文後面那個 curl 指令去下載官網最新版本 :
$ curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
然後再安裝 9.3.0 版了 :
不過我決定暫時不升版, 目前這樣夠用就好, 以後有需要再升.
參考 :
# Installing newer version of NodeJS on Pi 3
2018-01-02 補充 :
今天我用高雄家裡的 RPI B 執行了上面 curl 指令失敗, 結果如下 :
pi@raspberrypi:~ $ curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
## Installing the NodeSource Node.js v9.x repo...
## You appear to be running on ARMv6 hardware. Unfortunately this is not currently supported by the NodeSource Linux distributions. Please use the 'linux-armv6l' binary tarballs available directly from nodejs.org for Node.js v4 and later.
意思好像是 Node.js v9 還未支援 ARMv6 核心.
2018-01-05 補充 :
今日有熱心的 Node.js 先進留言指教, 原來上面我找到的方法不管用, 網友提供的更新方法我複製整理如下 :
1. 下載
wget https://nodejs.org/dist/latest-v9.x/node-v9.3.0-linux-armv6l.tar.gz
2. 解壓縮
tar -xvf node-v9.3.0-linux-armv6l.tar.gz
3. 切換至資料夾
cd node-v9.3.0-linux-armv6l
4. 移動至/usr/local/
sudo cp -R * /usr/local/
5. 設定NODE_PATH變數(.bashrc)
export NODE_PATH=/usr/local/lib/node_modules
6. 檢查版本
node -v
npm -v
我依此步驟進行, 果真將 Node.js 升版至 v9.3.0, 實在太感謝這位匿名的朋友啦!
升版過程節錄如下 :
pi@raspberrypi:~ $ wget https://nodejs.org/dist/latest-v9.x/node-v9.3.0-linux-ar
mv6l.tar.gz
--2018-01-05 19:21:38-- https://nodejs.org/dist/latest-v9.x/node-v9.3.0-linux-armv6l.tar.gz
Resolving nodejs.org (nodejs.org)... 104.20.22.46, 104.20.23.46, 2400:cb00:2048:1::6814:172e, ...
Connecting to nodejs.org (nodejs.org)|104.20.22.46|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 16930799 (16M) [application/gzip]
Saving to: node-v9.3.0-linux-armv6l.tar.gz
pi@raspberrypi:~/node-v9.3.0-linux-armv6l $ sudo cp -R * /usr/local/
pi@raspberrypi:~/node-v9.3.0-linux-armv6l $ export NODE_PATH=/usr/local/lib/node_modules
pi@raspberrypi:~/node-v9.3.0-linux-armv6l $ node -v
v9.3.0
pi@raspberrypi:~/node-v9.3.0-linux-armv6l $ npm -v
5.5.1
pi@raspberrypi:~/node-v9.3.0-linux-armv6l $ cd ..
pi@raspberrypi:~ $ node
> console.log("Hello World");
Hello World
undefined (為啥會有這個東東?Windows 上沒有)
參考 :
# Installing newer version of NodeJS on Pi 3
2018-01-02 補充 :
今天我用高雄家裡的 RPI B 執行了上面 curl 指令失敗, 結果如下 :
pi@raspberrypi:~ $ curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
## Installing the NodeSource Node.js v9.x repo...
## You appear to be running on ARMv6 hardware. Unfortunately this is not currently supported by the NodeSource Linux distributions. Please use the 'linux-armv6l' binary tarballs available directly from nodejs.org for Node.js v4 and later.
意思好像是 Node.js v9 還未支援 ARMv6 核心.
2018-01-05 補充 :
今日有熱心的 Node.js 先進留言指教, 原來上面我找到的方法不管用, 網友提供的更新方法我複製整理如下 :
1. 下載
wget https://nodejs.org/dist/latest-v9.x/node-v9.3.0-linux-armv6l.tar.gz
2. 解壓縮
tar -xvf node-v9.3.0-linux-armv6l.tar.gz
3. 切換至資料夾
cd node-v9.3.0-linux-armv6l
4. 移動至/usr/local/
sudo cp -R * /usr/local/
5. 設定NODE_PATH變數(.bashrc)
export NODE_PATH=/usr/local/lib/node_modules
6. 檢查版本
node -v
npm -v
我依此步驟進行, 果真將 Node.js 升版至 v9.3.0, 實在太感謝這位匿名的朋友啦!
升版過程節錄如下 :
pi@raspberrypi:~ $ wget https://nodejs.org/dist/latest-v9.x/node-v9.3.0-linux-ar
mv6l.tar.gz
--2018-01-05 19:21:38-- https://nodejs.org/dist/latest-v9.x/node-v9.3.0-linux-armv6l.tar.gz
Resolving nodejs.org (nodejs.org)... 104.20.22.46, 104.20.23.46, 2400:cb00:2048:1::6814:172e, ...
Connecting to nodejs.org (nodejs.org)|104.20.22.46|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 16930799 (16M) [application/gzip]
Saving to: node-v9.3.0-linux-armv6l.tar.gz
(略)
pi@raspberrypi:~ $ tar -xvf node-v9.3.0-linux-armv6l.tar.gz
(略)
pi@raspberrypi:~ $ cd node-v9.3.0-linux-armv6lpi@raspberrypi:~/node-v9.3.0-linux-armv6l $ sudo cp -R * /usr/local/
pi@raspberrypi:~/node-v9.3.0-linux-armv6l $ export NODE_PATH=/usr/local/lib/node_modules
pi@raspberrypi:~/node-v9.3.0-linux-armv6l $ node -v
v9.3.0
pi@raspberrypi:~/node-v9.3.0-linux-armv6l $ npm -v
5.5.1
pi@raspberrypi:~/node-v9.3.0-linux-armv6l $ cd ..
pi@raspberrypi:~ $ node
> console.log("Hello World");
Hello World
undefined (為啥會有這個東東?
下載
回覆刪除wget https://nodejs.org/dist/latest-v9.x/node-v9.3.0-linux-armv6l.tar.gz
解壓縮
tar -xvf node-v9.3.0-linux-armv6l.tar.gz
切換至資料夾
cd node-v9.3.0-linux-armv6l
移動至/usr/local/
sudo cp -R * /usr/local/
設定NODE_PATH變數(.bashrc)
export NODE_PATH=/usr/local/lib/node_modules
檢查版本
node -v
npm -v
這篇有解釋為什麼在node console執行會回傳undefined
回覆刪除https://stackoverflow.com/questions/24342748
如果是「node demo.js」就不會產生undefined
其實在Chrome/Firefox的主控台(F12)
執行console.log('hi')
也是會回傳undefined
原來如此, 受益頗多, 感謝您!
回覆刪除