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);
almypal Jul 01 '12 at 3:20 2012-07-01 03:20
source share