Nodejs bag module mounted with a third party and package.json npm and cnpm

Package with NPM

1 package

Nodejs in addition to its own offer to the core module, you can customize the module, you can also use third-party modules. Nodejs first -party modules composed by the package, can be unified management of a set of modules with interdependent relationships through the package.

 

In full compliance with CommonJs package directory contains the following general specification of these documents.

  • package.json: package description file.

  • bin: directory for the executable binary files.

  • lib: used to store JavaScript directory code.

  • doc: directory to store documents.

 


Nodejs NPM command to download third-party modules (package).

 

npm i silly-datetime –save
var sd = require('silly-datetime');
sd.format(new Date(), 'YYYY-MM-DD HH:mm');

 

NPM introduced

  1. npm 是世界上最大的开放源的生态系统,通过npm可以下载各种各样的包,这些源代码(包)我们可以在 https://www.npmjs.com 找到。
    npm 是随同 NodeJS 一起安装的包管理工具,能解决 NodeJS 代码部署上的很多问题,

    常见的使用场景有 以下几种:

    1.  允许用户从 NPM 服务器下载别人编写的第三方包到本地使用。

    2. 允许用户从 NPM 服务器下载并安装别人编写的命令行程序(工具)到本地使用。

       允许用户将自己编写的包或命令行程序上传到 NPM 服务器供别人使用。

 


NPM 命令详解

1.npm -v 查看 npm 版本

2.使用 npm 命令安装模块

npm install
安装 jq 模块:
 如
Module Name
npm install jquery

3.npm uninstall moudleName 卸载模块

npm uninstall ModuleName

4.npm list 查看当前目录下已安装的 node 包

npm list

5.npm  info jquery 查看 jquery的版本

npm info 模块 //查看模块的版本

6.指定版本安装:npm install [email protected]

 


package.json

package.json 定义了这个项目所需要的各种模块,以及项目的配置信息(比如名称、

版本、许可证等元数据)

1.创建 package.json

npm init
npm init –yes

2、package.json 文件

{
  "name": "commonjs_demo",
  "version": "1.0.0",
  "description": "",
  "main": "commonjs01.js",
  "dependencies": {
    "nav": "^1.0.0"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

 

安装模块并把模块写入 package.json(依赖)

  npm install babel-cli --save-dev
  npm install 模块 --save
  npm install 模块 --save-dev

4、dependencies 与 devDependencies 之间的区别?
使用 npm install node_module –save 自动更新 dependencies 字段值;

使用 npm install node_module –save-dev 自动更新 devDependencies 字段值;

depende ncie 配置当前程序所依赖的其他包。

 

devDepende ncie配置当前程序所依赖的其他包,只会下载块,而不会下载这些模块的测试和文档框架。

"dependencies": {
"ejs": "^2.3.4", 
"express": "^4.13.3", 
"formidable": "^1.0.17"
 
}

^ Denotes a version number change, take the latest two back

~ For the first two the same, the last to take the latest

* Means all the latest take

 


 

Taobao mirror installation

http://www.npmjs.org     npm package official website

https://npm.taobao.org/ Taobao npm mirroring official website

Taobao NPM mirror is a complete npmjs.org image, you can use this instead of the official version (read-only ), synchronous frequency current is 10 minutes in order to ensure as far as possible be synchronized with the official service.

We can use our custom cnpm (gzip compression support ) command-line tool instead of the default npm:

npm install -g cnpm --registry=https://registry.npm.taobao.org

Guess you like

Origin www.cnblogs.com/loaderman/p/11491758.html