一、什么是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

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基础的更多相关文章

  1. Node.js npm基础安装配置&创建第一个VUE项目

    使用之前,我们先来明白这几个东西是用来干什么的. node.js: 一种javascript的运行环境,能够使得javascript脱离浏览器运行.Node.js的出现,使得前后端使用同一种语言,统一 ...

  2. npm 基础

    npm账户 npm adduser npm whoami 初始化项目: npm init --scope=<username> 项目必要文件 README.md pageage.json: ...

  3. npm基础知识笔记

    # NPM Study 1.npm组成 --网站 --命令行界面(CLI) --注册表   2.npm入门-创建属于你的npm账户 --https://www.npmjs.com/signup   5 ...

  4. npm基础用法

    一. 安装 npm基于nodejs,因此应该先安装nodejs 可在nodejs官网中下载安装 我们一般选择安装稳定版,即长期支持版 安装过程很简单,和普通的软件一样,一直 下一步 就好了 nodej ...

  5. npm 基础命令

    npm是一个node包管理和分发工具,已经成为了非官方的发布node模块(包)的标准.有了npm,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装的包.npm 从5.2版开始,增加了 ...

  6. Node.js npm 详解

    一.npm简介 安装npm请阅读我之前的文章Hello Node中npm安装那一部分,不过只介绍了linux平台,如果是其它平台,有前辈写了更加详细的介绍. npm的全称:Node Package M ...

  7. VueJs2.0建议学习路线

    最近VueJs确实火了一把,自从Vue2.0发布后,Vue就成了前端领域的热门话题,github也突破了三万的star,那么对于新手来说,如何高效快速的学习Vue2.0呢. 既然大家会看这篇文章,那么 ...

  8. (二) 从Angular1到Angular2需要的预备知识

    1. TypeScript语法与ES6新特性 写惯了jQ的话突然从ES5跳到ES6,又是个变形的ES6(TypeScript),学习成本确实不低.不过笔者也是从ng1直接上手ng2,对与很多新特性的积 ...

  9. 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 十四 ║ VUE 计划书 & 我的前后端开发简史

    ---新内容开始--- 番外 大家周一好呀,又是元气满满的一个周一呀!感谢大家在周一这个着急改Bug的黄金时期,抽出时间来看我的博文哈哈哈,时间真快,已经到第十四篇博文了,也很顺顺(跌跌)利利 (撞撞 ...

随机推荐

  1. C++中的字符串输入输出函数详解

    常见的输入问题: 1.直接用cin输入(当然可以使用cout直接输出): 1)string s; cin >> s; //只接收回车键和空格前面所输入的字符!!!!! 一旦输入空格,cin ...

  2. Angular中$watch实现控件改变后实时发送HTTP请求

    实现代码如下 <!DOCTYPE html> <html ng-app="myServiceApp"> <head> <meta char ...

  3. 注册mySQL到JDBC驱动程序方法浅谈

    一.注册方法(4种) 1)服务提供者框架: 符合JDBC 4.0规范的驱动程序包含了一个文件META-INF/services/java.sql.Driver,在这个文件中提供了JDBC驱动实现的类名 ...

  4. JavaScrip 入门第一课

    一.代码引入的三种方式 1.直接在head中书写 在head标签里面可以写,在body标签里面也可以写,放到head标签里面和放到body标签里面到底有什么区别,我们后续在讲~ <head> ...

  5. mongodb初级

    上班第一天,先玩玩mongdb! 1:下载安装就不说了 2:启动服务  mongod 通常会报错: 原因:mongodb会使用默认的数据库存储路径是data/db,刚安装好的mongodb是不存在该路 ...

  6. Vue指令v-for之遍历输出JavaScript数组,json对象的几种方式

    定义数据: <script> new Vue({ el:"#test", data:{ message:"infor", list:["a ...

  7. 在 PHP 7 中不要做的 10 件事

    在 PHP 7 中不要做的 10 件事 1. 不要使用 mysql_ 函数 这一天终于来了,从此你不仅仅“不应该”使用mysql_函数.PHP 7 已经把它们从核心中全部移除了,也就是说你需要迁移到好 ...

  8. No such property: FOR_RUNTIME for class: org.gradle.api.attributes.Usage

    自从Android studio升级到3.1版本,谷歌又折腾出让你意想不到的错误.... 从github上下了个项目用来学习,却出现了如下错误: No such property: FOR_RUNTI ...

  9. 游览器发送http请求经过的步骤

    OSI参考模型(Open System Interconnection,开放系统互连),全称为开放系统互联参考模型 ,OSI将计算机网络体系结构划分为了七层 TCP/IP协议族(TCP/IP Prot ...

  10. UVA10570-Meeting with Aliens(枚举)

    Problem UVA1616-Caravan Robbers Accept: 531  Submit: 2490Time Limit: 3000 mSec Problem Description I ...