如何在Node.js中实现共享库?

我正在寻找类似于
Windows DLL的概念.举一个具体的例子,假设我有一个函数加密,我希望在几个不相关的项目中共享.如果我想理想地改变实现,我可以这样做,每个项目都可以访问新的实现.是否有在Node.js中执行此操作的机制? 最佳答案 看看这个
document特别是“写一个图书馆”这一节

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

If you have a lot of JavaScript code, then the custom is to put it in
the ./lib folder in your project.

Specify a main module in your package.json file. This is the module
that your users will load when they do require(‘your-library’). This
module should ideally expose all of the functionality in your library.

If you want your users to be able to load sub-modules from the “guts”
of your library, then they’ll need to specify the full path to them.
That is a lot of work to document! It’s better and more future-proof
to simply specify a main module, and then, if necessary, have ways to
dynamically load what they need.

For example, you might have a flip library that is a collection of
widget objects, defined by files in the flip/lib/widgets/*.js files.
Rather than having your users do require(‘flip/lib/widgets/blerg.js’)
to get the blerg widget, it’s better to have something like:
require(‘flip’).loadWidget(‘blerg’).

点赞