02. Use of npm

  1. Usage scenarios for npm
    • Allows users to download third-party packages written by others from the NPM server for local use.
    • Allows users to download and install command-line programs written by others from the NPM server for local use.
    • Allows users to upload their own packages or command-line programs to the NPM server for others to use.
  2. npm is installed with nodejs, you can use "npm -v" to view the version number and test whether the installation is successful.
    Upgrade npm using the following command
    npm install npm -g
     
  3. Use the npm command to install and uninstall the module

    installation syntax format such as npm install <Module Name>
    Use npm to locally install the Node.js web framework module express, such as
    npm install express
     After installation, the express package is placed in the node_modules directory under the project directory, which can be directly referenced such as
    var express = require('express'); //Use require to import directly
     
    Uninstall syntax format such as npm uninstall <Module Name>
    uninstall express such as
    npm uninstall express
     After uninstalling, the package no longer exists in the /node_modules/ directory, you can use the following command to view
    above sea level ls
      

  4. Global installation and local installation

      Local installation Enter the following command in the terminal
    npm install express
      The directory of the local installation
        is the path/node_modules/ where the npm command is run. You 
      can import the local installation package through require() and install it

      globally. Enter the following command in the terminal
    npm install express -g
      全局安装目录为
        C:\Users\[当前用户名]\AppData\Roaming\npm 或者 node的安装目录
      可直接在命令行中使用

      如果你希望具备两者功能,则需要在两个地方安装它或使用 npm link

       
    可以通过npm list 或 npm ls查看安装信息,如
      例子1,查看所有的全局安全模块
    npm list -g
      例子2,查看单个本地安装模块
    npm list express
     
  5. package.json的定义

    package.json 位于模块的目录下,用于定义包的属性。如node_modules/express/package.json

    package.json属性说明
    name 包名
    version 版本
    description 包描述
    homepage 包官网url
    author 包作者姓名
    contributors 包的其他贡献者姓名
    dependencies

    依赖包列表,如果依赖包没有安装,npm会自动将包安装在node_modules目录下

    repository 包代码存放的地方类型,可以是git或svn,git可以放在github上
    main main字段指定了程序的主入口文件,require('moduleName')就会加载这个文件.这个字段的默认值是模块根目录下面的index.js
    keywords 关键字
       



  6. npm更新与搜索模块
    npm search <Module Name>
    npm update <Module Name>
     

  7. 创建模块
    首先,创建模块需先生成package.json,可使用以下命令并且根据提示要求输入信息生成
    npm init
     然后,在npm资源库中注册用户
    npm adduser
    Username: yonghuming
    Password: mima
    Email: (this IS public) [email protected]
     之后,发布模块
    npm publish
      发布后就可以用npm命令安装使用本模块了

  8. 模块版本号为X.Y.Z三位
    Z
    • 如果只是修复bug,需要更新Z位。
    Y
    • 如果是新增了功能,但是向下兼容,需要更新Y位。
    X
    • 如果有大变动,向下不兼容,需要更新X位。
    "argv": "0.0.x"表示依赖于0.0.x系列的最新版argv


  9. npm其它常用命令

    • npm help查看所有命令,npm help <command>查看指定命令
    • 在package.json所在目录使用npm install . -g 可先在本地安装当前命令行程序,可用于发布前的本地测试
    • npm update <package> 可以更新本地安装模块,npm update <package> -g 可以更新全局安装模块(命令行程序)
    • npm cache clear 清空本地缓存,可以解决用相同版本号发布新版本的冲突.
    • npm unpublish <package>@<version>可以撤销发布自己发布过的某个版本的代码

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326242301&siteId=291194637