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

例如,如果查看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/


当前回答

虽然这些答案工作,但他们没有解决npm测试的问题

例如,如果我在server.js中创建一个全局变量,它将不会为我的测试套件执行设置。

设置全局apot变量,避免../../..在npm start和npm test中都可以使用,参见:

Mocha使用额外的选项或参数进行测试

请注意,这是新的官方摩卡解决方案。

其他回答

这里有关于这个问题的很好的讨论。

我遇到了同样的架构问题:想要给我的应用程序更多的组织和内部名称空间,但没有:

将应用程序模块与外部依赖项混合,或者为特定于应用程序的代码使用私有NPM回购 使用相对要求,使得重构和理解更加困难 使用符号链接或更改节点路径,这可能会模糊源代码位置,并且不能很好地进行源代码控制

最后,我决定使用文件命名约定而不是目录来组织我的代码。结构应该是这样的:

npm-shrinkwrap.json package.json node_modules ... src app.js app.config.js app.models.bar.js app.models.foo.js app.web.js app.web.routes.js ...

然后在代码中:

var app_config = require('./app.config');
var app_models_foo = require('./app.models.foo');

或者只是

var config = require('./app.config');
var foo = require('./app.models.foo');

和往常一样,外部依赖项可以从node_modules中获得:

var express = require('express');

通过这种方式,所有应用程序代码都按层次结构组织成模块,相对于应用程序根,所有其他代码都可以使用。

当然,主要的缺点是在文件浏览器中,您不能展开/折叠树,就好像它实际上被组织成目录一样。但我喜欢它非常明确所有代码的来源,而且它没有使用任何“魔法”。

感谢所有发帖的人,这篇帖子给我指明了正确的方向。由于提出的解决方案没有一个像我想要的那样简洁,我想在这里分享我的解决方案。

我一直在寻找一种解决方案,不“需要”任何额外的东西,没有模块,没有包。Json mod,没有环境变量,没有外部依赖,最小的CPU周期…

...所以我想到了这个:

全球。Dir = process.cwd();

我在应用程序加载时这样做一次。显然,您可以通过添加子目录和/或添加'/..,它将根据需要将您发送到目录树的上下。

在所有文件中,所有my include都是这样的格式:

找生气。(你,兄弟,妙/妙以致)

在Browserify手册中有一个非常有趣的章节:

avoiding ../../../../../../.. Not everything in an application properly belongs on the public npm and the overhead of setting up a private npm or git repo is still rather large in many cases. Here are some approaches for avoiding the ../../../../../../../ relative paths problem. node_modules People sometimes object to putting application-specific modules into node_modules because it is not obvious how to check in your internal modules without also checking in third-party modules from npm. The answer is quite simple! If you have a .gitignore file that ignores node_modules: node_modules You can just add an exception with ! for each of your internal application modules: node_modules/* !node_modules/foo !node_modules/bar Please note that you can't unignore a subdirectory, if the parent is already ignored. So instead of ignoring node_modules, you have to ignore every directory inside node_modules with the node_modules/* trick, and then you can add your exceptions. Now anywhere in your application you will be able to require('foo') or require('bar') without having a very large and fragile relative path. If you have a lot of modules and want to keep them more separate from the third-party modules installed by npm, you can just put them all under a directory in node_modules such as node_modules/app: node_modules/app/foo node_modules/app/bar Now you will be able to require('app/foo') or require('app/bar') from anywhere in your application. In your .gitignore, just add an exception for node_modules/app: node_modules/* !node_modules/app If your application had transforms configured in package.json, you'll need to create a separate package.json with its own transform field in your node_modules/foo or node_modules/app/foo component directory because transforms don't apply across module boundaries. This will make your modules more robust against configuration changes in your application and it will be easier to independently reuse the packages outside of your application. symlink Another handy trick if you are working on an application where you can make symlinks and don't need to support windows is to symlink a lib/ or app/ folder into node_modules. From the project root, do: ln -s ../lib node_modules/app and now from anywhere in your project you'll be able to require files in lib/ by doing require('app/foo.js') to get lib/foo.js. custom paths You might see some places talk about using the $NODE_PATH environment variable or opts.paths to add directories for node and browserify to look in to find modules. Unlike most other platforms, using a shell-style array of path directories with $NODE_PATH is not as favorable in node compared to making effective use of the node_modules directory. This is because your application is more tightly coupled to a runtime environment configuration so there are more moving parts and your application will only work when your environment is setup correctly. node and browserify both support but discourage the use of $NODE_PATH.

我喜欢做的是利用node从node_module目录加载的方式。

如果有人试图加载“thing”模块,他会做如下的事情

require('thing');

Node将在'node_module'目录中查找'thing'目录。

由于node_module通常位于项目的根,所以我们可以利用这种一致性。(如果node_module不在根节点上,那么您就会遇到其他令人头痛的问题。)

如果我们进入目录,然后从目录中返回,我们可以获得到节点项目根的一致路径。

require('thing/../../');

如果我们想访问/happy目录,我们会这样做。

require('thing/../../happy');

虽然这有点粗糙,但是我觉得如果node_modules加载的功能发生了变化,将会有更大的问题需要处理。这种行为应该保持一致。

为了使事情更清楚,我这样做,因为模块的名称并不重要。

require('root/../../happy');

我最近在angular2中使用了它。我想从根目录加载服务。

import {MyService} from 'root/../../app/services/http/my.service';

手动符号链接(和Windows连接)

examples目录中不能包含一个node_modules,该node_modules带有指向项目根目录的符号链接吗?/因此允许示例使用require('project'),尽管这并没有删除映射,但它允许源代码使用require('project')而不是require('../../')。

我已经对此进行了测试,它确实适用于v0.6.18。

项目目录清单:

$ ls -lR project
project:
drwxr-xr-x 3 user user 4096 2012-06-02 03:51 examples
-rw-r--r-- 1 user user   49 2012-06-02 03:51 index.js

project/examples:
drwxr-xr-x 2 user user 4096 2012-06-02 03:50 node_modules
-rw-r--r-- 1 user user   20 2012-06-02 03:51 test.js

project/examples/node_modules:
lrwxrwxrwx 1 user user 6 2012-06-02 03:50 project -> ../../

index.js的内容将一个值赋给exports对象的一个属性,并调用console.log,并用一条消息声明它是必需的。test.js的内容是require('project')。

自动化的符号链接

手动创建符号链接的问题是,每次你npm ci时,你都会丢失符号链接。如果您使符号链接进程成为依赖项,那么就没有问题。

模块basetag是一个postinstall脚本,它在每次运行npm install或npm ci时创建一个名为$的符号链接(或Windows连接):

npm install --save basetag
node_modules/$ -> ..

这样,您就不需要对代码或require系统进行任何特殊修改。$成为您可以从中进行请求的根。

var foo = require('$/lib/foo.js');

如果你不喜欢使用$,而更喜欢#或其他(除了@,这是npm的一个特殊字符),你可以fork它并进行更改。

注意:虽然Windows符号链接(到文件)需要管理员权限,但Windows连接(到目录)不需要管理员权限。这是一种安全、可靠、跨平台的解决方案。