我想要求我的文件总是通过我的项目的根,而不是相对于当前模块。
例如,如果查看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/
恕我直言,最简单的方法是将自己的函数定义为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,所以我最好展示我的测试项目的结构的图片。
我认为你不需要用你描述的方式来解决这个问题。如果您想在大量文件中更改相同的字符串,请使用sed。在你的例子中,
find . -name "*.js" -exec sed -i 's/\.\.\/\.\.\//\.\.\//g' {} +
/../变成了../
或者,您可以要求配置文件存储包含库路径的变量。如果您将以下文件存储为config.js在示例目录中
var config = {};
config.path = '../../';
在你的例子文件中
myConfiguration = require('./config');
express = require(config.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"
我实现这一点的方法是创建“本地链接模块”。
的文件夹结构为例
db ¬
models ¬
index.js
migrations
seed
config.json
routes ¬
index.js
user ¬
index.js
如果从。/routes/user/index.js我想访问/db/models/index.js我会写
require('../../db/models/index.js')
为了使/db/models/index.js可以从任何地方访问,我在db文件夹中创建了一个名为_module_的文件夹,其中包含一个包。Json和一个main.js文件。
# package.json
{
"name": "db", <-- change this to what you want your require name to be
"version": "1.0.0",
"description": "",
"author": "",
"repository": {},
"main": "main.js"
}
// main.js
module.exports = require('../../db/models/index');
main.js中的路径必须是相对的,就像文件在node_modules中一样
node_modules ¬
db ¬
main.js
然后你可以运行npm install ./db/_module_,这将把。/db/_module_中的文件复制到。/node_modules/db中,在应用程序包的依赖项下创建一个条目。json之类的
"db": "file:db/_module_"
您现在可以在任何地方使用此包
const db = require('db');
当你运行npm install时,它会自动安装你的其他模块,工作跨平台(没有符号链接),并且不需要第三方包。
只是想继续Paolo Moretti和Browserify的精彩回答。如果你正在使用一个编译器(例如babel, typescript),并且你有单独的文件夹存放源代码和编译过的代码,比如src/和dist/,你可以使用不同的解决方案
node_modules
目录结构如下:
app
node_modules
... // normal npm dependencies for app
src
node_modules
app
... // source code
dist
node_modules
app
... // transpiled code
然后你可以让Babel等编译SRC目录到dist目录。
符号链接
使用符号链接,我们可以摆脱一些嵌套级别:
app
node_modules
... // normal npm dependencies for app
src
node_modules
app // symlinks to '..'
... // source code
dist
node_modules
app // symlinks to '..'
... // transpiled code
关于babel——copy-files的警告:babel的——copy-files标志不能很好地处理符号链接。它可能会一直导航到…符号链接和隐性看到无尽的文件。一种变通方法是使用以下目录结构:
app
node_modules
app // symlink to '../src'
... // normal npm dependencies for app
src
... // source code
dist
node_modules
app // symlinks to '..'
... // transpiled code
通过这种方式,src下的代码仍然会有app解析到src,而babel将不再看到符号链接。