Process.chdir() 对 require 没有影响
Process.chdir() has no effect on require
给出结构目录结构:
.
├── alpha.js
└── foo
└── beta.js
和文件内容
alpha.js
module.exports = "alpha"
foo/beta.js
var cwd = process.cwd()
process.chdir('../')
var alpha = require('./alpha.js')
console.log(alpha)
process.chdir(cwd)
来自 foo/beta.js
。我希望能够让 require
认为当前工作目录是项目根目录。当以下为 运行.
时,上面的示例不起作用
node ./foo/beta.js
但是,如果我将 foo/beta.js
内的代码切换到以下内容。从系统中读取文件并将其传递给 npm 模块 _eval
.
已更新foo/beta.js
var path = require('path')
var cwd = process.cwd()
process.chdir(path.join(__dirname, '..'))
var fs = require('fs')
var _eval = require('eval')
var alpha = _eval(fs.readFileSync('./alpha.js'))
console.log(alpha)
process.chdir(cwd)
这确实有效,这证明 require
也应该可行。无论您从哪里 运行 如果它总是需要该文件。 node ./foo/beta.js
或 cd foo && node ./beta.js
有什么方法可以在文件中添加或设置 require
使用的目录?
If the module identifier passed to require() is not a native module,
and does not begin with '/', '../', or './', then node starts at the
parent directory of the current module, and adds /node_modules, and
attempts to load the module from that location.
If it is not found there, then it moves to the parent directory, and
so on, until the root of the file system is reached.
由此可以看出,当前目录并没有被用于加载模块。这里的主要内容应该是模块路径是相对于当前模块的位置的。这允许模块以独立的方式加载子模块,而无需知道父模块在目录结构中的位置。
如果传递给它的文件名不是以路径分隔符或 .
.
开头,那么这里有一个变通函数,它从当前目录加载模块文件描述符
var path = require('path');
function requireCWD(fname) {
var fullname = fname;
if (fname && fname.length &&
!path.isAbsolute(fname) &&
fname.charAt(0) !== '.') {
fullname = path.join(process.cwd(), fname);
}
return require(fullname);
}
然后,您给 requireCWD 的任何文件名都不是相对的并且不以“.”开头。将相对于当前工作目录加载。如果你甚至想允许 "."
和 ".."
相对于当前工作目录,那么你可以从函数中删除 '.'
的测试。
给出结构目录结构:
.
├── alpha.js
└── foo
└── beta.js
和文件内容
alpha.js
module.exports = "alpha"
foo/beta.js
var cwd = process.cwd()
process.chdir('../')
var alpha = require('./alpha.js')
console.log(alpha)
process.chdir(cwd)
来自 foo/beta.js
。我希望能够让 require
认为当前工作目录是项目根目录。当以下为 运行.
node ./foo/beta.js
但是,如果我将 foo/beta.js
内的代码切换到以下内容。从系统中读取文件并将其传递给 npm 模块 _eval
.
已更新foo/beta.js
var path = require('path')
var cwd = process.cwd()
process.chdir(path.join(__dirname, '..'))
var fs = require('fs')
var _eval = require('eval')
var alpha = _eval(fs.readFileSync('./alpha.js'))
console.log(alpha)
process.chdir(cwd)
这确实有效,这证明 require
也应该可行。无论您从哪里 运行 如果它总是需要该文件。 node ./foo/beta.js
或 cd foo && node ./beta.js
有什么方法可以在文件中添加或设置 require
使用的目录?
If the module identifier passed to require() is not a native module, and does not begin with '/', '../', or './', then node starts at the parent directory of the current module, and adds /node_modules, and attempts to load the module from that location.
If it is not found there, then it moves to the parent directory, and so on, until the root of the file system is reached.
由此可以看出,当前目录并没有被用于加载模块。这里的主要内容应该是模块路径是相对于当前模块的位置的。这允许模块以独立的方式加载子模块,而无需知道父模块在目录结构中的位置。
如果传递给它的文件名不是以路径分隔符或 .
.
var path = require('path');
function requireCWD(fname) {
var fullname = fname;
if (fname && fname.length &&
!path.isAbsolute(fname) &&
fname.charAt(0) !== '.') {
fullname = path.join(process.cwd(), fname);
}
return require(fullname);
}
然后,您给 requireCWD 的任何文件名都不是相对的并且不以“.”开头。将相对于当前工作目录加载。如果你甚至想允许 "."
和 ".."
相对于当前工作目录,那么你可以从函数中删除 '.'
的测试。