執行下面命令,安裝nodejs 和 npm
ubuntu@ubuntu22:~$ sudo apt update
ubuntu@ubuntu22:~$ sudo apt install npm # npm 是 Node.js 的包管理工具
ubuntu@ubuntu22:~$ sudo apt install -y curl
ubuntu@ubuntu22:~$ curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
ubuntu@ubuntu22:~$ sudo apt install -y nodejs
遇到如下報錯
ubuntu@ubuntu22:~$ sudo apt-get install nodejs -y Reading package lists... Done Building dependency tree... Done Reading state information... Done The following packages were automatically installed and are no longer required: libc-ares2 libnode72 Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed: nodejs 0 upgraded, 1 newly installed, 0 to remove and 23 not upgraded. Need to get 0 B/29.6 MB of archives. After this operation, 187 MB of additional disk space will be used. Selecting previously unselected package nodejs. (Reading database ... 192008 files and directories currently installed.) Preparing to unpack .../nodejs_18.20.4-1nodesource1_amd64.deb ... Unpacking nodejs (18.20.4-1nodesource1) ... dpkg: error processing archive /var/cache/apt/archives/nodejs_18.20.4-1nodesource1_amd64.deb (--unpack): trying to overwrite '/usr/share/systemtap/tapset/node.stp', which is also in package libnode72:amd64 12.22.9~dfsg-1ubuntu3.6 Errors were encountered while processing: /var/cache/apt/archives/nodejs_18.20.4-1nodesource1_amd64.deb E: Sub-process /usr/bin/dpkg returned an error code (1)
這個錯誤表明在安裝 Node.js 18 時,出現了檔案衝突問題,特別是 node.stp
檔案被兩個不同的包(nodejs
和 libnode72
)包含。解決這個問題的辦法通常是先解除安裝衝突的舊版本,再安裝新的 Node.js 版本
解決步驟
ubuntu@ubuntu22:~$ sudo apt-get remove libnode72
ubuntu@ubuntu22:~$ sudo apt autoremove
ubuntu@ubuntu22:~$ sudo apt install -y nodejs
指令碼內容如下:
ubuntu@ubuntu22:~$ cat update_html.js
const juice = require('juice'); const fs = require('fs'); const html = fs.readFileSync('file.html', 'utf8'); const inlinedHtml = juice(html); console.log(inlinedHtml);
執行指令碼獲得新的html檔案:
ubuntu@ubuntu22:~$ nodejs update_html.js > new.html