如何允许自定义本地 node_module 导航项目文件系统?

How to allow a custom local node_module to navigate project file system?

我有一个链接的本地自定义 node_module 需要使用项目文件目录:/tmp 以便在 运行 时间内将文件转储到那里。如何将自定义 node_module 指向此目录?

我曾尝试使用节点内部路径实用程序访问根目录,但这只保留在 node_modules 文件夹中。

正在尝试使用以下方法访问 tmp 文件夹:

var root = require('find-root')(path.resolve(__dirname));
this.tmpDir = path.resolve(root, 'tmp');

输出:

"/Users/me/Documents/Project/Billing/server/node_modules/processremote/tmp"

但我需要它导航(返回)到:

"/Users/me/Documents/Project/Billing/server/tmp"

find-root returns 最近的目录 package.json,在你的例子中是子模块本身的目录(因为它有一个 package.json 文件和最接近剧本)。

您可以使用 path.join(root, '../..', 'tmp')(而不是当前的 path.resolve(root, 'tmp'))来解决此问题:

var root = require('find-root')(path.resolve(__dirname));
this.tmpDir = path.join(root, '../..', 'tmp');

// `tmpDir` will now be "/Users/me/Documents/Project/Billing/server/tmp"