4

I'm looking for something similar conceptually to a Windows DLL. As a concrete example, suppose I have a function encrypt that I would like to share across several unrelated projects. If I want to change the implementation ideally I can do so once and every project has access to the new implementation. Is there a mechanism for doing this in Node.js?

Hoa
  • 19,858
  • 28
  • 78
  • 107

1 Answers1

2

Have a look at this document especially the section "Writing a Library"

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.

and

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').

thomas
  • 2,580
  • 1
  • 22
  • 28