Best practice for including projects to Nodeclipse project

197 views Asked by At

I have a Nodeclipse project x, which can be considered as a library, in the form of javascript files. It has a root folder: 'r'. This folder has an 'index.js' file.

A typical module using x starts by: var r = require('r');.

This works well when running Node.js from the cmd line by simply adding the full path to 'r' to NODE_PATH.

Now in another Nodeclipse project y, I add project x as a reference in the 'projects' tab of the 'Include path' properties.

I am expecting that Nodeclipse will do the magic of adding the source folders of x to the include paths of y when running the project. If not what's the use of adding a project?.

However, when hitting: var r = require('r');, Node.js complains it can't find 'r'. Stepping into the source code for 'require', it is obvious that none of the source paths of project 'x' have been added to the include path.

What am I missing?

2

There are 2 answers

0
user2969819 On

for what it's worth, I have found a solution. my project structure consists of 3 root folders: - generated - main - test all residing under a folder 'src'.

I have created a script: exploded.js which resides in the src folder. All my test scripts start with: "require('../../exploded') (# of ../ depends on depth) This script overrides Module._resolveLookupPaths and enriches the candidate path list with corresponding paths in the main and generated folders.

Eric

Exploded.js code (didn't find a way to upload it):

var path = require("path");
var root = path.dirname(module.filename);
var fs = require("fs");

function readSubDirs() {
    var _children = fs.readdirSync(root);
    var children = [];
    for(var i=0;i<_children.length;i++) {
        var _child = _children[i];
        if(_child!=="node_modules") {
            if(_child.indexOf(".")!==0) {
                var _path = root + path.sep + _child;
                var _isdir = fs.lstatSync(_path).isDirectory();
                if( _isdir )  {
                    children.push(_child);
                }
            }
        }
    }
    return children;
}
var children = readSubDirs();

var m = require("module").Module;
var old_resolveLookupPaths = m._resolveLookupPaths;

function new_resolveLookupPaths(request, parent) {
    var resolved = old_resolveLookupPaths(request, parent);
    var start = request.substring(0, 2);
    if (start === './' || start === '..') {
        // is the calling module in the same hierarchy as this?
        if(parent.filename.search(root)===0) {
            var dirpath = path.dirname(parent.filename);
            var subpath = dirpath.substring(root.length+1);
            var idx = subpath.indexOf(path.sep);
            var folder = subpath.substring(0, idx);
            subpath = subpath.substring(idx);
            var paths = resolved[1];
            var more = [];
            for(var i=0;i<paths.length;i++) {
                var _path = paths[i];
                if(_path.indexOf(root)===0) {
                    if(_path.indexOf(subpath)>root.length) {
                        for(var j=0;j<children.length;j++) {
                            var _child = children[j];
                            if( _child!==folder) {
                                var _fullpath = root + path.sep + _child + subpath;
                                var _exists = fs.existsSync(_fullpath);
                                if(_exists) {
                                    var _isdir = fs.lstatSync(_fullpath).isDirectory();
                                    if( _isdir )  {                             
                                        more.push(_fullpath);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            resolved[1] = paths.concat(more);
        }
    }
    return resolved;
}

if(m._resolveLookupPaths !== new_resolveLookupPaths) {
    m._resolveLookupPaths = new_resolveLookupPaths;
}  
5
Paul Verest On

This is actually Node.js general question, and I just added node.js tag.

In short you should do it Node.js way, not Eclipse way.

Now in another Nodeclipse project y, I add project x as a reference in the 'projects' tab of the 'Include path' properties.

That is what you get used to using Eclipse JDT for Java.
For Node.js it should work without any IDE also:

  • To use specific state of project Y

from project x do

npm install . -g

then in project y

npm install x --save
  • To use the latest of project Y

Other way it to require(path/to/project_x)

or in project y

npm link path/to/project_x

Just learn Node.js, Eclipse and of course Nodeclipse :)

Nodeclipse "Enide Studio 2014" plugins have integrated terminal: