用curl安裝
$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
安裝在家目錄下為/.nvm
安裝完的回應訊息
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--: 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:100 11699 100 11699 0 0 16751 0 --:--:-- --:--:-- --:--:-- 16736
=> Downloading nvm from git to '/Users/xanxus/.nvm'
=> Cloning into '/Users/xanxus/.nvm'...
remote: Counting objects: 6460, done.
remote: Total 6460 (delta 0), reused 0 (delta 0), pack-reused 6460
Receiving objects: 100% (6460/6460), 1.90 MiB | 395.00 KiB/s, done.
Resolving deltas: 100% (4007/4007), done.
* (HEAD detached at v0.33.2)
master
=> Compressing and cleaning up git repository
Counting objects: 6460, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (6416/6416), done.
Writing objects: 100% (6460/6460), done.
Total 6460 (delta 4263), reused 1987 (delta 0)
=> Appending nvm source string to /Users/xanxus/.zshrc
=> Appending bash_completion source string to /Users/xanxus/.zshrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
驗證是否安裝成功:
$ command -v nvm
git下來安裝:
git clone https://github.com/lukechilds/zsh-nvm.git ~/.zsh-nvm
打開.zshrc:
$ open ~/.zshrc
在.zshrc加入:
$ source ~/.zsh-nvm/zsh-nvm.plugin.zsh
$ source ~/.zsh-nvm/zsh-nvm.plugin.zsh
升級nvm:
$ nvm upgrade
查看有哪些node版本可裝:
$ nvm ls-remote
安裝node:
$ nvm install v6.11.0
查看所有安裝的node版本
$ nvm ls
查看所有安裝的node版本
$ nvm ls
使用已安裝的node
$ nvm use v6.11.0
設定預設node版本
$ nvm alias default v6.11.0
which查看裝在哪
$ nvm which v6.11.0
更新npm版本 [2]
$ sudo npm install npm --global
寫一個簡單的web server測試是否可用,程式碼檔名為server.js:
cd到server.js的位置,並執行:
$ node server.js
Server running at http://127.0.0.1:3000/
退出node對話:
⌃+c 兩次
寫一個簡單的web server測試是否可用,程式碼檔名為server.js:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
cd到server.js的位置,並執行:
$ node server.js
Server running at http://127.0.0.1:3000/
退出node對話:
⌃+c 兩次
npm相關指令
安裝套件有分安裝在全域global、和本地(Local Mode)
安裝在全域指令會多加個-g,如:
$ npm install 要裝的套件名稱 -g
裝在本地就是一般的:
$ npm install 要裝的套件名稱
查看目前使用的npm路徑在哪(設置環境變數要用的路徑):
$ npm -g bin
/Users/username/.nvm/versions/node/v8.11.1/bin
列出所有安裝的全域套件:
$ npm ls -g
如果出現
npm WARN enoent ENOENT: no such file or directory, open '/Users/muser/package.json'
這樣的訊息
是因為沒有初始化的關係,請下:
$ npm init
Netbeans的node.js專案的npm install不能用
當我對我創的node.js專案按右鍵選npm install,console回應以下訊息:
"/bin/bash" "-lc" "\"/Users/xanxus/.nvm/versions/node/v8.11.1/bin/npm\" \"install\""
Done.
env: node: No such file or directory
根據紅色訊息去爬文很像是因為node用非官方安裝的話,可能就會出現這樣的問題,可參考以下連結,有解法:
https://bit.ly/1WdULMe
以下是程式筆記
建立模組(module),Messages.js:
不指定名字給函式的模組寫法,anonyMsg.js:
呼叫模組,anonyMsgApp.js:
Exports類別化函式,Person.js:
在Node.js直接搜尋資料夾,不載入檔案的寫法與檔案結構: 先創建一個utility的資料夾,裡面放兩個檔案,分別是package.json和log.js。 package.json
安裝套件有分安裝在全域global、和本地(Local Mode)
安裝在全域指令會多加個-g,如:
$ npm install 要裝的套件名稱 -g
裝在本地就是一般的:
$ npm install 要裝的套件名稱
查看目前使用的npm路徑在哪(設置環境變數要用的路徑):
$ npm -g bin
/Users/username/.nvm/versions/node/v8.11.1/bin
列出所有安裝的全域套件:
$ npm ls -g
如果出現
npm WARN enoent ENOENT: no such file or directory, open '/Users/muser/package.json'
這樣的訊息
是因為沒有初始化的關係,請下:
$ npm init
Netbeans的node.js專案的npm install不能用
當我對我創的node.js專案按右鍵選npm install,console回應以下訊息:
"/bin/bash" "-lc" "\"/Users/xanxus/.nvm/versions/node/v8.11.1/bin/npm\" \"install\""
Done.
env: node: No such file or directory
根據紅色訊息去爬文很像是因為node用非官方安裝的話,可能就會出現這樣的問題,可參考以下連結,有解法:
https://bit.ly/1WdULMe
但當我下ln指令時,我下的ln指令如下:
$ sudo ln -s /Users/xanxus/.nvm/versions/node/v8.11.1/bin/node /usr/bin/node
ln: /usr/bin/node: Operation not permitted
出現Operation not permitted這樣的訊息,再把這訊息丟關鍵字去搜尋,很像是mac的問題,可參考以下連結的文章:
https://digishot.keenchief.com/tw/7066615261
這篇也是這樣寫:
https://blog.csdn.net/hankai945/article/details/72654927
這篇也是這樣寫:
https://blog.csdn.net/hankai945/article/details/72654927
文章說很像是因為mac權限的問題,必須重開機去Command+R進入Recovery模式...
有點麻煩,所以先記錄到這裡,也不繼續往下做了,以後有機會再來試試看。
以下連結的影片是設置Netbeans的node.js的相關環境變數:
https://www.youtube.com/watch?v=UqDuhvDFc7A
以下連結的影片是設置Netbeans的node.js的相關環境變數:
https://www.youtube.com/watch?v=UqDuhvDFc7A
以下是程式筆記
建立模組(module),Messages.js:
module.exports = 'Hello world';
//or
//exports = 'Hello world';//這個不知為何只能輸出{}
引用模組,MessageApp.js:var msg = require('./Messages.js');
console.log(msg);//印出Hello world
以上兩隻程式都是在同一個目錄底下。
不指定名字給函式的模組寫法,anonyMsg.js:
module.exports = function (msg) {
console.log(msg);
};
Node-RED的節點的程式就是這樣寫的:呼叫模組,anonyMsgApp.js:
var msg = require('./anonyMsg.js');
msg('Hello World');
會印出Hello World
Exports類別化函式,Person.js:
module.exports = function (firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.fullName = function () {
return this.firstName + ' ' + this.lastName;
}
}
呼叫模組:
var person = require('./Person.js');
var person1 = new person('James', 'Bond'); //建立一個 person 物件,名為 person1
console.log(person1.firstName);
console.log(person1.fullName());
在Node.js直接搜尋資料夾,不載入檔案的寫法與檔案結構: 先創建一個utility的資料夾,裡面放兩個檔案,分別是package.json和log.js。 package.json
{
"name": "log",
"main": "./log.js"
}
log.js
module.exports = "Hello World";呼叫模組,main.js:
var log = require("./utility");
console.log(log);
會印出Hello World
參考資料:


沒有留言:
張貼留言