javascript - Process.chdir() has no effect on require - Stack Overflow

Given the structure directory structure:.├── alpha.js└── foo└── beta.jsAnd file contentsalpha.jsmodul

Given the structure directory structure:

.
├── alpha.js
└── foo
    └── beta.js

And file contents

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)

From within foo/beta.js. I'd like to be able to trick require into thinking that the current working directory is the project root. The example above does not work when the following is run.

node ./foo/beta.js

However if I switch over the code to within foo/beta.js to the following. Reading the file from the system and passing it to the npm module _eval.

updated 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)

This does work, which proves it should be possible with require as well. No matter where you run if from it will always require the file. node ./foo/beta.js or cd foo && node ./beta.js

Is there any way that I can prepend or set the directory that require uses from within the file?

Given the structure directory structure:

.
├── alpha.js
└── foo
    └── beta.js

And file contents

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)

From within foo/beta.js. I'd like to be able to trick require into thinking that the current working directory is the project root. The example above does not work when the following is run.

node ./foo/beta.js

However if I switch over the code to within foo/beta.js to the following. Reading the file from the system and passing it to the npm module _eval.

updated 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)

This does work, which proves it should be possible with require as well. No matter where you run if from it will always require the file. node ./foo/beta.js or cd foo && node ./beta.js

Is there any way that I can prepend or set the directory that require uses from within the file?

Share Improve this question edited Aug 12, 2015 at 3:16 ThomasReggi asked Aug 12, 2015 at 3:10 ThomasReggiThomasReggi 59.8k97 gold badges260 silver badges459 bronze badges 13
  • Why not use var alpha = require('./../alpha.js')? – Dan D. Commented Aug 12, 2015 at 3:35
  • @DanD. I'm have to execute the code from the parent dir and be able to make the module call with ./alpha.js that is the technical challenge I need to meet. – ThomasReggi Commented Aug 12, 2015 at 3:41
  • @DanD. The correct string reference from within foo/beta.js would be var alpha = require('../alpha.js'). – ThomasReggi Commented Aug 12, 2015 at 3:43
  • Can you expand on why you want to do this? I didn't really follow your ment, and this feels like an XY problem. – Aaron Dufour Commented Aug 12, 2015 at 3:54
  • @AaronDufour I wanted to allow a argument to a global module I created called evalmd that would allow markdown strings with require statements to be executed from a different working dir. So if you run the cmd and you have your md files within docs it would work. As an alternative to my question I just went ahead and prepended the require string with the dir. – ThomasReggi Commented Aug 12, 2015 at 4:30
 |  Show 8 more ments

1 Answer 1

Reset to default 6

From the node.js doc for 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.

From this, you can see that the current directory is not used in loading modules. The main takeaway here should be that modules paths are relative to the location of the current module. This allows modules to load sub-modules in a self-contained fashion without having to know anything about where the parent is placed in the directory structure.

Here's a work-around function that loads a module file descriptor from the current directory if the filename passed to it does not start with a path separator or a ..

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);
}

Then, any filename you give to requireCWD that is not relative and does not start with a "." will be loaded relative to the current working directory. If you want to allow even "." and ".." to be relative to the current working directory, then you can remove that test for '.' from the function.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744938462a4602170.html

相关推荐

  • javascript - Process.chdir() has no effect on require - Stack Overflow

    Given the structure directory structure:.├── alpha.js└── foo└── beta.jsAnd file contentsalpha.jsmodul

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信