npm 的安装与使用

创建: 2019/04/06

完成: 2019/04/07

 

 安装

 npm写在node.js里, 故安装node.js即可

 https://nodejs.org/en/download/

 确认是否安装 

node -v

 

 

 安装包

 

npm install <package_name>

 

 ● 本地安装与全局安装

 

 在本地安装指定包

 ● 在当前目录下创建 node_modules 文件夹(如不存在), 并将下载的包保存于此

-g  将包装在全局环境

 ● 安装的版本

   当前目录中有 package.json 时安装文件内指定的版本

   没有的话安装最新版

 ● 使用已安装的包

   安装后可以require

 package.json

 

{
    "name": "sample",
    "version": 1.0.0
}

 ● 目的: 管理本地安装的npm包

 ● 必须有name, version

 name

 ● 全部小写

 ● 可以用如下字符

# dash
'
# underscore
_
# hyphen
-

 ● 不能有空格

 version  ● 如下形式 
x.x.x
#
1.0.0
1.0.1
1.5.2
2.0.7

 

 

 创建package.json

  

npm init

 会有问卷, 根据回答来创建(跳过的设为默认值)

 --yes

 -y

 根于当前文件夹来推测

npm init --yes
npm init -y
  • name: the current directory name
  • version: always 1.0.0
  • description: info from the readme, or an empty string ""
  • main: always index.js
  • scripts: by default creates an empty test script
  • keywords: empty
  • author: empty
  • licenseISC
  • bugs: info from the current directory, if present
  • homepage: info from the current directory, if present
 指定需要的库

 

 dependencies  所有环境下
 devDependencies  development和test环境下

 

 

 安装package.json指定的库  
npm install
 
 查看有更新的包

 ● 本地包

npm outdated

 ● 全局包

npm outdated -g --depth=0

 

 更新本地安装的包  
npm undate

 

 拆卸安装的包   
npm uninstall <package>
# 同时删除package.json里的描述
npm uninstall <package> --save # dependencies
npm uninstall <package> --save-dev #devDependencies

 

 安装全局包  
npm install -g <package>

 

 更新全局包   
npm update -g

 

 拆卸全局包   
npm uninstall -g <package>

 

 脚本运行

 脚本写法scripts属性内

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "haha": "rails -v",
}

 ● 查看可运行脚本

npm run

 

 ● 运行自定义脚本 

npm run <自定义脚本名>
# 例
npm run haha

 

   
   
posted @ 2019-04-06 06:13  懒虫哥哥  阅读(461)  评论(0编辑  收藏  举报