Node.js 部分

以下內容為 2015 年內容,部分尚未更新

安裝

# Using Ubuntu
curl -fsSL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt-get install -y nodejs

# Using Debian, as root
curl -fsSL https://deb.nodesource.com/setup_12.x | bash -
apt-get install -y nodejs

1. 下載Node.js

使用 VSCode, Sublime, Atom. Nodepad 或任何編輯器創造如下檔案

class.js

function hello() {
  console.log('Hello World!');
}
hello();

之後開啟終端機,並輸入cd 你的檔案路徑

輸入node class

2.模組

export

新增一個class1.js

class.js

3.module.exports

class1.js

class.js

4.一個模組中的JS代碼僅在模組第一次被使用時執行一次,並在執行過程中初始化模組的導出對像。之後,緩存起來的導出對像被重複利用。

class1.js

class.js

5.預設載入路徑(如給予相對路徑沒/)

class.js

將產生無法找到路徑之錯誤

於是新增node_modules資料夾,將class.js放入即可

NodeJS定義了一個特殊的node_modules目錄用於存放模塊。例如某個模塊的絕對路徑是/home/user/hello.js,在該模塊中使用require('foo/bar')方式加載模塊時,則NodeJS依次嘗試使用以下路徑。

6.手動設定預設加載模組路徑

  1. 將以下放在require前

2.直接更改系統環境變數

7.一次加載(require)在資料夾下的js檔案

1.第一種方式,將檔案改名為index.js,創建一個as資料夾下面創一個index.js

在外面創一個class.js

名稱一定要為index.js才可將路徑指定為其上之資料夾,而非檔案

8.使用package.json

在as資料夾裡面新增package.json檔案

之後即可將index.js改名為class1,而之後就算require的路徑為資料夾,只要其下有package.json檔案,且有指定main(預設開啟檔案),即可

9.指定require資料夾路徑但其下需載入多個js檔

使用require將每個js擋在入口js內載入即可

10.Build a command line tool

http://blog.npmjs.org/post/118810260230/building-a-simple-command-line-tool-with-npm

http://www.ruanyifeng.com/blog/2015/05/command-line-with-node.html

11. process.env

用來定義執行時期的參數, 建議寫在script的最前面

package.json

讀取

12. 使用 Export

Last updated

Was this helpful?