0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00

support for package.json

This adds basic support for situations where there is a package.json
with a "main" field.  That "main" module is used as the code that is
loaded when the package folder is required.
This commit is contained in:
isaacs 2011-01-27 17:57:45 -08:00 committed by Ryan Dahl
parent 45c0faf399
commit da2f4b2dc4
5 changed files with 53 additions and 0 deletions

View File

@ -69,6 +69,19 @@ interrupting once a file is found. Files ending in `'.node'` are binary Addon
Modules; see 'Addons' below. `'index.js'` allows one to package a module as
a directory.
Additionally, a `package.json` file may be used to treat a folder as a
module, if it specifies a `'main'` field. For example, if the file at
`./foo/bar/package.json` contained this data:
{ "name" : "bar",
"version" : "1.2.3",
"main" : "./lib/bar.js" }
then `require('./foo/bar')` would load the file at
`'./foo/bar/lib/bar.js'`. This allows package authors to specify an
entry point to their module, while structuring their package how it
suits them.
`require.paths` can be modified at runtime by simply unshifting new
paths onto it, or at startup with the `NODE_PATH` environmental
variable (which should be a list of paths, colon separated).

View File

@ -60,6 +60,34 @@ function statPath(path) {
return false;
}
// check if the directory is a package.json dir
var packageCache = {};
function readPackage(requestPath) {
if (packageCache.hasOwnProperty(requestPath)) {
return packageCache[requestPath];
}
var fs = NativeModule.require('fs');
try {
var jsonPath = path.resolve(requestPath, 'package.json');
var json = fs.readFileSync(jsonPath, 'utf8');
var pkg = packageCache[requestPath] = JSON.parse(json);
return pkg;
} catch (e) {}
return false;
}
function tryPackage(requestPath, exts) {
var pkg = readPackage(requestPath);
if (!pkg || !pkg.main) return false;
var filename = path.resolve(requestPath, pkg.main);
return tryFile(filename) || tryExtensions(filename, exts);
}
// check if the file exists and is not a directory
function tryFile(requestPath) {
var fs = NativeModule.require('fs');
@ -113,6 +141,10 @@ Module._findPath = function(request, paths) {
}
}
if (!filename) {
filename = tryPackage(basePath, exts);
}
if (!filename) {
// try it with each of the extensions at "index"
filename = tryExtensions(path.resolve(basePath, 'index'), exts);

View File

@ -0,0 +1 @@
exports.ok = "ok"

View File

@ -0,0 +1,3 @@
{"name":"package-name"
,"version":"1.2.3"
,"main":"package-main-module"}

View File

@ -67,6 +67,10 @@ var three = require('../fixtures/nested-index/three'),
assert.equal(threeFolder, threeIndex);
assert.notEqual(threeFolder, three);
common.debug('test package.json require() loading');
assert.equal(require('../fixtures/packages/main').ok, 'ok',
'Failed loading package');
common.debug('test cycles containing a .. path');
var root = require('../fixtures/cycles/root'),
foo = require('../fixtures/cycles/folder/foo');