Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find a Solution for requiring External Dependencys from fs #36

Open
frank-dspeed opened this issue Oct 20, 2017 · 0 comments
Open

Find a Solution for requiring External Dependencys from fs #36

frank-dspeed opened this issue Oct 20, 2017 · 0 comments

Comments

@frank-dspeed
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant