一、npm基礎
一、什麼是npm?
npm 是模組管理工具,可以下載、更新第三方模組,也可以釋出自己的模組共替他人使用,主要目的在於分享和重用程式碼;
二、下載安裝node,更新npm
node 下載網址 https://nodejs.org/
下載完成 node -v 檢視版本
安裝node自帶npm npm -v 檢視版本
更新npm 的命令 npm install [email protected] -g
三、下載安裝npm package
npm install <包名
npm install <包名> --save-dev 開發環境依賴
npm install -g jshint 全域性安裝
通過ls node_modules 檢視安裝的包
使用 var lodash = require('lodash');
重點:npm 如何安裝指定版本的包
沒有
如果有packge.json 檔案
四、使用package.json
建立packge.json
npm init 問卷的形式
npm init --yes 建立個預設的
If you have a package.json file in your directory and you run npm install, then npm will look at the dependencies that are listed in that file and download the latest versions satisfying
五、更新pakage
npm update jshint 更新
npm update -g jshint 全域性更新
npm update -g 全域性更新全部
To find out which packages need to be updated, you can use npm outdated -g --depth=0.
六、下載依賴包pakage
npm uninstall lodash
npm uninstall lodash --save-dev 從package.json中刪除
npm uninstall -g jshint
七、釋出包public package
(1)建立package.json => npm init
(2) 建立入口檔案 => main 欄位
(3)creating user
npm adduser 註冊個賬戶
npm login 登入
(4)npm publish 釋出檔案
(5)npm install xxx --save 安裝
(6)npm unpublic xxx 取消釋出
注意:
package.json中的 name屬性名字為根目錄檔案的名字
也是 模組的名字 用於require;
名稱要不要跟npm 上的衝突了
八、更新packge
1.如果需要更新包,在修改完程式碼後請記得修改package.json包的version欄位,然後 npm publish。否則會無法釋出;
修改了提示文字,那麼我們需要 npm version <update_type>
update_type就是版本號的意思,會自動更新package.json裡面的版本號
然後重新 npm publish,更新就會完成
2.如果在釋出中顯示類似'請確認你是否有許可權更新xxx包'的英文提示,這就說明你的包名有人使用了。換個名字就好啦。
3.如果你想刪除一個自己釋出過的包,請使用命令 npm unpublish --force xxx (xxx為包名),一些沒有意義的包還是建議刪掉。
使用 cnpm 的注意報錯:
no_perms Private mode enable, only admin can publish this module
設定回原本的就可以了
npm config set registry http://registry.npmjs.org
釋出完成之後,如果還想回到之前的cnpm,使用下面的命令
npm config set registry https://registry.npm.taobao.org
九、版本控制Semantic versioning
Semantic versioning is a standard that a lot of projects use to communicate what kinds of changes are in this release. It's important to communicate what kinds of changes are in a release because sometimes those changes will break the code that depends on the package.
它是一個標準,用來溝通版本反生了哪些變化
版本號應該 從1.0.0開始
After this, changes should be handled as follows:
從1.0.0後,發生改變應該按照以下方式進行操作:
Bug fixes and other minor changes: Patch release, increment the last number, e.g. 1.0.1
命令:npm version patch
New features which don't break existing features: Minor release, increment the middle number, e.g. 1.1.0
命令:npm version minor
Changes which break backwards compatibility: Major release, increment the first number, e.g. 2.0.0
命令:npm version marjor
npm社群版本號規則採用的是semver(語義化版本),主要規則版本格式:主版本號.次版本號.修訂號,版本號遞增規則如下:
主版本號:當你做了不相容的 API 修改, 次版本號:當你做了向下相容的功能性新增, 修訂號:當你做了向下相容的問題修正。
先行版本號及版本編譯資訊可以加到“主版本號.次版本號.修訂號”的後面,作為延伸。
十 、 private module
With npm private modules, you can use the npm registry to host your own private code and the npm command line to manage it. This makes it easy to use public modules like Express and Browserify side-by-side with your own private code.
(1)Before we start
You need a version of npm greater than 2.7.0, and you'll need to log in to npm again.
sudo npm install -g npm
npm login
(2)Setting up your package
All private packages are scoped.
scolpes 是npm 的一個新的特性;如果一個packge的name 以@開頭,那麼他就是一個scoped package;
Scopes are a new feature of npm. If a package's name begins with @, then it is a scoped package. The scope is everything in between the @ and the slash.
@scope/project-name
When you sign up for private modules as an individual user, your scope is your username.
@username/project-name
If you use npm init to initialize your packages, you can pass in your scope like this:
npm init --scope=<your_scope>
If you use the same scope most of the time, you'll probably want to set it in your default configuration instead.
npm config set scope <your_scope>
(3)Publishing your package
npm publish 預設是private
npm publish --access=public
Once it's published, you should see it on the website with a private flag.
Giving access to others
npm owner add <user> <package name>
Installing private modules
npm install @scope/project-name
var project = require('@scope/project-name')
Switching from private to public
npm access restricted <package_name>
清除npm 的快取
npm cache clean
十一、.淘寶映象
$ npm install -g cnpm --registry=https://registry.npm.taobao.org
$ cnpm install [name]
11.how npm work
(1)packages and modules 定義是不同的
關於packages
A package is a file or directory that is described by a package.json
What is a package?
A package is any of the following:
a) a folder containing a program described by a package.json file
b) a gzipped tarball containing (a)
c) a url that resolves to (b)
d) a <name>@<version> that is published on the registry with (c)
e) a <name>@<tag> that points to (d)
f) a <name> that has a latest tag satisfying (e)
g) a git url that, when cloned, results in (a).
What is a module?
A module is anything that can be loaded with require() in a Node.js program. The following are all examples of things that can be loaded as modules:(以下是可以是可以作為模組載入你的例子)
A folder with a package.json file containing a main field.
A folder with an index.js file in it.
A JavaScript file.
翻譯:
一個資料夾包含package.json檔案並指定了main欄位
一個資料夾包含index.js檔案
一個javascript檔案
具體表現形式:
官網:https://nodejs.org/api/modules.html
阮一峰:http://www.ruanyifeng.com/blog/2015/05/require.html