您如何强制节点模块的绝对路径? [英] How do you force the absolute path for node modules?

查看:92
本文介绍了您如何强制节点模块的绝对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用CommonJS模块样式的Titanium项目.但是,代码使用绝对路径,因此在构建绝对路径时,会将绝对路径沙箱化到应用程序目录.

I have a Titanium project which uses the CommonJS module style. However the code uses absolute paths so that when it builds the absolute path is sandboxed to the application directory.

var foo = require("/lib/module");

我想在命令行上运行一些测试,并让茉莉花节点工作.但是,当测试执行模块时,该模块将在其require语句中具有上述绝对路径.

I want to run some tests on the command line and have jasmine-node working. However when a test executes a module the module will have the above absolute paths in their require statements.

是否有一种隔离(也许是chroot)节点来解析到特定目录的绝对需求路径的方法?如果可以,怎么办?

Is there a way to isolate (maybe chroot) node to resolve absolute require paths to a specific directory? If so how?

-- RepositoryRoot/
   |- app/
   |  \- Resources/
   |     |- app.js  # Has require("/lib/module1.js")
   |     \- lib/
   |        |- module1.js # Has require("/lib/module2.js")
   |        \- module2.js
   \- tests/
      \- module1.spec.js # Has require("../app/Resources/lib/module1")
                         # Or require("/lib/module1")

推荐答案

找到解决方案后,我学到了:上面确切问题的简短答案是您无法做到.节点将绝对路径读取为绝对路径.简而言之,答案是将我的路径从绝对路径更改为 pseudo-absolute (相对)路径.这是来自此博客文章的引用可以说明一些问题:

After finding a solution here is what I learned: The short answer to the exact above question is you can't do that. Node reads absolute paths as absolute paths. So the answer in short was to change my paths from absolute to pseudo-absolute (relative) paths. Here is a quote from this blog post that sheds some light:

CommonJS require()的Titanium实现是 buggy 和 不正确支持相对路径.这代表了一个重大 尝试将茉莉花节点测试运行程序集成到项目中时出现问题 甚至具有最小复杂的目录树.

the Titanium implementation of CommonJS require() is buggy and doesn’t correctly support relative paths. This represents a major problem when trying to integrate jasmine-node test runners in projects with even minimally complex directory trees.

该问题的可能解决方案是不使用相对路径 钛中的require()(但您可以在茉莉花中自由使用它们 规格通过节点运行).而不是相对路径,我们需要使用完整 Resources作为根目录的路径.

A possible solution to the issue is to not use relative paths in require() in Titanium (but you are free to use them in your jasmine specs run through node). Instead of relative paths we need to use full paths with Resources as the root directory.

这是通过在运行任何节点命令之前设置NODE_PATH环境变量来完成的.这样,诸如和钛来解析.

This is accomplished by setting the NODE_PATH environment variable prior to running any node commands. That way a path such as `require("module/path") is resolved by node and titanium.

有一些警告.一些模块强制需要绝对路径.在这种情况下,只要没有循环依赖关系,就需要 proxyquire 来模拟出绝对路径.工作.同样,由于节点不具有Titanium API,您还必须包括 mockti 包,以模拟出Titanium API.在您的spec_helper.js中使用它:

There are a few caveats. Some module force the need for absolute paths. In this case one needs proxyquire to mock out the absolute paths as long as there are not any circular dependancies this will work. Also since node does not have Titanium API's you also have to include the mockti package to mock out the Titanium API. Use this in your spec_helper.js:

global.Ti = require("mockti");

proxyquire = require("proxyquire");
var myModule = proxyquire("relative/path/to/MyModule", {
    "/absolute/path/to/some/module": require("absolute/path/to/some/module")
});

这篇关于您如何强制节点模块的绝对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆