【学习笔记】nodejs之包管理npm/yarn

来源:互联网 发布:微分销源码 .net 编辑:程序博客网 时间:2024/06/13 03:11

NPM: node package management
yarn: Yet Another Resource Negotiator
npm官方连接:npm 官网
本地创建package.json,写入以下内容

{"name": "my-example-app","version": "0.1.0","dependencies": {"request": "*","nano": "3.3.x","async": "~0.2"}}

运行npm install 或者yarn install,等待执行完成,在运行npm ls,会有一个文件树。

$ npm installnpm WARN deprecated node-uuid@1.4.8: Use uuid module insteadnpm notice created a lockfile as package-lock.json. You should commit this file.npm WARN my-example-app@0.1.0 No descriptionnpm WARN my-example-app@0.1.0 No repository field.npm WARN my-example-app@0.1.0 No license field.$ npm lsmy-example-app@0.1.0 /home/ec2-user/learn_node├── async@0.2.10├─┬ nano@3.3.8│ ├── errs@0.2.4│ ├─┬ follow@0.8.0│ │ └── request@2.2.9│ ├─┬ request@2.16.6│ │ ├── aws-sign@0.2.0│ │ ├── cookie-jar@0.2.0│ │ ├── forever-agent@0.2.0│ │ ├─┬ form-data@0.0.10│ │ │ ├── async@0.2.10 deduped│ │ │ ├─┬ combined-stream@0.0.7│ │ │ │ └── delayed-stream@0.0.5│ │ │ └── mime@1.2.11 deduped│ │ ├─┬ hawk@0.10.2│ │ │ ├─┬ boom@0.3.8│ │ │ │ └── hoek@0.7.6 deduped│ │ │ ├─┬ cryptiles@0.1.3│ │ │ │ └── boom@0.3.8 deduped│ │ │ ├── hoek@0.7.6│ │ │ └─┬ sntp@0.1.4│ │ │   └── hoek@0.7.6 deduped│ │ ├── json-stringify-safe@3.0.0│ │ ├── mime@1.2.11│ │ ├── node-uuid@1.4.8│ │ ├── oauth-sign@0.2.0│ │ ├── qs@0.5.6│ │ └── tunnel-agent@0.2.0│ └── underscore@1.4.4└─┬ request@2.81.0  ├── aws-sign2@0.6.0  ├── aws4@1.6.0  ├── caseless@0.12.0  ├─┬ combined-stream@1.0.5  │ └── delayed-stream@1.0.0  ├── extend@3.0.1  ├── forever-agent@0.6.1  ├─┬ form-data@2.1.4  │ ├── asynckit@0.4.0  │ ├── combined-stream@1.0.5 deduped  │ └── mime-types@2.1.17 deduped  ├─┬ har-validator@4.2.1  │ ├─┬ ajv@4.11.8  │ │ ├── co@4.6.0  │ │ └─┬ json-stable-stringify@1.0.1  │ │   └── jsonify@0.0.0  │ └── har-schema@1.0.5  ├─┬ hawk@3.1.3  │ ├─┬ boom@2.10.1  │ │ └── hoek@2.16.3 deduped  │ ├─┬ cryptiles@2.0.5  │ │ └── boom@2.10.1 deduped  │ ├── hoek@2.16.3  │ └─┬ sntp@1.0.9  │   └── hoek@2.16.3 deduped  ├─┬ http-signature@1.1.1  │ ├── assert-plus@0.2.0  │ ├─┬ jsprim@1.4.1  │ │ ├── assert-plus@1.0.0  │ │ ├── extsprintf@1.3.0  │ │ ├── json-schema@0.2.3  │ │ └─┬ verror@1.10.0  │ │   ├── assert-plus@1.0.0  │ │   ├── core-util-is@1.0.2  │ │   └── extsprintf@1.3.0 deduped  │ └─┬ sshpk@1.13.1  │   ├── asn1@0.2.3  │   ├── assert-plus@1.0.0  │   ├─┬ bcrypt-pbkdf@1.0.1  │   │ └── tweetnacl@0.14.5 deduped  │   ├─┬ dashdash@1.14.1  │   │ └── assert-plus@1.0.0  │   ├─┬ ecc-jsbn@0.1.1  │   │ └── jsbn@0.1.1 deduped  │   ├─┬ getpass@0.1.7  │   │ └── assert-plus@1.0.0  │   ├── jsbn@0.1.1  │   └── tweetnacl@0.14.5  ├── is-typedarray@1.0.0  ├── isstream@0.1.2  ├── json-stringify-safe@5.0.1  ├─┬ mime-types@2.1.17  │ └── mime-db@1.30.0  ├── oauth-sign@0.8.2  ├── performance-now@0.2.0  ├── qs@6.4.0  ├── safe-buffer@5.1.1  ├── stringstream@0.0.5  ├─┬ tough-cookie@2.3.2  │ └── punycode@1.4.1  ├─┬ tunnel-agent@0.6.0  │ └── safe-buffer@5.1.1 deduped  └── uuid@3.1.0

再看一下项目所在根目录下会多一个node_modules文件夹。
所有的以来关系都安装完了以后,我们就可以引用了。

var request = require('request');var nano = require('nano');var async = require('async');

NPM会默认在node_modules文件夹中搜索你引入的模块,如果在这个文件夹中找不到模块,nodejs会到上一层目录里搜索,直到root目录下。

参考文献:
instant nodejs starter

原创粉丝点击