Repository navigation
Odd behaviour: createRequireFromPath('.') #27401
Description
Activity
- addedfeature requestIssues requesting new Node.js features.Issues requesting new Node.js features.moduleIssues and PRs related to the module subsystem.Issues and PRs related to the module subsystem.
on Apr 24, 2019 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
bugbehavior here... in thatcreateRequireFromPath('.')seems to always useprocess.cwd()as the base 😅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.jsonedit: this is because
createRequireFromPath(path)callsnew Module(path)which is turn callspath.dirname(path)which when called is relative to the CWD, not the filename of the calleeI don't think there should be any "callee" magic here. my intention when I originally proposed this feature was something along the lines of
makeRequire(import.meta.url).@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
- open issue: module: createRequireFromPath documentation is incorrect #23710
- open fix: module: allow passing a directory to createRequireFromPath #23818
Seems like we need to revisit some of the behavior here imho
@BridgeAR I just wanted to clarify the behaviour:
-
running
node … esm/tests/create-require.mjsfrom'‹scope›'/- it calls
process.chdir(`‹scope›/esm/`)first - it calls
createRequireFromPath('.').resolve('./package.json') - this returns
'‹scope›/esm/package.json' - it then calls
process.chdir(`‹scope›/esm/test/`)second - it calls
createRequireFromPath('.').resolve('./package.json') - this returns
'‹scope›/esm/package.json'also
- it calls
-
running
node … esm/tests/create-require.mjs -t2from'‹scope›'/- it calls
process.chdir(`‹scope›/esm/test/`)first - it calls
createRequireFromPath('.').resolve('./package.json') - this returns
'‹scope›/esm/test/package.json' - it then calls
process.chdir(`‹scope›/esm/`)second - it calls
createRequireFromPath('.').resolve('./package.json') - this returns
'‹scope›/esm/test/package.json'also
- it calls
There is clearly a cached aspect which:
- does not get initialized until first call to
createRequireFromPath - picks up
process.cwd()at that time (at least with'.'as the arg). - since 2 happens, then
process.chdir(…)not affecting next call to a newcreateRequireFromPath- irrespective of why the user wanted 2 of them — is not a behaviour that is clear from a user perspective. - if 2 is not intended, then
'.'not resolving to the current module — irrespective of this being CJS/ESM — is not a behaviour that is helpful from a user perspective.
So I was really hoping we can clarify the behaviour of
'.'here.Hope this was helpful 😄
Side note: The few times, I wanted to chdir in node, I always need to remind myself that
process.chdir(…)notprocess.cwd(…)— ie I maybe do this once every 1-2 years and usually to test odd behaviour of resolved paths seemingly tied toprocess.cwd().So, I would be favourable of module related utilities being more aware of module locations than
process.cwdis my intuitive feel for moving forward here.-
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
Details