美文网首页
NPM 常用命令

NPM 常用命令

作者: 二三筆 | 来源:发表于2018-10-28 19:12 被阅读0次

这里介绍的命令有如下:

  • npm -v
  • npm init
  • npm install
  • npm list
  • npm uninstall

更新 npm

npmNode.js 更频繁地更新,因此请确保您拥有最新版本。

c:\ > npm -v
6.3.0
c:\ > npm install npm@latest -g     // 需要全局安装

初始化包管理 init

管理本地安装 npm 包的最好方式就是创建 package.json 文件,使用 init 就能创建并初始化 package.json 文件。

c:\ > npm init
This utility will walk you through creating a package.json file.                      
It only covers the most common items, and tries to guess sensible defaults.           
                                                                                      
See `npm help json` for definitive documentation on these fields                      
and exactly what they do.                                                             
                                                                                      
Use `npm install <pkg>` afterwards to install a package and                           
save it as a dependency in the package.json file.                                     
                                                                                      
Press ^C at any time to quit.                                                         
package name: (npmlearn) ftest                                                        
version: (1.0.0)                                                                      
description: test for the first time;                                                 
entry point: (index.js)                                                               
test command: npm test                                                                
git repository:                                                                       
keywords:                                                                             
author: zys                                                                           
license: (ISC)                                                                        
About to write to c:\Users\叶子\Desktop\npmLearn\package.json:                          
                                                                                      
{                                                                                     
  "name": "ftest",                                                                    
  "version": "1.0.0",                                                                 
  "description": "test for the first time;",                                          
  "main": "index.js",                                                                 
  "scripts": {                                                                        
    "test": "npm test"                                                                
  },                                                                                  
  "author": "zys",                                                                    
  "license": "ISC"                                                                    
}                                                                                     
                                                                                      
                                                                                      
Is this OK? (yes)              
c:\ >                                                        
  • package name:包的名字,默认为当前目录名称 (名字不能有大写)
  • version:版本,默认从 1.0.0 开始
  • description:自述文件的信息或空字符串 ""
  • git repository:git 仓库
  • keywords:关键词
  • author:作者
  • license: 许可证 ISC

其中还有 test command ,可以为空,npm test 为测试模块的命令。
当然,也可以 快速创建 默认值 package.json

c:\ > npm init --yes   // 亦可使用 npm init -y

安装模块 install

输入 install 帮助

c:\ > npm help isntall

可以看到如下信息

npm install (with no args, in package dir)
npm install [<@scope>/]<name>
npm install [<@scope>/]<name>@<tag>
npm install [<@scope>/]<name>@<version>
npm install [<@scope>/]<name>@<version range>
npm install <git-host>:<git-user>/<repo-name>
npm install <git repo url>
npm install <tarball file>
npm install <tarball url>
npm install <folder>

alias: npm i        // 别名
...

可以知道,安装的基本语法为 npm install <module>
可以在后面加上版本,即 @<version>
安装最新版本: 如果 @latest 则为安装最新版本,
安装版本范围: 如 npm install sax@">=0.1.0 <0.2.0"
全局安装: 加上 -g,例如 npm install sax -g 则为全局安装。

关于 install 还有很多,想了解更多可以输入 npm help isntall 查看。
另外,如果初始化时 git repository 选项为空,则会有如下的警告:

c:\ > npm install less
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN npmlearn@1.0.0 No repository field.

可知为仓库的问题,可以在 package.json 设为私有,亦可为其加上随意一个地址。

{
    ...
    "private": true,
    ...
}
// 亦可以如此
{
    ...
    "repository": {
          "type": "git",
          "url": "www.baidu.com"
    },
    ...
}

查看安装的模块 list

查看当前文件下安装的模块,可以使用 list 查看

c:\ > npm list 

查看全局的包加上 -g 参数 (不是当前项目中的包)

c:\ > npm list  -g

还可以查看具体的模块名,例如查看 less

c:\> npm list  less

删除模块 uninstall

删除需不需要的模块,可以使用 uninstall

c:\ > npm uninstall less
removed 59 packages in 4.881s

先介绍到这里了,后续有需要会继续加上。

相关文章

  • npm 常用命令缩写

    npm 常用命令缩写,不断更新 npm i === npm install npm i webpack -D ==...

  • ionic常用命令

    常用命令: npm install -g cnpm --registry=https://registry.npm...

  • [mark] npm 了解一下~

    npm 常用命令及解析

  • npm常用命令

    npm常用命令 npm init 生成package.json文件 npm install 安装package.j...

  • Yarn 常用命令

    Yarn 常用命令 ``` npm install === yarn —— install安装是默认行为 npm ...

  • nodejs npm 常用命令

    国内镜像 设置代理 npm设置列表 npm config 所有的api npm 常用命令npm查看全局安装过的包 ...

  • Angular 学习笔记

    几个常用命令 npm + ng npm i --save 包名:软件依赖 npm i --save-dev 包名...

  • NPM

    npm 常用命令 1.react-native --version ...

  • npm命令

    1.查看npm 版本 2.npm淘宝镜像 3.npm常用命令说明 4.npm卸载 5.npm更新 6.npm清除缓...

  • NPM一些常用命令

    欢迎移步我的博客阅读:《NPM一些常用命令》 关于NPM NPM的全称是Node Package Manager,...

网友评论

      本文标题:NPM 常用命令

      本文链接:https://www.haomeiwen.com/subject/fvxjtqtx.html