网络科技

    今日:444| 主题:244965
收藏本版
互联网、科技极客的综合动态。

[其他] Node.js at Scale - npm Publishing Tutorial

[复制链接]
那种倒影成月 发表于 2016-10-4 19:50:19
76 2

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
With    Node.js at Scalewe are creating a collection of articles focusing on the needs of companies with bigger Node.js installations, and developers who already learned the basics of Node.  
  In this second chapter of    Node.js at Scaleyou are going to learn how to expand the npm registry with your own modules. This tutorial is also going to explain how versioning works.  
  Upcoming chapters for the Node.js at Scale series:

  
       
  •       Using npm
              
    •           npm Tricks and Best Practices        
    • SemVer and Module Publishing          (you are reading it now)        
    • Dependency Management      
            
  •       Node.js Internals Deep Dive
              
    • The Event Loop        
    • Garbage Collection        
    • Writing Native Modules      
            
  •       Building
              
    • Structuring Node.js Applications        
    • Clean Code        
    • Handling Async        
    • Event sourcing        
    • Command Query Responsibility Segregation      
            
  •       Testing
              
    • Unit testing        
    • End-to-end testing      
            
  •       Node.js in Production
              
    • Monitoring Node.js Applications        
    • Debugging Node.js Applications        
    • Profiling Node.js Applications      
            
  •       Microservices
              
    • Request Signing        
    • Distributed Tracing        
    • API Gateways      
          
  npm Module Publishing

  When writing Node.js apps, there are so many things on npm that can help us being more productive. We don't have to deal with low-level things like padding a string from the left because there are already existing modules that are (eventually) available on the npm registry.
  Where do these modules come from?

  The modules are stored in a huge registry which is powered by a CouchDB instance.
  The official public npm registry is at    https://registry.npmjs.org/. It is powered by a CouchDB database, which has a public mirror at    https://skimdb.npmjs.com/registry. The code for the couchapp is available at    https://github.com/npm/npm-registry-couchapp.  
  How do modules make it to the registry?

  People like you write them for themselves or for their co-workers and they share the code with their fellow JavaScript developers.
  When should I consider publishing?

  
       
  • If you want to share code between projects,   
  • if you think that others might run into the very same problem and you'd like to help them,   
  • if you have a bit (or even more) code that you think you can make use of later.  
  Creating a module

  First let's create a module:    npm init -yshould take care of it, as you've learned in theprevious post.  
  1. {
  2.   "name": "npm-publishing",
  3.   "version": "1.0.0",
  4.   "description": "",
  5.   "main": "index.js",
  6.   "scripts": {
  7.     "test": "echo "Error: no test specified" && exit 1"
  8.   },
  9.   "keywords": [],
  10.   "author": "",
  11.   "repository": {
  12.     "type": "git",
  13.     "url": "git+https://github.com/author/modulename"
  14.   },
  15.   "bugs": {
  16.     "url": "https://github.com/caolan/async/issues"
  17.   },
  18.   "license": "ISC"
  19. }
复制代码
Let's break this down really quick.    These fields in your      package.jsonare mandatory when you're building a module for others to use.      
  First, you should give your module a distinct    namebecause it has to be unique in the npm registry. Make sure it does not collide with any trademarks out there!    maindescribes which file will be returned when your users do a    require('modulename'). You can leave it as default or set it to any file in your project, but make sure you actually point it to a valid filename.  
      keywordsshould also be included because npm is going to index your package based on those fields and people will be able to find your module if they search those keywords in npm's search, or in any third party npm search site.  
      author, well obviously that's going to be you, but if anyone helps you develop your project be so kind to include them too! :) Also, it is very important to include where can people contact you if they'd like to.  
  In the    repositoryfield, you can see where the code is hosted and the    bugssection tells you where can you file bugs if you find one in the package. To quickly jump to the bug report site you can use    npm bug modulename.  
  #1 Licensing

  Solid license and licenses adoption helps    Node adoption by large companies. Code is a valuable resource, and sharing it has it's own costs.  
  Licensing is a really hard, but    this sitecan help you pick one that fits your needs.  
  Generally when people publish modules to npm they use the    MIT license.  
  The MIT License is a permissive free software license originating at the Massachusetts Institute of Technology (MIT). As a permissive license, it puts only very limited restriction on reuse and has therefore an excellent license compatibility.
  #2 Semantic Versioning

  Versioning is so important that it deserves its own section.
  Most of the modules in the npm registry follow the specification called semantic versioning. Semantic versioning describes the version of a software as 3 numbers separated by "."-s. It describes how this version number has to change when changes are made to the software itself.
  Given a version number MAJOR.MINOR.PATCH, increment the:
  
       
  • MAJOR version when you make incompatible API changes,   
  • MINOR version when you add functionality in a backwards-compatible manner, and   
  • PATCH version when you make backwards-compatible bug fixes.  
  Additional labels for the pre-release and the build metadata are available as extensions to the MAJOR.MINOR.PATCH format.
  These numbers are for machines, not for humans! Don't assume that people will be discouraged from using your libraries when you often change the major version.
            "If you break your API, think about your users and BUMP THAT MAJOR!" via @RisingStack #nodejs #semver   
​    You have to start versioning at 1.0!

  Most people think that doing changes while the software is still in "beta" phase should not respect the semantic versioning. They are wrong! It is really important to communicate    breaking changesto your users even in beta phase. Always think about your users who want to experiment with your project.  
  #3 Documentation

  Having a proper documentation is imperative if you’d like to share your code with others. Putting a    README.mdfile in your project’s root folder is usually enough, and if you publish it to the registry npm will generate a site like    this one. It's all done automatically and it helps other people when they try to use your code.  
  Before publishing, make sure you have all documentation in place and up to date.
  #4 Keeping secret files out of your package

  Using a specific file called    .npmignorewill keep your secret or private files from publishing. Use that to your advantage, add files to    .npmignorethat you wish to not upload.  
  If you use    .gitignorenpm will use that too by default. Like git, npm looks for    .npmignoreand    .gitignorefiles in all subdirectories of your package, not only in the root directory.  
  #5 Encouraging contributions

  When you open up your code to the public, you should consider adding some guidelines for them on how to contribute. Make sure they know how to help you dealing with software bugs and adding new features to your module.
  There are a few of these available, but in general you should consider using    github's issue and pull-request templates.  
  npm publish

  Now you understand everything that's necessary to publish your first module. To do so, you can type:    npm publishand the npm-cli will upload the code to the registry.  
    Congratulations, your module is now public on the npm registry! Visit
          www.npmjs.com/package/yourpackagenamefor the public URL.   
    If you published something public to npm, it's going to stay there forever. There is little you can do to make it non-discoverable. Once it hits the public registry,    every other replicathat's connected to it will copy all the data.    Be careful when publishing.  
  I published something that I didn't mean to.

  We're human. We make mistakes, but what can be done now? Since the recent    leftpad scandal, npm changed the unpublish policy. If there is no package on the registry that depends on your package, then you're fine to unpublish it, but remember all the replicas will copy all the data so someone somewhere will always be able to get it. If it contained any secrets, make sure you change them after the act, and remember to add them to the    .npmignorefile for the next publish.  
            "If you accidentally published secrets to #npm change & add them to the .npmignore file!" via @RisingStack #nodejs   
​    Private Scoped Packages

  If you don't want or you're not allowed to publish code to a public registry (for any corporate reasons), npm allows organizations to open an organization account so that they can push to the registry without being public. This way you can share private code between you and your co-workers.
  Further read on how to set it up:    https://docs.npmjs.com/misc/scope  
  npm enterprise

  If you'd like to further tighten your security by running a registry by yourself, you can do that pretty easily. npm has an on-premise version that can be run behind corporate firewalls. Read more about    setting up npm enterprise.  
  Build something!

  Now that you know all these things, go and build something. If you’re up for a little bragging, make sure you tweet us (    @risingstack) the name of the package this tutorial helped you to build! If you have any questions, you’ll find me in the comments.  
  Happy publishing!
友荐云推荐




上一篇:Automated Code Audit’s First Customer
下一篇:Amazon’s ComiXology is now creating its own original comics
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

wqih23711 发表于 2016-10-4 21:50:23
楼主,么么哒!
回复 支持 反对

使用道具 举报

狂徒 发表于 2016-10-7 02:41:28
待我长发及腰,少年娶我可好?
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表