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

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


当前回答

在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_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"

恕我直言,最简单的方法是将自己的函数定义为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,所以我最好展示我的测试项目的结构的图片。

大局

这看起来“真的很糟糕”,但要给它时间。事实上,它真的很好。显式的require()提供了完全的透明性和易于理解,就像项目生命周期中的一股新鲜空气。

可以这样想:你正在阅读一个例子,尝试Node.js,你认为它“在我看来真的很糟糕”。你在质疑Node.js社区的领导者,他们比任何人都花了更多的时间来编写和维护Node.js应用程序。作者犯这样一个新手错误的可能性有多大?(我同意,从我的Ruby和Python背景来看,乍一看这像是一场灾难。)

围绕Node.js有很多炒作和反炒作。但是当尘埃落定后,我们将承认显式模块和“本地优先”包是采用的主要驱动力。

一般情况

当然,首先搜索当前目录中的node_modules,然后是父目录、祖父目录、曾祖父目录等等。所以您已经安装的包已经以这种方式工作了。通常你可以从项目中的任何地方要求(“express”),它工作得很好。

如果您发现自己正在从项目的根目录加载公共文件(可能是因为它们是公共实用程序函数),那么这就是一个很大的线索,说明是时候创建包了。包非常简单:将文件移动到node_modules/,并放入package.json 在那里。瞧!整个项目都可以访问该名称空间中的所有内容。包是将代码放入全局名称空间的正确方法。

其他解决方法

我个人不使用这些技巧,但它们确实回答了你的问题,当然你比我更了解自己的情况。

您可以将$NODE_PATH设置为项目根目录。当您需要()时,将搜索该目录。

接下来,您可以折衷一下,从所有示例中要求一个通用的本地文件。该通用文件只是重新导出祖父目录中的真实文件。

Examples /downloads/app.js(以及其他类似的文件)

var express = require('./express')

/下载/ express.js例子

module.exports = require('../../')

现在当你重新定位这些文件时,最坏的情况是修复一个垫片模块。

我创建了一个名为rekiure的节点模块

它允许您在不使用相对路径的情况下进行要求

https://npmjs.org/package/rekuire

它非常容易使用

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

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

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

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

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