美文网首页
发布自己的npm包

发布自己的npm包

作者: lesdom | 来源:发表于2021-03-21 11:50 被阅读0次

    一、注册npm账号

    注册地址:https://www.npmjs.com/

    登录后记得要验证邮箱

    二、初始化要发布的项目

    1、搭建本地环境:安装node.js,包含了npm命令。

    2、新建目录,在该目录下,初始化项目:npm init

    package name: (firstnpm) npm-lee-first
    version: (1.0.0)
    description: study publish npm module
    entry point: (index.js)
    test command:
    git repository:
    keywords: console function
    author: lesdom
    license: (ISC)
    

    到这里就生成了一个package.json文件

    3、新建入口文件index.js

    function consoleFunc(arg) {
      console.log(arg)
    }
    
    module.exports = consoleFunc
    

    三、发布npm包

    1、查看npm配置

    npm config list

    2、将npm源设置为官方地址

    npm set registry https://registry.npmjs.org/

    3、登录账户

    npm adduser 登录npm账户

    npm whoami 可以通过这个指令查看登录的用户名

    4、发布项目

    npm publish

    5、这时候就已经发布成功了,可以在网站上登录自己的账户查看(头像->Packages)

    四、使用自己发布的包

    1、新建项目

    npm init 一路回车

    2、安装自己刚刚发布的包

    npm install npm-lee-first

    3、新建入口文件index.js

    const consoleFunc = require('npm-lee-first')
    
    consoleFunc('hello world!')
    

    运行node index.js 就能看到已经生效了

    五、更新自己的包及添加readme.md

    1、修改文件

    function consoleFunc(arg,user) {
      console.log(arg)
      var username = user ? user : 'developer'
      return {    
        msg: arg ? arg : 'hello '+username
      }
    }
    
    module.exports = consoleFunc
    

    2、更新版本

    npm version patch

    npm version后面参数说明:
    patch:小变动,比如修复bug等,版本号变动 v1.0.0->v1.0.1
    minor:增加新功能,不影响现有功能,版本号变动 v1.0.0->v1.1.0
    major:破坏模块对向后的兼容性,版本号变动 v1.0.0->v2.0.0

    3、发布

    npm publish

    六、安装使用更新后的包

    1、安装更新

    npm i npm-lee-first@1.0.1

    2、使用

    const consoleFunc = require('npm-lee-first')
    
    var obj = consoleFunc('','lee')
    console.log(obj)
    

    相关文章

      网友评论

          本文标题:发布自己的npm包

          本文链接:https://www.haomeiwen.com/subject/tguncltx.html