一、npm基础
一、什么是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
一、npm基础的更多相关文章
- Node.js npm基础安装配置&创建第一个VUE项目
使用之前,我们先来明白这几个东西是用来干什么的. node.js: 一种javascript的运行环境,能够使得javascript脱离浏览器运行.Node.js的出现,使得前后端使用同一种语言,统一 ...
- npm 基础
npm账户 npm adduser npm whoami 初始化项目: npm init --scope=<username> 项目必要文件 README.md pageage.json: ...
- npm基础知识笔记
# NPM Study 1.npm组成 --网站 --命令行界面(CLI) --注册表 2.npm入门-创建属于你的npm账户 --https://www.npmjs.com/signup 5 ...
- npm基础用法
一. 安装 npm基于nodejs,因此应该先安装nodejs 可在nodejs官网中下载安装 我们一般选择安装稳定版,即长期支持版 安装过程很简单,和普通的软件一样,一直 下一步 就好了 nodej ...
- npm 基础命令
npm是一个node包管理和分发工具,已经成为了非官方的发布node模块(包)的标准.有了npm,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装的包.npm 从5.2版开始,增加了 ...
- Node.js npm 详解
一.npm简介 安装npm请阅读我之前的文章Hello Node中npm安装那一部分,不过只介绍了linux平台,如果是其它平台,有前辈写了更加详细的介绍. npm的全称:Node Package M ...
- VueJs2.0建议学习路线
最近VueJs确实火了一把,自从Vue2.0发布后,Vue就成了前端领域的热门话题,github也突破了三万的star,那么对于新手来说,如何高效快速的学习Vue2.0呢. 既然大家会看这篇文章,那么 ...
- (二) 从Angular1到Angular2需要的预备知识
1. TypeScript语法与ES6新特性 写惯了jQ的话突然从ES5跳到ES6,又是个变形的ES6(TypeScript),学习成本确实不低.不过笔者也是从ng1直接上手ng2,对与很多新特性的积 ...
- 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 十四 ║ VUE 计划书 & 我的前后端开发简史
---新内容开始--- 番外 大家周一好呀,又是元气满满的一个周一呀!感谢大家在周一这个着急改Bug的黄金时期,抽出时间来看我的博文哈哈哈,时间真快,已经到第十四篇博文了,也很顺顺(跌跌)利利 (撞撞 ...
随机推荐
- Mybatis 报错 There is no getter for property named '***' in 'class java.lang.String'
在mapper.xml中 , 如果单参数是String类型 , 且在sql语句中对参数进行了判断 , 如下 when 中的判断 , 如果出现 if 判断也是一样的.都需要把判断中的参数用 _param ...
- DB2常见错误信息
000 00000 SQL语句成功完成01xxx SQL语句成功完成,但是有警告+012 01545 未限定的列名被解释为一个有相互联系的引用+098 01568 动态SQL语句用分号结束+100 0 ...
- Java多线程(一) 什么是线程
声明:本系列大多是翻译自https://www.javatpoint.com,加上自己的增删改,尽力写的系统而通俗易懂,后文不再重复声明. 点我跳过黑哥的卑鄙广告行为,进入正文. Java多线程系列更 ...
- [WeChall] Training: Crypto - Caesar I (Crypto, Training)
Training: Crypto - Caesar I (Crypto, Training) Crypto - Caesar I As on most challenge sites, there a ...
- MySQL 目录结构、配置文件、修改密码
查看全局数据文件路径 show global variables like "%datadir%" 一.文件目录结构 文件安装路径为F:/JJ/MYSQL-5.6.42-WINX6 ...
- 详解区块链P2P网络
根据前一篇文章<从微观到宏观理解区块链>我们已经了解到,微观上,区块链本质就是一种不可篡改且可追踪溯源的哈希链条:宏观上,还具备了另外三个基本特征:分布式存储.P2P 网络和共识机制.分布 ...
- UVA1153-Keep the Customer Satisfied(贪心)
Problem UVA1153-Keep the Customer Satisfied Accept: 222 Submit: 1706Time Limit: 3000 mSec Problem D ...
- 003_python学习之 字符串前'r'的用法
在打开文件的时候open(r'c:\....') 加r和不加''r是有区别的 'r'是防止字符转义的 如果路径中出现'\t'的话 不加r的话\t就会被转义 而加了'r'之后'\t'就能保留原有的样子 ...
- linux运行级别和开机流程
linux有七个运行级别 运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动 运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆 运行级别2:多用户状态(没有NF ...
- ORA-00600: internal error code, arguments: [2662]
转自 http://www.eygle.com/archives/2005/12/oracle_diagnostics_howto_deal_2662_error.html 在ORA-00600 22 ...