我想要求我的文件总是通过我的项目的根,而不是相对于当前模块。

例如,如果查看https://github.com/visionmedia/express/blob/2820f2227de0229c5d7f28009aa432f9f3a7b5f9/examples/downloads/app.js第6行,您将看到

express = require('../../')

在我看来,这真的很糟糕。假设我想让我所有的例子都只靠近根结点一层。这是不可能的,因为我必须更新超过30个例子,并且在每个例子中更新很多次。:

express = require('../')

我的解决方案是有一个基于根的特殊情况:如果字符串以$开头,那么它相对于项目的根文件夹。

任何帮助都是感激的,谢谢

更新2

现在我使用require.js,它允许你以一种方式编写,在客户端和服务器上都可以工作。Require.js还允许你创建自定义路径。

更新3

现在我转移到webpack + gulp,我使用enhanced-require来处理服务器端模块。看这里的基本原理:http://hackhat.com/p/110/module-loader-webpack-vs-requirejs-vs-browserify/


当前回答

尝试使用asapp:

NPM安装——保存为app

https://www.npmjs.com/package/asapp

var { controller, helper, middleware, route, schema, model, APP, ROOT } = require('asapp')

Controller ('home')改为require('../../controllers/home)

其他回答

我也遇到了同样的问题,所以我编写了一个名为include的包。

包含通过定位包来确定项目根文件夹的句柄。Json文件,然后传递路径参数,你给它的本地require()没有所有的相对路径混乱。我认为这不是require()的替代品,而是需要处理非打包/非第三方文件或库的工具。类似的

var async = require('async'),
    foo   = include('lib/path/to/foo')

我希望这对你有用。

另一个答案是:

想象一下这个文件夹结构:

node_modules lodash src 子目录 foo.js bar.js main.js 测试 . js

然后在test.js中,你需要这样的文件:

const foo = require("../src/subdir/foo");
const bar = require("../src/subdir/bar");
const main = require("../src/main");
const _ = require("lodash");

在main.js中:

const foo = require("./subdir/foo");
const bar = require("./subdir/bar");
const _ = require("lodash");

现在你可以使用。babelrc文件中的babel和babel插件模块解析器来配置两个根文件夹:

{
    "plugins": [
        ["module-resolver", {
            "root": ["./src", "./src/subdir"]
        }]
    ]
}

现在你可以在测试和src中以同样的方式要求文件:

const foo = require("foo");
const bar = require("bar");
const main = require("main");
const _ = require("lodash");

如果你想使用es6模块语法:

{
    "plugins": [
        ["module-resolver", {
            "root": ["./src", "./src/subdir"]
        }],
        "transform-es2015-modules-commonjs"
    ]
}

然后像这样在测试和SRC中导入文件:

import foo from "foo"
import bar from "bar"
import _ from "lodash"

这是我六个多月来的实际做法。我在项目中使用一个名为node_modules的文件夹作为我的根文件夹,这样它将始终从我调用绝对require的任何地方查找该文件夹:

node_modules myProject index.js我可以require("myProject/someFolder/hey.js")而不是require("./someFolder/hey.js") 包含hey.js的someFolder

当你被嵌套到文件夹中时,这更有用,如果以绝对方式设置,更改文件位置的工作要少得多。我在整个应用程序中只使用了2个相对要求。

恕我直言,最简单的方法是将自己的函数定义为GLOBAL对象的一部分。 在项目的根目录下创建projRequire.js,包含以下内容:

var projectDir = __dirname;

module.exports = GLOBAL.projRequire = function(module) {
  return require(projectDir + module);
}

在你的主文件中,在需要任何特定于项目的模块之前:

// init projRequire
require('./projRequire');

之后,以下工作对我来说:

// main file
projRequire('/lib/lol');

// index.js at projectDir/lib/lol/index.js
console.log('Ok');

@Totty,我想出了另一个解决方案,可以解决你在评论中描述的情况。描述将是tl;dr,所以我最好展示我的测试项目的结构的图片。

我编写了这个小包,它允许您通过项目根的相对路径来要求包,而不引入任何全局变量或覆盖节点默认值

https://github.com/Gaafar/pkg-require

它是这样工作的

// create an instance that will find the nearest parent dir containing package.json from your __dirname
const pkgRequire = require('pkg-require')(__dirname);

// require a file relative to the your package.json directory 
const foo = pkgRequire('foo/foo')

// get the absolute path for a file
const absolutePathToFoo = pkgRequire.resolve('foo/foo')

// get the absolute path to your root directory
const packageRootPath = pkgRequire.root()