How to implement a shared library in Node.js?

I am looking for something similar conceptually for a Windows DLL. As a specific example, suppose I have an encryption function that I would like to split into several unrelated projects. If I want to ideally change the implementation, I can do it, and each project has access to a new implementation. Is there a mechanism for this in Node.js?

+4
source share
1 answer

Take a look at this document , in particular the "Writing a Library" section.

If you are writing a program intended for use by others, the most important thing you need to specify in your package.json file is the main module. This is the module that is the entry point to your program.

and

If you have a lot of JavaScript code, then the custom should be placed in. / lib in your project.

Specify the main module in the package.json file. This is the module that your users will load when they require it ("your library"). This module should ideally display all the functions in your library.

If you want your users to be able to download submodules from the guts of your library, they will need to specify the full path to them. This is a lot of documentation work! It is better and more reliable to simply specify the main module, and then, if necessary, dynamically load what they need.

For example, you may have a flip library, which is a set of widget objects defined by files in flip / lib / widgets / * files. js. Instead of requiring your users ("flip / lib / widgets / blerg.js") to get a blerg widget, it's better to have something like: require ('flip'). loadWidget ('blerg').

+2
source

Source: https://habr.com/ru/post/1410834/


All Articles