简体   繁体   English

加载模块node.js的最佳方法

[英]Best way to load modules node.js

My project has got many folders and I often load my own modules in node.js in the following way: 我的项目有很多文件夹,我经常通过以下方式将自己的模块加载到node.js中:

var request = require("request"),
    config  = require("../../../modules/config"),
    urls    = require("../../../modules/urls");

I sometimes move the folders around and the path changes, so I need to adjust the ../ part manually. 有时我会移动文件夹并更改路径,因此我需要手动调整../零件。

I don't want to move my modules into the node_modules folder, but I'd like to load the modules in the following way: 我不想将模块移动到node_modules文件夹中,但是我想通过以下方式加载模块:

var request = require("request"),
    config  = require("config"),
    urls    = require("urls");

or 要么

var request = require("request"),
    config  = require("modules/config"),
    urls    = require("modules/urls");

What are my options? 我有什么选择?

New Answer: 新答案:

Relative paths seem to be the simplest choice after all, it allows you to run your script from any location. 毕竟,相对路径似乎是最简单的选择,它允许您从任何位置运行脚本。

var config = require("../../config");

Old answer: 旧答案:

I found out that, while not ideal, there's also the possibility to use process.cwd() . 我发现,虽然不理想,但也可以使用process.cwd()

var request = require("request"),
    config  = require(process.cwd() + "/modules/config");

or, if the process.cwd() is set to a global variable in the main js file 或者,如果将process.cwd()设置为主js文件中的全局变量

global.cwd = process.cwd();

then 然后

var request = require("request"),
    config  = require(global.cwd + "/modules/config"),
    urls    = require(global.cwd + "/modules/urls");

You can try to do the following, based on some conditions 您可以根据某些情况尝试执行以下操作

  • if the scripts are exclusively written for your application, meaning it won't work with any other application, and the scripts don't have any dependencies place them under modules directory and try to create expose a variable such as global.basepath and using path.join to construct the filepath and require it.You could also inject module variable after you require them at the main script of your app. 如果脚本是专门为您的应用程序编写的,则意味着它不能与任何其他应用程序一起使用,并且这些脚本没有任何依赖关系,请将它们放置在modules目录下,并尝试创建公开变量,例如global.basepath并使用path.join来构造文件路径并要求它。您还可以在应用程序的主脚本中要求它们之后注入模块变量。

main.js

var config = require('modules/config.js')(_,mongoose,app);

modules/config.js

module.exports=function(_,mongoose,app){
 return function(){
  // _,mongoose,app
  this.loadConfigVariable..
 }
}
  • if the scripts are not one-files that have separate tests, require other vendor modules in order to be executed then create individual node modules and publish them to a registry for convenience when installing the main application. 如果脚本不是具有单独测试的单一文件,则需要其他供应商模块才能执行,然后创建单个节点模块并将它们发布到注册表中,以方便安装主应用程序。 Just as express does, there is the main application express and there are modules that express uses such as express-validation which in turn has its own dependencies. 就像express一样,有主要的应用程序express ,也有一些express用途的模块,例如express-validation ,这些模块又具有自己的依赖性。

You could add a symlink in node_modules , pointing to wherever your modules are. 您可以在node_modules添加一个符号链接,以指向模块所在的位置。

For example, add a symlink named "my_modules", referencing '../foo/bar/my_modules'. 例如,添加一个名为“ my_modules”的符号链接,引用“ ../foo/bar/my_modules”。 Then, your requires will look like 然后,您的需求将看起来像

var config = require('my_modules/config');
var urls = require('my_modules/urls');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM