1. nvm的官方叫法:nodejs版本管理工具。javascript
nvm至關因而家長,一個家長能夠管理多個孩子。
也就是說:一個nvm能夠管理不少node版本和npm版本。
2. nodejs前端
在項目開發時的所須要的代碼庫
3. npmjava
在安裝的nodejs的時候,npm也會跟着一塊兒安裝,它是包管理工具。
npm管理nodejs中的第三方插件
nvm、nodejs、npm的關係:node
nvm是爸爸,管理nodejs和npm這一對雙胞胎兄弟。npm是哥哥,npm哥哥能夠管理node弟弟的東西。react
我的是這麼理解的,要是有誤差,請指點。nginx
打開https://github.com/creationix/nvm。在下面的簡介中找到install這幾個字,而後繼續往下找,直到找到 git
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash
(隨着時間變哈 ,這個版本可能會出現變化,自行在github上查看)
複製這些字,到Mac的terminal中,就會安裝nvm,安裝完成後,還暫時不能用,須要複製它提示的兩行代碼(就是下圖拿箭頭標出來的兩行代碼)來配置環境變量: es6
完成之後 輸入github
nvm
出現web
node version manger
說明nvm安裝成功。
若是你們以前作過web服務器的人都知道,nginx+lua與如今流行的node.js都是能夠作web服務器的,前者在程序的寫法和配置上要比後者麻煩,但用起來都是差很少.在這裏建議你們若是對lua腳本語言不瞭解,能夠多瞭解這門腳本語言,他號稱是全部腳本語言執行效率是最高的一門腳本語言.底層是基於C語言的,很是好用,跨平臺!
下面我就來給你們配置一下node.js環境.
第一步:打開終端,輸入如下命令安裝Homebrew
ruby -e 「$(curl -fsSL
https://raw.githubusercontent.com/Homebrew/install/master/install);
第二步:安裝node,在終端輸入如下命令
brew install node
第三步 查看node安裝成功與否
node -v
以上三步 node就安裝成功了
程序測試
第一步:新建一個文件test.js
var http=require('http');//導入Node.Js中的Http的庫文件,並獲取句柄 //createServer函數,傳入回調函數,request,response var server=http.createServer(function(req,res){ console.log("Get A Request..."); res.writeHead(200,{ "Content-Type":"Text/plain" }); res.write("Hello NodeJs666"); res.end(); }); server.listen(5000);
第二步:用終端找到其所在的目錄運行
node test.js
第三步:經過瀏覽器進行訪問localhost:5000,返回數據
第四步:前端就能夠經過調用這個接口進行數據解析了,而且能夠在當前頁面進行數據展現了.
是否是很簡單,若是以前作過web服務接口開發的,這個應該不會陌生,甚至來講很是簡單了.
npm install <package_name>
:這個命令將在當前目錄中建立node_modules目錄(若是尚不存在),並將該軟件包下載到該目錄。該命令默認本地安裝。
若是本地目錄中沒有package.json文件,則會安裝最新版本的軟件包。
若是有package.json文件,則安裝知足該package(若是有的話)在package.json中聲明的semver規則的最新版本。
npm install -g <package>
:全局安裝包。
npm init
:這個命令用於建立一個package.json。
npm init --yes
或npm init -y
:從當前目錄中提取的信息生成默認的package.json。建立過程當中不會提問。
若是您的目錄中已經有一個package.json文件,而且運行了npm install
,那麼npm將查看該文件中的dependencies,並下載知足全部這些的最新版本。
package.json文件中的description幫助人們在npm搜索中找到您的包,因此在package.json中進行自定義描述很是有用。
也能夠徹底自定義package.json文件的內容和在init期間提出的問題。這經過建立自定義.npm-init.js來完成。默認狀況下,npm將查找您的主目錄。 〜/ .npm-init.js
dependencies和devDependencies指定了項目依賴的包。
dependencies:這些包在生產中須要。
devDependencies:這些包用於開發和測試。
npm install <package_name> --save
命令會添加條目到package.json的dependencies中。npm install <package_name> --save-dev
命令會添加條目到package.json的devDependencies中。
npm update
:用於更新依賴的軟件包。須要在package.json文件所在的目錄中運行該命令。
npm update -g <package>
:更新全局軟件包。npm update -g
:更新全部的全局軟件包。npm outdated -g --depth=0
:找出須要更新的包。
npm uninstall <package>
:從node_modules目錄中移除一個包。
npm uninstall --save <package>
:從package.json的dependencies中移除一個包。
npm uninstall --save-dev <package>
:從package.json的devDependencies中移除一個包。
實際操做時,發現使用npm uninstall <package>
不只會在node_modules目錄下刪除該包,還會將該包在package.json中dependencies或devDependencies裏面的信息刪除。
npm uninstall -g <package>
:卸載全局軟件包。
總結:本地命令加上-g就是全局命令。
參考自:npm
es6 javascript的ESLint 代碼檢測
ESLint 是一個語法規則和代碼風格的檢查工具,能夠用來保證寫出語法正確、風格統一的代碼。
首先,安裝 ESLint。
$ npm i -g eslint
而後,安裝 Airbnb 語法規則,以及 import、a11y、react 插件。
$ npm i -g eslint-config-airbnb $ npm i -g eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-react
最後,在項目的根目錄下新建一個.eslintrc
文件,配置 ESLint。
{ "extends": "eslint-config-airbnb" }
如今就能夠檢查,當前項目的代碼是否符合預設的規則。
index.js
文件的代碼以下。
var unusued = 'I have no purpose!'; function greet() { var message = 'Hello, World!'; alert(message); } greet();
使用 ESLint 檢查這個文件,就會報出錯誤。
$ eslint index.js index.js 1:1 error Unexpected var, use let or const instead no-var 1:5 error unusued is defined but never used no-unused-vars 4:5 error Expected indentation of 2 characters but found 4 indent 4:5 error Unexpected var, use let or const instead no-var 5:5 error Expected indentation of 2 characters but found 4 indent ✖ 5 problems (5 errors, 0 warnings)
上面代碼說明,原文件有五個錯誤,其中兩個是不該該使用var
命令,而要使用let
或const
;一個是定義了變量,卻沒有使用;另外兩個是行首縮進爲 4 個空格,而不是規定的 2 個空格。
原文:How to install NPM package in a home directory (PyCharm)
I don’t like to install things as a root user. If it’s possible to install something in my home directory, why not use it?
I use PyCharm for programming and for example to validate JavaScript code it needs http://eslint.org package. Let’s install ESLint in home directory and make it globally accessible.
First, we need to tell NPM that it should install everything in ~/.npm
directory.
npm config set prefix '~/.npm'
Now every time when you install NPM package using -g
option it will install it in ~/.npm
directory. You don’t need the root access to do it. All executables will be in ~/.npm/bin
directory. For example if I install ESLint using npm install -g eslint
command then it will be possible to run eslint
command from terminal using full path:
~/.npm/bin/eslint
It’s inconvenient, right? It’s better to be able to use just eslint
command. To achieve that we need to modify ~/.bash_profile
file. Find in this file line which starts with PATH=
and add ~/.npm/bin
at the end of the line. In my ~/.bash_profile
file it looks like this:
PATH=$PATH:$HOME/.local/bin:$HOME/bin:~/.npm/bin
If the line with PATH
doesn’t exist then you can add it:
PATH=$PATH:~/.npm/bin
Now you need to apply changes.
source ~/.bash_profile
And it’s done. You should be able to run eslint
without specifying the full path to the file.
Quick tip about PyCharm. PyCharm needs a full path to the ESLint directory, not to ESLint executable file. In settings you need to use~/.npm/lib/node_modules/eslint
path.
PyCharm - ESLint configuration
The error you are facing is because your configuration is not present. To configure the eslint type
eslint --init
then configure as your requirement.
then execute the project again.