Local NodeJS modules for complex application structures

I am currently involved in building a Windows 8 team using JavaScript. We use npm and browse browsers for dependency management and convert our modules into a format compatible with the AMD browser.

One problem we are facing is crazy ways. This is due to the fact that we have a top-level folder inside our "components" of the application. This folder contains a bunch of nested ui components / modules. These modules sometimes require lib utils and helpers, which are located in the lib directory.

So, for example, for a module living in "my / app / components / product / grid / item", you may need a helper module, which is located in "my / app / lib / helpers / view".

The required path is a little crazy and very ugly: they require ("../../../../Library/helpers/view");

We do our best to create applications in a modular way. Now I think that a suitable way to get closer to this is to make the modules of our components dependent on these auxiliary modules. I could put lib helpers in my own external git private repository, but that was painful in terms of providing other commands (plus git private repositories are slow). Plus, since these modules are used only in the application, it is a waste of time to make changes, push the changes, and then return to updating the application and npm. This is good for some, but if we really break it, it can quickly become old.

Could I do npm install "my / app / lib / helpers / view" inside the package.json components? But npm install will not automatically do this for us.

I know several other ways around this (NODE_PATH, maybe use the npm installation hook or maybe the npm preinstall script), but wanted to know if anyone has a similar problem and a good solution.

+3
source share
3 answers

You can put your "my/app/components/product/grid/item" node_modules/grid/item.js in node_modules/grid/item.js , and then when you require('grid/item') in your application code, you get a file that you need, with the syntax of the desired path. Just check node_modules/grid/item.js and any other files on git. The node_modules/ directory node_modules/ not even be at the top level, since the require algorithm used by node and the browser will look for the node_modules/ directories from the current path to / until it finds the corresponding module.

Just add "grid" to the "bundledDependencies" array in package.json so you don't accidentally install something on it.

You can learn more about checking node modules in git .

Read the Browser Reference section on Avoiding .. /../../../../../ for more information.

NODE_PATH is always a bad idea, and the browser does not support it. Never use it ever.

+7
source

One thing you could do is create an alias for your helpers in your config request ...

 require.config({ paths: { "helpers": "my/app/lib/helpers" } }); 

This will shorten some of your long journeys.

0
source

The problem with the require () function is that the paths are relative to the current file. You can put your modules in the node_modules directory, but this is the worst thing you could do. node_modules is the directory in which all third-party modules live. If you follow this simple rule, it’s very easy and convenient to constantly keep abreast of events, you can remove all the dependencies (remove node_modules) and just do npm install .

The best solution is to define your own require function and make it global. For example:

The structure of your project:

 my-project | tools |- docs |- logs |- conf `- src |- node_modules |- package.json |- mod.js |- a | `- b | `- c.js `- d `- app.js 

mod.js

 global.mod = function (file){ return require ("./" + file); }; 

app.js

 //This should be the first line in your main script require ("../mod"); //Now all the modules are relative from the `src` directory //You want to use the a/b/c.js module var c = mod ("a/b/c"); 

This is all simple. If you want to get a third-party module located in node_modules, use require (). If you want your own modules to use mod ().

And remember, node_modules is only for third-party modules, rule nΒΊ1.

0
source

All Articles