博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
npm入手笔记0x004-npm命令及配置文件说明(未完善)
阅读量:6909 次
发布时间:2019-06-27

本文共 5334 字,大约阅读时间需要 17 分钟。

0x000 概述

本篇文章承接,记录的package.json的配置和npm命令的详细说明。

0x001 package.json的配置

  • name

    • 说明:项目名称,npm install的时候就是使用这个name

    • 案例:lodash@followwinter/lodash

  • version

    • 说明:版本号,符合npm的版本规范的版本号,默认从1.0.0开始。

    • 案例:1.0.02.0.1

  • description

    • 说明:项目的简介,如果不写会默认读去README.md的第一样作为npmjs搜索时候的简介

    • 案例:这是一个好项目

  • 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.jsvar 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.27git+ssh://git@github.com:npm/npm#semver:^5.0git+https://isaacs@github.com/npm/npm.gitgit://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 
    `的时候回将`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 [
    | major | minor | patch | premajor | preminor | prepatch | prerelease | from-git]'npm [-v | --version]' 查看`npm`版本'npm view
    version' 查看某个包的版本'npm ls' 列出当前依赖的包
  • cache:缓存

    'npm cache clean [
    ]' 清除缓存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>/]
    npm install [<@scope>/]
    @
    npm install [<@scope>/]
    @
    npm install [<@scope>/]
    @
    npm install
    :
    /
    npm install
    npm install
    npm install
    npm install
    alias: npm icommon options: [-P|--save-prod|-D|--save-dev|-O|--save-optional] [-E|--save-exact] [-B|--save-bundle] [--no-save] [--dry-run]
  • upinstall:卸载

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

    npm update [-g] [
    ...]aliases: up, upgrade
  • publish:发布

    npm publish [
    |
    ] [--tag
    ] [--access
    ]Publishes '.' if no argument suppliedSets tag 'latest' if no --tag specified
  • unpublish:取消发布

    npm unpublish [<@scope>/]
    [@
    ]
  • whoami:包所属

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

    npm dist-tag add 
    @
    [
    ]npm dist-tag rm
    npm dist-tag ls [
    ]aliases: dist-tags
  • config:设置配置

    npm config set 
    [-g|--global]npm config get
    npm config delete
    npm config list [-l]npm config editnpm get
    npm set
    [-g|--global]aliases: c
  • adduser:添加用户

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

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

0x003 总结

多看官方文档才是王道,知道如何找到自己想找的资料才是真正的办法,死命去记是没有办法的。

0x004 资源

转载地址:http://wefcl.baihongyu.com/

你可能感兴趣的文章
【LeetCode】初级算法-136.只出现一次的数
查看>>
分布式(一) 搞定服务注册与发现
查看>>
精读《手写 SQL 编译器 - 回溯》
查看>>
Spring XML MongoDB连接配置指定用户名和密码注意事项
查看>>
jvm内存区域
查看>>
PHP三种数组合并方式区别示例
查看>>
Golang 在 Mac、Linux、Windows 下如何交叉编译
查看>>
Linux Shell编程(5) - 正则表达式
查看>>
Jena ARQ小试牛刀
查看>>
Mac 神兵利器(二) 极简软件清单
查看>>
有赞跨平台长连接组件设计及可插拔改造
查看>>
小会计记账 小程序 走一波
查看>>
vue-router小记
查看>>
python的“=”与C++的区别
查看>>
快速排序就这么简单
查看>>
腾讯公司副总裁曾宇:技术必须产生价值,开源需要携手发展
查看>>
jsonp 解决跨域问题
查看>>
微信协程库libco研究(三):协程的事件管理
查看>>
用nginx搭建简单的文件下载服务器
查看>>
Js/Jquery获取iframe中的元素 在Iframe中获取父窗体的元素方法
查看>>