一、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的黄金时期,抽出时间来看我的博文哈哈哈,时间真快,已经到第十四篇博文了,也很顺顺(跌跌)利利 (撞撞 ...
随机推荐
- FCM算法的matlab程序
FCM算法的matlab程序 在“FCM算法的matlab程序(初步)”这篇文章中已经用matlab程序对iris数据库进行简单的实现,下面的程序最终的目的是求准确度. 作者:凯鲁嘎吉 - 博客园 h ...
- [福大软工] Z班 团队Alpha阶段成绩汇总
团队成绩汇总表 团队 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 总分 Dipper 9 85 90 26 42 27.5 120 74 25 111 19 628.5 SW ...
- 【文学文娱】《屌丝逆袭》-出任CEO、迎娶白富美、走上人生巅峰
本文地址:http://www.cnblogs.com/aiweixiao/p/7759790.html 原文地址:(微信公众号) 原创 2017-10-30 微信号wozhuzaisi 程序员的文娱 ...
- Spring 的application.properties项目配置与注解
一.项目结构介绍 如上图所示,Spring Boot的基础结构共三个文件: src/main/java 程序开发以及主程序入口 src/main/resources 配置文件 src/test/ja ...
- UltraISO制作Ubuntu14.04 64bit到U盘文件载入不完整
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/zinss26914/article/details/37728251 前言 今天新买的Thinkpa ...
- eclipse中添加server后,启动server,访问项目时,端口是怎么选择的。
1 eclipse中添加了tomcat 2 设置端口时,可以在图2.1修改 也可以在图2.2修改 3 点击server的publish按钮,会将图2.2的配置文件和server中添加的项目同步到实 ...
- multiply对应位置相乘 与 dot矩阵乘
区别 # -*- coding: utf- -*- import numpy as np a = np.array([[,], [,]]) b= np.arange().reshape((,)) c ...
- js 常用的比较排序算法总结
每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code 一直很惧怕算法,总是感觉特别伤脑子,因此至今为止,几种基本的排序算法一直都不是很清楚, ...
- 【转】让Xcode支持iOS12.0 设备真机测试(不断更新真机支持包)Xcode 真机测试 iOS
最新支持12.0 (16A366)! 所有真机包都是同一大版本号向下兼容.例如12.0 Beta6的真机包,是可以兼容12.0 Beta2及Beta1的. 12.0 (16A366)正式版可用!所有低 ...
- ssm框架的整合搭建(二)
maven简单项目的创建前面已经完成了,下面开始依赖包和配置文件的编写 好好努力每一天!!!!!! 项目结构看这里 1.首先,依赖包pom.xml <project xmlns="ht ...