Skip to content

Find a Solution for requiring External Dependencys from fs #36

@frank-dspeed

Description

@frank-dspeed

steal-prerender could offer a middelware to link all modules from
the path

function npmls(cb) {
require('child_process').exec('npm ls --depth 0 --json', function(err, stdout, stderr) {
if (err) return cb(err)
cb(null, JSON.parse(stdout));
});
}
npmls(console.log);

npm.commands.ls(args, [silent,] callback)
console.log(Object.keys(require('./package.json').dependencies));

we will probally use rootRequire try catched

---- https://gist.github.com/branneman/8048520#comment-1412502
2. The Global

In your app.js:

global.__base = __dirname + '/';
In your very/far/away/module.js:

var Article = require(__base + 'app/models/article');


  1. The Wrapper

Courtesy of @a-ignatov-parc. Another simple solution which increases obviousness, simply wrap the require() function with one relative to the path of the application's entry point file.

Place this code in your app.js, again before any require() calls:

global.rootRequire = function(name) {
return require(__dirname + '/' + name);
}
You can then require modules like this:

var Article = rootRequire('app/models/article');


Install some module:

npm install app-module-path --save
In your app.js, before any require() calls:

require('app-module-path').addPath(__dirname + '/app');

or Use NODE_PATH=. or project root for require

hacky method

process.env.NODE_PATH = __dirname;
require('module').Module._initPaths();

I've been using symlinks with the following structure:

/node_modules
/package.json
/src
  /node_modules
    /client -> ../client
    /server -> ../server
    /shared -> ../shared
  /client
    /apps
      /main
        /test
          main.spec.js
        index.js
    /modules
      /foo
        /test
          foo.spec.js
        index.js
  /server
    /apps
    /modules
  /shared

it also solves the problem of not know where the modules come from because all app modules have client/server/shared prefixes in require paths

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions