-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
Odd behaviour: createRequireFromPath('.')
#27401
Comments
This comment has been minimized.
This comment has been minimized.
This is actually working as intended after looking deeper into this. To path provided is the relative path to the current file or an absolute path. AFAIK this should not honor changing the working directory as it's independent. |
I think there is a slightly more subtle indirection/lol.js const {createRequireFromPath} = require('module');
const req = createRequireFromPath('.');
console.log(process.cwd());
console.log(require.resolve('./package.json'));
console.log(req.resolve('./package.json')); $ node indirection/lol.js
/Users/mylesborins/code/makeRequireFromPath
/Users/mylesborins/code/makeRequireFromPath/indirection/package.json
/Users/mylesborins/code/makeRequireFromPath/package.json
$ cd indirection
$ node lol.js
/Users/mylesborins/code/makeRequireFromPath/indirection
/Users/mylesborins/code/makeRequireFromPath/indirection/package.json
/Users/mylesborins/code/makeRequireFromPath/indirection/package.json edit: this is because |
I don't think there should be any "callee" magic here. my intention when I originally proposed this feature was something along the lines of |
@devsnek but as the feature is documented it doesn't really work for two reasons from docs const { createRequireFromPath } = require('module');
const requireUtil = createRequireFromPath('../src/utils');
// Require `../src/utils/some-tool`
requireUtil('./some-tool'); issue 1) if you create a require from a relative path it is going to be different depending on cwd(), likely unexpected issue 2) directories don't actually work like that
Seems like we need to revisit some of the behavior here imho |
@BridgeAR I just wanted to clarify the behaviour:
There is clearly a cached aspect which:
So I was really hoping we can clarify the behaviour of Hope this was helpful 😄
|
The cached behaviour in your opinion, does that qualify as a bug? |
Closing as I believe this has been fixed |
Calling
createRequireFromPath('.')
does not honourprocess.cwd()
when called a second time after an effective call toprocess.chdir(…)
.Best explained by this: https://github.com/SMotaal/esm/blob/master/tests/create-require.mjs#L8
The text was updated successfully, but these errors were encountered: