1. 程式人生 > >Mac brew安裝node 及 npm用法

Mac brew安裝node 及 npm用法

使用brew安裝node,首先先對brew進行檢查

brew update
brew doctor

如果報這個錯:

Warning: Homebrew's sbin was not found in your PATH but you haveinstalled

formulae that put executables in /usr/local/sbin.

Consider setting the PATH for example like so

  echo 'exportPATH="/usr/local/sbin:$PATH"' >> ~/.bash_profile 

解決方法:

export PATH="/usr/local/bin:$PATH"
source ~/.bash_profile

然後進行安裝

brew link node
brew uninstall node
brew install node

安裝node成功。

在使用nodejs時,首先要設定一個工作資料夾,並對其初始化和新增各種dependencies。 

建立資料夾後,首先:

npm init

//this utility will walk you through createing a package.json file.
//it covers the most common items, and tries to guess sane defaults.

然後安裝各種依賴dependecies,它們會被安裝在node_module包裡。下面舉幾個例子。

npm install web3
npm install web3-eth
npm install ethereumjs-tx
npm install fast-csv

語法為:

npm install <module>   //安裝
npm uninstall <module>   //解除安裝
npm search <module>   //搜尋
npm update <module>   //更新

這種安裝是本地安裝,安裝在命令列所在的資料夾裡。nodejs使用這些依賴時,要用var xx = requires('xxx')去使用。

還可以全域性安裝:npm install <module> -g,這種安裝方式安裝在使用者目錄下,使用時候不能用require方式。

node install -g 才會把module安裝到node全域性 不加-g預設安裝到當前工程 別的工程不可見。

注:

fs 是node自帶的功能,所以不需要npm install fs

npm 是node.js自帶的功能

參考文章: 

npm init node 通過指令建立一個package.json檔案及npm安裝package.json

【連結】nodejs npm install全域性安裝和本地安裝的區別

【連結】NPM使用介紹