How to use code between node.js applications?

I have several applications in node that all use several modules that I wrote. These modules are not available through npm. I would like to be able to freely share between applications, but I do not want to copy directories and do not rely on Git. And I'm not very big for using symbolic links for this.

I would like to arrange directories like this:

app1 server.js node_modules (public modules from npm needed for app1) lib (my own modules specific to app1) app2 server.js node_modules (public modules from npm needed for app2) lib (my own modules specific to app2) shared_lib (my own modules that are used in both app1 and app2) 

The problem I see is that the modules in shared_lib seem to get confused as to where to find the modules that will be in the node_modules directory of the application in which they are running. At least I think this is a problem.

So ... what is a good way to do this to avoid duplicate files? (note that I don't need duplicate things in node_modules, since this is not my code, I do not test them on Git, etc.)

+65
Jul 01 '12 at 1:22
source share
5 answers

This works for me, having the node_modules folders at different levels - node, then it automatically moves up until it finds a module.

Note that you do not need to publish to npm to have a module inside node_modules - just use:

 "private": true 

Inside each of your personal package.json file - for your project, I will have the following:

 app1 server.js node_modules (public modules from npm needed for app1) (private modules locally needed for app1) app2 server.js node_modules (public modules from npm needed for app2) (private modules locally needed for app2) node_modules (public modules from npm needed for app1 & app2) (private modules locally for app1 & app2) 

The node.js point has a mechanism to deal with this already and it's awesome. Just pair it with a non-NPM trick and you're good to go.

In short:

 require('somemodule') 

From application A or B, there will be a cascade up until it finds a module - regardless of whether it lived lower or higher. Indeed - this allows you to "change" the location without changing any require (...) statements.

node.js module documentation

+17
Jul 01 2018-12-12T00:
source share

The npm documentation recommends using the npm link to create your own Node.js packages locally and then make them available to other Node.js applications. This is a simple four-step process.

A typical procedure is to first create a package with the following structure:

  hello | index.js | package.json 

A typical implementation of these files is:

index.js

  exports.world = function() { return('Hello World'); } 

package.json

  { "name": "hello", "version": "0.0.1", "private": true, "main": "index.js", "dependencies": { }, "engines": { "node": "v0.6.x" } } 

"private: true" ensures that npm refuses to publish the package. This is a way to prevent accidental publication of private packages.

Then go to the root of the Node.js. package folder and run npm link to bind the package globally so that it can be used in other applications.

To use this package in another application, for example, "hello-world", with the following directory structure:

  hello-world | app.js 

Go to the hello-world folder and run:

  npm link hello 

Now you can use it like any other npm package, for example:

app.js

  var http = require('http'); var hello = require('hello'); var server = http.createServer(function(req, res) { res.writeHead(200); res.end(hello.world()); }); server.listen(8080); 
+44
Jul 01 '12 at 3:20
source share

Just use the correct path in the required call

For example, in server.js, which will be:

var moduleName = require ('../shared_lib/moduleName/module.js');

Important It is important to know that as soon as your path has the prefix "/", "../" or "./", the path refers to the calling file.

For more information on downloading a node module, visit: http://nodejs.org/docs/latest/api/modules.html

+4
Jul 01 2018-12-01T00:
source share

Yes, you can reference shared_lib from app1, but then you run into a problem if you want to package and deploy application1 to another environment, such as an AWS web server.

In this case, you better install your modules in shared_lib on app1 and app2 using "npm install shared_lib / module". It will also install all dependencies of the shared_lib modules in app1 and app2 and will encounter conflicts / duplicates.

See this: How to install a private NPM module without my own registry?

+3
Jan 13 '17 at 10:45
source share

If you look at node.js docs , you will see that Node.js understands the package.json file format, at least easily.

Basically, if you have a directory called foo , and in this directory there is a package.json file with a key-value pair: "main": "myCode.js" , then if you try require("foo") , and it finds this directory with a package.json inside, then it will use foo/myCode.js for the foo module.

So, with your directory structure, if each shared library has its own directory with such a simple package.json file inside, then your applications can get shared libraries:

 var lib1 = require('../shared_lib/lib1'); var lib2 = require('../shared_lib/lib2'); 

And this should work for both of these applications.

+1
Jul 01 2018-12-12T00:
source share



All Articles