青训营 - 快乐出发 Flag

81 阅读1分钟

今天是Flag打卡的Day2

1、NVM: Node Version Manager

1.1 Mac 安装 nvm

https://github.com/nvm-sh/nvm/blob/master/README.md

1.2 Windows 安装 nvm

nvm-windows
nodist

2、NPM: Node Package Manager

2.1 全局安装package

$ npm install forever --global (-g)
$ forever
$ npm uninstall forever --global
$ forever

全局安装包的目录

  • Mac

    /Users/felix/.nvm/versions/node/nvm各个版本/bin/
    
  • Windows

    C:\Users\你的用户名\AppData\Roaming\npm\node_modules
    

2.2 本地安装package

$ cd ~/desktop
$ mkdir gp-project
$ cd gp-project
$ npm install underscore
$ npm list (ls)

2.3 package.json初始化

$ pwd
$ npm init -y
$ ls
$ cat package.json

2.4 使用package.json

$ npm install underscore --save
$ cat package.json
$ npm install lodash --save-dev
$ cat package.json
$ rm -rf node_modules
$ ls
$ npm install
$ npm uninstall underscore --save
$ npm list | grep underscore
$ cat package.json

2.5 安装指定版本的包

$ pwd
$ npm list
$ npm info underscore
$ npm view underscore versions
$ npm install underscore@1.8.0
$ npm list
$ npm uninstall underscore
$ npm list

2.6 更新本地安装的包

$ npm info underscore
$ npm view underscore versions
$ npm install underscore@1.4.4 --save-dev
$ npm list | grep gulp
$ npm outdated //~2.0.0表示patch, ^2.0.0表示minor * 表示xx最新版本
$ npm list | grep gulp
$ npm update

2.7 清除缓存

npm cache clean --force

2.8 上传自己的包

2.8.1 编写模块

保存为index.js

exports.sayHello = function(){ 
  return 'Hello World'; 
}
2.8.2 初始化包描述文件

$ npm init package.json

{ 
  "name": "gp19-npm", 
  "version": "1.0.1", 
  "description": "gp19 self module", 
  "main": "index.js",
  "scripts": { 
    "test": "make test" 
  }, 
  "repository": { 
    "type": "Git", 
    "url": "git+https://github.com/lurongtao/gp19-npm.git" 
  }, 
  "keywords": [ 
    "demo" 
  ], 
  "author": "Felixlu", 
  "license": "ISC", 
  "bugs": { 
    "url": "https://github.com/lurongtao/gp19-npm/issues" 
  }, 
  "homepage": "https://github.com/lurongtao/gp19-npm#readme", 
}
2.8.3 注册npm仓库账号
https://www.npmjs.com 上面的账号
felix_lurt/qqmko09ijn
$ npm adduser
2.8.4 上传包
$ npm publish

坑:403 Forbidden

查看npm源:npm config get registry
切换npm源方法一:npm config set registry http://registry.npmjs.org
切换npm源方法二:nrm use npm
2.8.5 安装包
$ npm install gp19-npm
2.8.6 卸载包
查看当前项目引用了哪些包 :
npm ls
卸载包:
npm unpublish --force
2.8.7 使用引入包
var hello = require('gp19-npm')
hello.sayHello()