一、什么是npm?
npm 是模块管理工具,可以下载、更新第三方模块,也可以发布自己的模块共替他人使用,主要目的在于分享和重用代码;
二、下载安装node,更新npm
node 下载网址 https://nodejs.org/
下载完成 node -v 查看版本
安装node自带npm npm -v 查看版本
更新npm 的命令 npm install npm@latest -g
三、下载安装npm package
npm install <包名> --save 生产环境依赖
npm install <包名> --save-dev 开发环境依赖
npm install -g jshint 全局安装
通过ls node_modules 查看安装的包
使用 var lodash = require('lodash');
重点:npm 如何安装指定版本的包
没有packge.json 文件将默认下载最新版本
如果有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 semver rules for all of those.
五、更新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