Node.js How to Elegantly Package a Utility Function NPM Pack

Why is it to encapsulate an NPM package of a practical function?

When you have written multiple Node applications, you want console to be beautiful, you might be encapsulated below. Function

Import chalk from “chalk”; function noop (msg: string): string {return msg} function log (function): (msg: string) = > void {return (msg: string): void => {console.log (fn (msg));};} export default {success}: log (, error: log (, WARN: Log (chalk.yellow.cyan), log: log (noop)};

Import log from ‘./log’log.success (“Hello Green Message!”) Log.error (“Hello Red Message!”) Log.warn (“Hello Yellow Message!”) (“Hello Cyan Message!”) Log.log (“Hello Normal Message!”)
Slowly discovering that each project requires such a log file, yes ! We can consider the set of these commonly used functions at this time.Inside an NPM package!

The increase in code quantity and the increase in contributors are necessary to join the unit test

Test Framework – MOCHA is a feature-rich JavaScript test framework, which can run in Node.js and browse In the device, support for BDD, TDD, QUnit, Exports

Assessment library – Assert is Node comes with

continuous integration

  • Travis Ci is provided is a Continuous Integration Service (CI). It binds the project above Github, as long as there is a new code, it will be automatically captured. Then, provide a running environment, perform testing, completing build, and deploy to the server.

Code level


When the SRC directory new Increase a utility file, index.ts can automatically capture and then mounted on module.export


Differential functions of lazy loading via getter

Object.defineProperty (Module.export, ‘log’, {Configurable: false, enumerable: true, get: () => reguire (‘./ log’)};

Project address Welcome & # 127775; Star & # 127775; and PR and use in project ~
 The above is all the content of this article, I hope to help everyone, I hope everyone will support Tumi Clouds.                      
© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment