初学node.js(二)npm、模块更新、异步回调

继续学习node.js~
表示在公司每天都能学到新的知识点,很happy了~


Node.js - npm

npm 表示节点程序包管理器。npm 提供以下两个主要功能:

  • Node.js包/模块的在线软件仓库
  • 命令行实用程序安装包,作为Node.js版本管理和依赖包管理。

Node.js v0.6.3版本后才开始捆绑 npm 安装。为了验证,打开控制台,然后输入以下命令并查看结果:
这里写图片描述

package.json 包

package.json是存在于任何Node应用程序/模块的根目录,并用于定义一个包的属性。 让我们打开 package.json,
express包在 C:\Users\Administrator\node_modules下(路径因人而异)

{
  "version": "5.6.0",
  "name": "npm",
  "description": "a package manager for JavaScript",
  "keywords": [
    "install",
    "modules",
    "package manager",
    "package.json"
  ],
  "preferGlobal": true,
  "config": {
    "publishtest": false
  },
  "homepage": "https://docs.npmjs.com/",
  "author": "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me)",
  "repository": {
    "type": "git",
    "url": "https://github.com/npm/npm"
  },
  "bugs": {
    "url": "https://github.com/npm/npm/issues"
  },
  "directories": {
    "bin": "./bin",
    "doc": "./doc",
    "lib": "./lib",
    "man": "./man"
  },
  "main": "./lib/npm.js",
  "bin": {
    "npm": "./bin/npm-cli.js",
    "npx": "./bin/npx-cli.js"
  },
  "dependencies": {
    "JSONStream": "~1.3.1",
    "abbrev": "~1.1.1",
    "ansi-regex": "~3.0.0",
    "ansicolors": "~0.3.2",
    "ansistyles": "~0.1.3",
    "aproba": "~1.2.0",
    "archy": "~1.0.0",
    "bin-links": "^1.1.0",
    "bluebird": "~3.5.1",
    "cacache": "~10.0.1",
    "call-limit": "~1.1.0",
    "chownr": "~1.0.1",
    "cli-table2": "~0.2.0",
    "cmd-shim": "~2.0.2",
    "columnify": "~1.5.4",
    "config-chain": "~1.1.11",
    "detect-indent": "~5.0.0",
    "dezalgo": "~1.0.3",
    "editor": "~1.0.0",
    "find-npm-prefix": "~1.0.1",
    "fs-vacuum": "~1.2.10",
    "fs-write-stream-atomic": "~1.0.10",
    "gentle-fs": "^2.0.1",
    "glob": "~7.1.2",
    "graceful-fs": "~4.1.11",
    "has-unicode": "~2.0.1",
    "hosted-git-info": "~2.5.0",
    "iferr": "~0.1.5",
    "inflight": "~1.0.6",
    "inherits": "~2.0.3",
    "ini": "~1.3.4",
    "init-package-json": "~1.10.1",
    "is-cidr": "~1.0.0",
    "lazy-property": "~1.0.0",
    "libnpx": "~9.7.1",
    "lockfile": "~1.0.3",
    "lodash._baseuniq": "~4.6.0",
    "lodash.clonedeep": "~4.5.0",
    "lodash.union": "~4.6.0",
    "lodash.uniq": "~4.5.0",
    "lodash.without": "~4.4.0",
    "lru-cache": "~4.1.1",
    "meant": "~1.0.1",
    "mississippi": "~1.3.0",
    "mkdirp": "~0.5.1",
    "move-concurrently": "^1.0.1",
    "nopt": "~4.0.1",
    "normalize-package-data": "~2.4.0",
    "npm-cache-filename": "~1.0.2",
    "npm-install-checks": "~3.0.0",
    "npm-lifecycle": "~2.0.0",
    "npm-package-arg": "~6.0.0",
    "npm-packlist": "~1.1.10",
    "npm-profile": "~2.0.5",
    "npm-registry-client": "~8.5.0",
    "npm-user-validate": "~1.0.0",
    "npmlog": "~4.1.2",
    "once": "~1.4.0",
    "opener": "~1.4.3",
    "osenv": "~0.1.4",
    "pacote": "^7.0.2",
    "path-is-inside": "~1.0.2",
    "promise-inflight": "~1.0.1",
    "qrcode-terminal": "~0.11.0",
    "query-string": "~5.0.1",
    "qw": "~1.0.1",
    "read": "~1.0.7",
    "read-cmd-shim": "~1.0.1",
    "read-installed": "~4.0.3",
    "read-package-json": "~2.0.12",
    "read-package-tree": "~5.1.6",
    "readable-stream": "~2.3.3",
    "request": "~2.83.0",
    "retry": "~0.10.1",
    "rimraf": "~2.6.2",
    "safe-buffer": "~5.1.1",
    "semver": "~5.4.1",
    "sha": "~2.0.1",
    "slide": "~1.1.6",
    "sorted-object": "~2.0.1",
    "sorted-union-stream": "~2.1.3",
    "ssri": "~5.0.0",
    "strip-ansi": "~4.0.0",
    "tar": "^4.0.2",
    "text-table": "~0.2.0",
    "uid-number": "0.0.6",
    "umask": "~1.1.0",
    "unique-filename": "~1.1.0",
    "unpipe": "~1.0.0",
    "update-notifier": "~2.3.0",
    "uuid": "~3.1.0",
    "validate-npm-package-name": "~3.0.0",
    "which": "~1.3.0",
    "worker-farm": "~1.5.1",
    "wrappy": "~1.0.2",
    "write-file-atomic": "~2.1.0"
  },
  "bundleDependencies": [
    "abbrev",
    "ansi-regex",
    "ansicolors",
    "ansistyles",
    "aproba",
    "archy",
    "cacache",
    "call-limit",
    "bluebird",
    "bin-links",
    "chownr",
    "cli-table2",
    "cmd-shim",
    "columnify",
    "config-chain",
    "debuglog",
    "detect-indent",
    "dezalgo",
    "editor",
    "find-npm-prefix",
    "fs-vacuum",
    "fs-write-stream-atomic",
    "gentle-fs",
    "glob",
    "graceful-fs",
    "has-unicode",
    "hosted-git-info",
    "iferr",
    "imurmurhash",
    "inflight",
    "inherits",
    "ini",
    "init-package-json",
    "is-cidr",
    "JSONStream",
    "lazy-property",
    "libnpx",
    "lockfile",
    "lodash._baseindexof",
    "lodash._baseuniq",
    "lodash._bindcallback",
    "lodash._cacheindexof",
    "lodash._createcache",
    "lodash._getnative",
    "lodash.clonedeep",
    "lodash.restparam",
    "lodash.union",
    "lodash.uniq",
    "lodash.without",
    "lru-cache",
    "meant",
    "mkdirp",
    "mississippi",
    "move-concurrently",
    "nopt",
    "normalize-package-data",
    "npm-cache-filename",
    "npm-lifecycle",
    "npm-install-checks",
    "npm-package-arg",
    "npm-packlist",
    "npm-profile",
    "npm-registry-client",
    "npm-user-validate",
    "npmlog",
    "once",
    "opener",
    "osenv",
    "pacote",
    "path-is-inside",
    "promise-inflight",
    "query-string",
    "qrcode-terminal",
    "qw",
    "read",
    "read-cmd-shim",
    "read-installed",
    "read-package-json",
    "read-package-tree",
    "readable-stream",
    "readdir-scoped-modules",
    "request",
    "retry",
    "rimraf",
    "semver",
    "sha",
    "slide",
    "sorted-object",
    "sorted-union-stream",
    "ssri",
    "strip-ansi",
    "tar",
    "text-table",
    "uid-number",
    "umask",
    "unique-filename",
    "unpipe",
    "update-notifier",
    "uuid",
    "validate-npm-package-license",
    "validate-npm-package-name",
    "which",
    "wrappy",
    "write-file-atomic",
    "safe-buffer",
    "worker-farm"
  ],
  "devDependencies": {
    "deep-equal": "~1.0.1",
    "marked": "~0.3.6",
    "marked-man": "~0.2.1",
    "npm-registry-couchapp": "~2.7.0",
    "npm-registry-mock": "~1.1.0",
    "require-inject": "~1.4.2",
    "sprintf-js": "~1.1.1",
    "standard": "~6.0.8",
    "tacks": "~1.2.6",
    "tap": "~10.7.2"
  },
  "scripts": {
    "dumpconf": "env | grep npm | sort | uniq",
    "prepare": "node bin/npm-cli.js --no-timing prune --prefix=. --no-global && rimraf test/*/*/node_modules && make -j4 doc",
    "preversion": "bash scripts/update-authors.sh && git add AUTHORS && git commit -m \"update AUTHORS\" || true",
    "tap": "tap --timeout 300",
    "tap-cover": "tap --nyc-arg='--cache' --coverage --timeout 600",
    "test": "standard && npm run test-tap",
    "test-coverage": "npm run tap-cover -- \"test/tap/*.js\" \"test/network/*.js\" \"test/broken-under-*/*.js\"",
    "test-tap": "npm run tap -- \"test/tap/*.js\" \"test/network/*.js\" \"test/broken-under-*/*.js\"",
    "test-node": "tap --timeout 240 \"test/tap/*.js\" \"test/network/*.js\" \"test/broken-under-nyc*/*.js\""
  },
  "license": "Artistic-2.0"
}

package.json属性

  • name - 包的名称

  • version - 包的版本

  • description - 包的描述

  • homepage - 包的网站主页

  • author - 包的作者

  • contributors - 包的贡献者的名称列表

  • dependencies - 依赖性列表。npm自动安装所有在node_module文件夹中的包提到的依赖关系。

  • repository - 包的存储库类型和URL

  • main - 包的入口点

  • keywords - 关键字

卸载模块

使用以下命令卸载模块

D:\nodejs> npm uninstall express

当npm卸载的软件包,可以通过查看<用户目录>/npm/node_modules 的内容验证。或键入以下命令:

D:\nodejs> npm ls

更新模块

更新的package.json并更改其版本依赖关系进行更新,运行以下命令。

D:\nodejs> npm update

搜索模块

根据npm命令搜索包名

D:\nodejs>npm search express

创建一个模块

创建模块需要要生成package.json。让我们使用 npm 产生 package.json。

这里写图片描述
一旦的 package.json 产生。使用一个有效的电子邮件地址在npm库网站上注册自己,使用下面的命令。
这里写图片描述
现在,是时候来发布模块了,使用publish:

D:\nodejs> npm publish

Node.js - 回调概念

什么是回调?

回调是一种异步相当于一个函数。回调函数被调用在完成既定任务。Node大量使用了回调。Node所有的API写的都是支持回调的这样一种方式。例如,一个函数读取一个文件可能开始读取文件,并立即返回控制到执行环境 使得下一个指令可以马上被执行。一旦文件 I/O 完成,它会调用回调函数,同时传递回调函数,该文件作为参数的内容。因此不会有堵塞或等待文件I/O。这使得Node.js高度可扩展,因此可以处理大量的请求,而无需等待任何函数来返回结果。

阻塞代码实例

创建一个 txt 文件,test.txt 在D:\nodejs\node 目录
这里写图片描述
验证:
这里写图片描述

非阻塞代码实例

//非阻塞代码例子
var fs=require('fs');
fs.readFile('input.txt',function(err,data){
    if(err) 
        return console.error(err);
    console.log(data.toString);
});

console.log("Program Ended");

验证:
这里写图片描述
这样就代表成功啦~~


node.js系列教程是博主学习node.js时的记录,所以记录的内容并不全面,想要深入了解的同学可以去易百教程学习~博主也用的这个∩_∩~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值