Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't always work with relative paths #1

Open
kflorence opened this issue Aug 6, 2014 · 6 comments
Open

Doesn't always work with relative paths #1

kflorence opened this issue Aug 6, 2014 · 6 comments

Comments

@kflorence
Copy link

If I'm in /home/kflorence/dev/project/foo/bar and I require: optional("../baz/qux") it will fail because it's trying to look up /home/kflorence/dev/project/baz/qux instead of /home/kflorence/dev/project/foo/baz/qux

@tony-o
Copy link
Owner

tony-o commented Aug 14, 2014

Is /home/kflorence/dev/project/foo/bar a file or a dir? I'm assuming it's a directory from the question.

In what directory is the main script located? Is it also in /home/kflorence/dev/project/foo/bar or is it in /home/kflorence/dev/project/foo ?

@kflorence
Copy link
Author

Sorry for the ambiguous example, let me try to state it more clearly: optional() uses the process.cwd() directory as it's base, meaning the path you give it must be relative to that, whereas require() allows you to include relative to the file that contains the require() call -- so optional() isn't just a drop-in replacement for require() calls in some cases.

So in the example above, the process.cwd() directory would be /home/kflorence/dev/project/foo which is why using optional("../baz/qux") from a file within the /home/kflorence/dev/project/foo/bar folder would fail to find /home/kflorence/dev/project/foo/baz/qux whereas require() would succeed.

While I don't think this is a bug, it also wasn't intuitive to me at first as I expected it to work in the same way as require().

@tony-o
Copy link
Owner

tony-o commented Dec 4, 2014

@kflorence i believe i resolved this one too, can you confirm?

@kflorence
Copy link
Author

@tony-o Relative includes are only fixed for modules like "./foo/bar" -- the current solution doesn't work correctly in cases like "../../foo/bar"

@panstav
Copy link

panstav commented Jan 31, 2016

Tried to fix this with:

try{
  if(required[0] in {".":1}){
    let pathToFile = module.parent.filename;
    let dirOfCallee = pathToFile.substr(0, pathToFile.lastIndexOf('/'))
    required = path.resolve(dirOfCallee, required);
  }
  return require(required);
}

But apparently node modules are cached, which is good, but module.parent is also cached, so basically it will work the first time you call it, but the second time, if you call optional(./file.js) from a different file on a different path - than module.parent.filename will stay the same as it was for the first call 😕

Hope this helps.

Edit: require-new might be useful here.

@fengb
Copy link

fengb commented Apr 11, 2017

I wrote a module that should handle relative paths: https://github.com/fengb/require-optional

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants