• 如何开发一个npm包并发布


    一、安装nodejs

    不多说了,网上教程多得是

    二、创建自己的npm包

    目录结构

    • npm-test
      • a.js
      • b.js
      • package.json

    开发

    为了简单便于理解,就开发一个简单地hello程序

    a.js

    function hello(name){
      console.log("hello "+ name);
    }
    exports.hello=hello;
    

    b.js

    var h=require('./a');
    h.hello('Jarrick');
    

    使用命令npm init创建一个package.json

    {
      "name": "qzy-npm-test",
      "version": "1.0.1",
      "description": "npm包开发测试",
      "main": "a.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "repository": "",
      "keywords": [
        "qzy",
        "npm-test"
      ],
      "author": "quanzaiyu",
      "license": "ISC"
    }
    
    

    三、发布npm包

    首先,必须注册一个npm账号,自己去 https://www.npmjs.com 注册即可。此处不多说

    添加账户

    npm adduser
    

    填入自己的npm账户名、密码和邮箱即可

    发布npm包

    npm publish
    

    进入npm个人中心,可以看到自己的npm包已经发布在上面了

    获取npm包

    npm install <package-name>
    

    此处package-name使用qzy-npm-test即可
    可以看到,多了一个node_modules目录,里面多了一个qzy-npm-test文件夹,里面放的就是我们刚才创建的npm包,包含a.js、b.js、package.json三个文件

    使用npm包

    跟使用普通的npm包一样,问了测试简单,创建一个index.js,输入

    let a = require('qzy-npm-test')
    a.hello('qzy')
    

    执行node index即可看见输出了hello qzy

    更新npm包

    更新npm包也是使用npm publish命令发布,不过必须更改npm包的版本号,即package.json的version字段,否则会报错:

    npm ERR! publish Failed PUT 403
    npm ERR! code E403
    npm ERR! You cannot publish over the previously published version 1.0.0. : qzy-npm-test
    
    npm ERR! A complete log of this run can be found in:
    npm ERR!     C:\Users\quanzaiyu\AppData\Roaming\npm-cache\_logs\2017-09-12T07_59_18_829Z-debug.log
    

    成功之后的提示:

    λ npm publish
    + qzy-npm-test@1.0.1
    
      
    链接:https://www.jianshu.com/p/3ce0a9fec0bd 
  • 相关阅读:
    Atitti  css   transition Animation differ区别
    Atitit 游戏引擎物理系统(1)爆炸效果
    Atitit.rsa密钥生成器的attilax总结
    新特性AAtitti css3 新特性attilax总结titti css
    Atitit 异常的实现原理 与用户业务异常
    Atitit.uke 团队建设的组织与运营之道attilax总结
    atitit 业务 触发器原理. 与事件原理 docx
    Atitit 基于dom的游戏引擎
    Atitit 团队建设的知识管理
    Javascript判断页面刷新或关闭的方法(转)
  • 原文地址:https://www.cnblogs.com/javalinux/p/15631646.html
Copyright © 2020-2023  润新知