最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Setting a path for require in node.js - Stack Overflow

programmeradmin1浏览0评论

I have a folder structure like this.

  • include/

    • index.js
    • plugin/

      • plugin.js
      • helper.js

Where:-

include/index.js

//Function for mapping the path of  "require" statement in  the plugin.js file.

var mapRequirePath = function(){

    var plugins = require('./plugin/plugin');
     return plugins;
}

//Now call the plugins..
var plugin = mapRequirePath();

include/plugin/plugin.js

    /*
       I want all four require statements to point to the same file location '/include/plugin/helper.js'

      i.e search in the same folder location for module irrespective of the '../' or '../../' present in the require statement
    */

    var helper1 = require('./helper');
    var helper2 = require('helper');
    var helper3 = require('../../helper');
    var helper4 = require('../helper');

I want to map the path of require in plugin.js file so that all require call should search for its module in the same directory only.

I have a folder structure like this.

  • include/

    • index.js
    • plugin/

      • plugin.js
      • helper.js

Where:-

include/index.js

//Function for mapping the path of  "require" statement in  the plugin.js file.

var mapRequirePath = function(){

    var plugins = require('./plugin/plugin');
     return plugins;
}

//Now call the plugins..
var plugin = mapRequirePath();

include/plugin/plugin.js

    /*
       I want all four require statements to point to the same file location '/include/plugin/helper.js'

      i.e search in the same folder location for module irrespective of the '../' or '../../' present in the require statement
    */

    var helper1 = require('./helper');
    var helper2 = require('helper');
    var helper3 = require('../../helper');
    var helper4 = require('../helper');

I want to map the path of require in plugin.js file so that all require call should search for its module in the same directory only.

Share Improve this question edited Nov 15, 2015 at 17:25 Robins Gupta asked Nov 15, 2015 at 16:55 Robins GuptaRobins Gupta 3,1534 gold badges35 silver badges58 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 6

You might be able to dynamically change the NODE_PATH environment variable:

// First take a backup:
var _NODE_PATH = process.env.NODE_PATH;
// Add /includes/plugin to the path, also note that we need to support 
//   `require('../hello.js')`. We can do that by adding /includes/plugin/a, 
//   /includes/plugin/a/b, etc.. to the list
process.env.NODE_PATH+=':/includes/plugin:/includes/plugin/a';
// Do your think...
require('./plugins/plugin');
// Restore NODE_PATH 
process.env.NODE_PATH = _NODE_PATH;

Try changing the NODE_PATH variable via the mand line:

exports NODE_PATH=directoryYouWant

If you don't want to have to change it for every other project, you could try just dynamically changing it in you .js file:

var currentNodePath = process.env.NODE_PATH;
process.env.NODE_PATH = directoryYouWant;
//do stuff then change it back
process.env.NODE_PATH = currentNodePath;

If your wanna add /foo/bar to require:

  • by editting process.env.NODE_PATH

then your js files should reside inside /foo/bar/node_modules/

process.env.NODE_PATH = '/foo/bar' + ':' + process.env.NODE_PATH;
  • by editting module.paths

then your js files should reside inside /foo/bar/

module.paths.push('/foo/bar');
发布评论

评论列表(0)

  1. 暂无评论