npm入手筆記0x004-npm命令及配置文件說明(未完善)

0x000 概述

本篇文章承接上文,記錄的package.json的配置和npm命令的詳細說明。node

0x001 package.json的配置

  • namewebpack

    • 說明:項目名稱,npm install的時候就是使用這個namegit

    • 案例:lodash@followwinter/lodashgithub

  • versionweb

    • 說明:版本號,符合npm的版本規範的版本號,默認從1.0.0開始。express

    • 案例:1.0.02.0.1npm

  • description json

    • 說明:項目的簡介,若是不寫會默認讀去README.md的第同樣做爲npmjs搜索時候的簡介segmentfault

    • 案例:這是一個好項目緩存

  • keywords

    • 說明:關鍵詞

    • 案例:lodashjs

  • homepage

    • 說明:項目主頁

    • 案例:http://lodashjs.com/

  • license

    • 說明:協議

    • 案例:BSD-3-Clause

  • main

    • 說明:模塊ID

    • 案例:若是你的模塊名爲foo,若是一個用戶使用require('foo'),就會返回一個你export出來的主對象。例如以前咱們export一個printMsg,咱們直接@followwinter/0x007-local-global-diff1就獲得了一個對象,是由於咱們指定了@followwinter/0x007-local-global-diff1中的package.jsonmianindex。js

    //  @followwinter/0x007-local-global-diff1/package.json
    {
      "name": "@followwinter/0x007-local-global-diff1",
      "version": "1.0.2",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "ISC"
    }
    //  @followwinter/0x008-local-global-diff2/index.js
    var myModule = require('@followwinter/0x007-local-global-diff1');
    console.log(myModule);
    myModule.printMsg();
  • dependencies

    • 說明:項目的依賴庫

      • version: 必須等於該版本

      • >version:必須大於該版本

      • >=version: 必須大於等於該版本

      • <version: 必須小於該版本

      • <=version: 必須小於等於該版本

      • ~version: 大約等於該版本

      • ^version: 和該版本可兼容的版本

      • 1.2.x1.2.* 版本

      • http://...http地址

      • *:任意版本

      • "":任意版本

      • version1 - version2: 在version1version2之間,包含version1version2

      • range1 || range2:在範圍1或者範圍2之間

      • git...:git地址

      • user/repouser/repo地址

      • tag: 該tag的版本

      • path/path/path 本地地址

    • 案例:

    { "dependencies" :
      { "foo" : "1.0.0 - 2.9999.9999"
      , "bar" : ">=1.0.2 <2.1.2"
      , "baz" : ">1.0.2 <=2.3.4"
      , "boo" : "2.0.1"
      , "qux" : "<1.0.0 || >=2.3.1 <2.4.5 || >=2.5.2 <3.0.0"
      , "asd" : "http://asdf.com/asdf.tar.gz"
      , "til" : "~1.2"
      , "elf" : "~1.2.3"
      , "two" : "2.x"
      , "thr" : "3.3.x"
      , "lat" : "latest"
      , "dyl" : "file:../dyl"
      }
    }
    • 特殊說明

      • git地址格式:<protocol>://[<user>[:<password>]@]<hostname>[:<port>][:][/]<path>[#<commit-ish> | #semver:<semver>]

      • git地址示例:

      git+ssh://git@github.com:npm/npm.git#v1.0.27
      git+ssh://git@github.com:npm/npm#semver:^5.0
      git+https://isaacs@github.com/npm/npm.git
      git://github.com/npm/npm.git#v1.0.27
      //如今還能夠直接這麼寫
      {
        "name": "foo",
        "version": "0.0.0",
        "dependencies": {
          "express": "expressjs/express",
          "mocha": "mochajs/mocha#4727d357ea",
          "module": "user/repo#feature\/branch"
        }
      }
      • 本地地址

      ../foo/bar
      ~/foo/bar
      ./foo/bar
      /foo/bar
      // 如下寫法更優

      {
      "name": "baz",
      "dependencies": {

      "bar": "file:../foo/bar"

      }
      }

  • devDependencies

    • 說明:開發依賴

    • 案例:同上

  • bin:

    • 說明:可執行目錄

    • 案例:

    //`npm installl -g <package_name>`的時候回將`cli.js`複製到`/usr/local/bin/myapp`,就能夠使用`myapp`做爲命令了,好比`webpack`
    { "bin" : { "myapp" : "./cli.js" } }
  • scripts:

    • 說明:自定義命令,也能夠覆蓋自定義命令

    • 默認值:

      • "start": "node server.js":執行server.js

      • "install": "node-gyp rebuild":安裝依賴

    • 自定義指令:

    "test":"jtest",
    "build":"npm install && npm test && npm publish --access public"
    • 執行自定義指令:
      若是是覆蓋默認指令,直接使用默認指令即可,好比npm installnpm start,若是是自定義指令,則須要使用npm run <script_name>來調用,好比npm run build,會先執行npm install,再執行npm test,最後執行npm publish --access public.

0x002 npm命令

  • version:版本

    npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease | from-git]
    
    'npm [-v | --version]' 查看`npm`版本
    'npm view <pkg> version' 查看某個包的版本
    'npm ls' 列出當前依賴的包
  • cache:緩存

    'npm cache clean [<path>]' 清除緩存
    aliases: npm cache clear, npm cache rm
  • dedupe:重構

    'npm dedupe' 從新整理依賴包架構
    'npm ddp'
    
    aliases: find-dupes, ddp
    
    // 執行前
    
    +-- b <-- depends on c@1.0.x
    |   `-- c@1.0.3
    `-- d <-- depends on c@~1.0.9
        `-- c@1.0.10
        
    //執行後
    
    +-- b
    +-- d
    `-- c@1.0.10
  • init:初始化

    npm init [-f|--force|-y|--yes] 加了參數就不會提示任何信息了
  • install:安裝

    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
    common options: [-P|--save-prod|-D|--save-dev|-O|--save-optional] [-E|--save-exact] [-B|--save-bundle] [--no-save] [--dry-run]
  • upinstall:卸載

    npm uninstall [<@scope>/]<pkg>[@<version>]... [-S|--save|-D|--save-dev|-O|--save-optional|--no-save]
    
    aliases: remove, rm, r, un, unlink
  • update:更新

    npm update [-g] [<pkg>...]
    
    aliases: up, upgrade
  • publish:發佈

    npm publish [<tarball>|<folder>] [--tag <tag>] [--access <public|restricted>]
    
    Publishes '.' if no argument supplied
    Sets tag 'latest' if no --tag specified
  • unpublish:取消發佈

    npm unpublish [<@scope>/]<pkg>[@<version>]
  • whoami:包所屬

    npm whoami [--registry <registry>]
  • dist-tag:加tag

    npm dist-tag add <pkg>@<version> [<tag>]
    npm dist-tag rm <pkg> <tag>
    npm dist-tag ls [<pkg>]
    
    aliases: dist-tags
  • config:設置配置

    npm config set <key> <value> [-g|--global]
    npm config get <key>
    npm config delete <key>
    npm config list [-l]
    npm config edit
    npm get <key>
    npm set <key> <value> [-g|--global]
    
    aliases: c
  • adduser:添加用戶

    npm adduser [--registry=url] [--scope=@orgname] [--always-auth] [--auth-type=legacy]
    
    aliases: login, add-user
  • logout:退出用戶

    npm logout [--registry=<url>] [--scope=<@scope>]

0x003 總結

多看官方文檔纔是王道,知道如何找到本身想找的資料纔是真正的辦法,死命去記是沒有辦法的。

0x004 資源

相關文章
相關標籤/搜索