关于npm:npm基本使用

10次阅读

共计 1639 个字符,预计需要花费 5 分钟才能阅读完成。

1. package.json 文件

作用:保留以后我的项目所应用的模块信息,作为包形容文件,当 node_modules 失落的时候,能够通过 package.json 文件疾速复原我的项目所应用的包

  1. 创立:通过 npm init 主动初始化

    E:\ 前端 \nodejs\ 实际 >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 init` 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: (实际) test // 创立项目名称
    version: (1.0.0) 0.0.1 // 我的项目版本号
    description: This is test // 我的项目形容
    entry point: (index.js) main.js// 我的项目入口文件
    test command:
    git repository: // 我的项目 github 地址
    keywords:
    author: zt // 我的项目作者
    license: (ISC)
    About to write to E:\ 前端 \nodejs\ 实际 \package.json: // package.json 文件
    
    {
      "name": "test",
      "version": "0.0.1",
      "description": "This is test",
      "main": "main.js",
      "scripts": {"test": "echo \"Error: no test specified\"&& exit 1"},
      "author": "zt",
      "license": "ISC"
    }
    
    
    Is this OK? (yes) yes

    因为 dependencies 选项,能够保留第三方包的依赖信息,如果 node_modules 删除了,能够通过 npm install 主动把`package.json 中的dependencies` 中的所有的依赖项都下载回来

    • 倡议每个我的项目根目录下都有一个 package.json 文件
    • 倡议执行 npm install 包名的时候都加上 --save 这个选项,目标是用来保留依赖项信息

2.npm

npm 有两层含意

  1. npm 网站

    https://www.npmjs.com/npm 官网,能够在该网址上上传包以及搜寻已有的包,只有在此处搜寻到的包能力被 npm 下载

  2. npm 命令行工具

    只有装置了 node 就会装置 npm,npm 也有版本,能够在命令行输出以下命令获取版本号

    npm --version
    // 或者
    npm -v

    降级 npm(本人降级本人)

    npm install --global npm

npm 常用命令

  • npm init

    生成 package.json 文件

    • npm init -y 能够跳过向导,疾速生成 package.json 文件
  • npm install

    一次性把 package.json 文件中 dependencies 选项中的依赖项全副装置

    • 简写:npm i
    • npm install 包名

      • 下载包
      • 简写:npm i 包名
    • npm install –save 包名 / npm install 包名 –save

      • 下载并且保留依赖项(package.json 文件中的 dependencies 选项)
      • 简写:npm i -S
  • npm uninstall 包名

    • 只删除包,然而如果有依赖项信息不会被删除
    • 简写:npm un 包名
  • npm uninstall –save 包名 / npm uninstall 包名 –save

    • 删除的同时也会把依赖项信息也删除
    • 简写:npm un -S
  • npm –help

    • 查看应用帮忙
  • npm 命令 –help

    • 查看指定命令的应用帮忙
正文完
 0