How do you reference Node JS projects within a solution?

Nov 22, 2014 at 2:38 PM
Solution
-->Project_1
-->Project_2

Project_2 needs to reference Project_1

I could manually copy dependencies or set up grunt but I was hoping VS may have something build in already. Sort of like you can do it in C#

Cheers
Coordinator
Nov 22, 2014 at 7:53 PM
When you say "reference", what exactly do you expect it to do? In case of C#, a project has a single assembly as its output, and it is that assembly that is referenced by the other project. For Node projects, there are no outputs to speak of, and Node has its own resolution rules when you do a require(), that need the files on the filesystem be laid out in a certain way.
Nov 22, 2014 at 10:01 PM
Edited Nov 22, 2014 at 10:04 PM
What I meant was a way to reference modules from Project_1 in Project_2.

The doc here https://nodejstools.codeplex.com/wikipage?title=Projects says that "Solutions can contain multiple projects, including those of different types, and projects can include references to each other.". But it is unclear how and what you meant by it.

I could of course use one of the methods below but I thought may be there is a better way.

i. Method 1
Copy Project_1 into node_modules in Project_2,
Add dependency in package.json
Call require("Project_1")
dependencies : {
    "Project_1" : ">=0.0.1"
}

...

var module = require("Project_1");
ii. Method 2
Use require ("../Project_1") in Project_2

Method 1 is too manual and method 2 is too dependent on the specific directory structure.
Nov 23, 2014 at 2:08 AM
Modified method 1, but VS does not automatically copy the module for you so you have to npm install it in the cmd.
dependencies : {
    "Project_1" : "../Project_1"
}

...

var module = require("Project_1");
Coordinator
Nov 23, 2014 at 2:16 AM
Any MSBuild project can reference any other MSBuild project - this establishes a build dependency (if you build Project_2, it will automatically build Project_1 first). It does not establish a package dependency, though.

Ultimately, the behavior of require() is controlled by Node package resolution algorithm, as described here. NTVS doesn't really try to interfere with this in any way. So you can, for example, add the parent folder of both projects to NODE_PATH (there is a setting for environment variables in project properties), and then you should be able to use require - but 1) this will only work when you launch from VS, and you'll need to use .cmd scripts or similar to set the variable in the same manner when running your code manually, and 2) it breaks Node conventions.

A better option might be to arrange your projects on disk such that they're physically dependencies of each other - in other words, instead of copying Project_1 to Project_2's node_modules every time, just move it there on a permanent basis, and work on it directly from that folder, .njsproj file and all.
Coordinator
Nov 23, 2014 at 2:28 AM
BTW, one thing that NTVS could do in this case is automatically npm install the referenced project as if it were a module. Feel free to file a feature for this in the tracker. In the meantime, note that you can implement this yourself by adding an <Exec> task to BeforeBuild target in your .njsproj file.
Marked as answer by serg_1 on 11/22/2014 at 8:32 PM
Nov 23, 2014 at 3:32 AM
Edited Nov 23, 2014 at 3:33 AM
pminaev wrote:
BTW, one thing that NTVS could do in this case is automatically npm install the referenced project as if it were a module. Feel free to file a feature for this in the tracker. In the meantime, note that you can implement this yourself by adding an <Exec> task to BeforeBuild target in your .njsproj file.
I ended up doing the above. It is more logical and flexible to have npm install for the referenced project..

Thanks for your comments and suggestions
Nov 23, 2014 at 4:30 AM
Edited Nov 23, 2014 at 4:30 AM
One more thing - it is has to be npm install -f
Coordinator
Nov 23, 2014 at 4:38 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.