I have several apps in node that all share a few modules that I've written. These modules are not available via npm. I would like to be able to share freely between apps, but I don't want to copy directories around, nor rely on Git to do so. And I'm not really big on using symlinks to do this either.
I would like to arrange directories something 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'm seeing 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 whichever app they are running in. At least I think that is the problem.
So....what is a good way to do this that avoids having duplicates of files? (note that I don't care about duplicates of things in node_modules, since those aren't my code, I don't check them into Git, etc)
Being able to call Node. js modules from JavaScript running in the browser has many advantages because it allows you to use Node. js modules for client-side JavaScript applications without having to use a server with Node.
You can copy but it will be of no use, its as simple as copy the package. json entry for "react-native-fetch-blob" and do npm install. Also you can do "npm install react-native-fetch-blob@<version>" if you want a specific version else latest will be installed.
Node. js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices. Node. js is an open source, cross-platform runtime environment for developing server-side and networking applications.
The npm documentation recommends using npm-link to create your own Node.js packages locally, and then making them available to other Node.js applications. It's a simple four-step process.
A typical procedure would be to first create a package with the following structure:
hello | index.js | package.json
A typical implementation of these files would be:
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 will refuse to publish the package. This is a way to prevent accidental publication of private packages.
Next, navigate to the root of your Node.js package folder and run npm link
to link the package globally so it can be used in other applications.
To use this package in another application, e.g., "hello-world", with the following directory structure:
hello-world | app.js
Navigate to the hello-world folder and run:
npm link hello
Now you can use it like any other npm package like so:
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);
I've got this working by having node_modules folders at different levels - node then automatically traverses upwards until it finds the module.
Note you don't have to publish to npm to have a module inside of node_modules - just use:
"private": true
Inside each of your private package.json files - for your project I would 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 point is node.js has a mechanism for dealing with this already and it's awesome. Just combine it with the 'private not on NPM' trick and you are good to go.
In short a:
require('somemodule')
From app A or B would cascade upwards until it found the module - regardless if it lived lower down or higher up. Indeed - this lets you hot-swap the location without changing any of the require(...) statements.
node.js module documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With