NPM 常用命令

这里介绍的命令有如下:

  • 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

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • npm是一个node包管理和分发工具,已经成为了非官方的发布node模块(包)的标准。有了npm,可以很快的找到特...
    执著_7a69阅读 669评论 0 0
  • npm是什么 NPM的全称是Node Package Manager,是随同NodeJS一起安装的包管理和分发工具...
    街角仰望阅读 929评论 0 0
  • npm 包管理器的常用命令 测试环境为node>=8.1.3&&npm>=5.0.3 1, 首先是安装命令 2, ...
    BlackGan3阅读 5,220评论 0 1
  • npm是一个node包管理和分发工具,已经成为了非官方的发布node模块(包)的标准。有了npm,可以很快的找到特...
    We_Me阅读 300评论 0 1
  • 飞哥_4b4d阅读 185评论 0 0

友情链接更多精彩内容