Repository navigation
Inconsistent multiple extension handling between Module._findPath and Module.load #4778
Description
Activity
- addedmoduleIssues and PRs related to the module subsystem.Issues and PRs related to the module subsystem.
on Jan 20, 2016 What version of Node are you using? The following script (and the variations of it that I tried) work for me on v5.4.1.
'use strict'; const assert = require('assert'); const fs = require('fs'); const file = './test.foo.bar'; fs.writeFileSync(file, 'NOT_JS', 'utf8'); require.extensions['.foo.bar'] = (module, path) => { console.log('required .foo.bar', path); }; assert.throws(() => { require(file); }); require.extensions['.bar'] = (module, path) => { console.log('required .bar', path); }; delete require.extensions['.foo.bar'] assert.doesNotThrow(() => { require(file); });
Sorry, to be clear, the
_findPath/tryExtensionscode only happens when the file is required with no extension. Try replacing your lastrequire(file)withrequire('./test').Ok, here is the reproduction:
'use strict'; const fs = require('fs'); fs.writeFileSync('./test.foo.bar', 'NOT_JS', 'utf8'); require.extensions['.foo.bar'] = (module, path) => {}; // delete require.extensions['.foo.bar']; require.extensions['.bar'] = (module, path) => { console.log(`loaded ${path}`); }; require('./test');
Toggle the commented out line to see the issue. This seems, to me, like something that should be fixed, but module system. What do others think?
- added a commit that references this issue
on Mar 16, 2016 - added 2 commits that reference this issue
on Mar 30, 2016 I know it's policy not to fix bugs in module that might help compile-to-js languages, but I figure someone else might come across the same problem and this report could be useful to them.
@sgentle Fortunately, this is no longer true. And while we are unlikely to add new features or change the intended behavior of existing features, fixing bugs in
moduleis now totally on the table. If you feel up to fixing, by all means, please submit a pull request.Reacted by Aljeksej Jaroševič- addedconfirmed-bugIssues and PRs for confirmed bugs.Issues and PRs for confirmed bugs.
on Dec 31, 2017 - addedhelp wantedIssues that need assistance from volunteers or PRs that need help to proceed.Issues that need assistance from volunteers or PRs that need help to proceed.
on Apr 20, 2018 - added a commit that references this issue
on Oct 10, 2018 - added a commit that references this issue
on Oct 17, 2018 - added a commit that references this issue
on Jul 27, 2026
Let's start by making a file with multiple extensions:
test.foo.barSo we already know that
require.extensionsdoesn't work with multiple extensions:And that it will use the last component of the extension instead:
Which is all perfectly obvious and by design. But did you know that the path searching uses the whole extension? Which means:
However:
In summary: to hook the requiring of a file with multiple extensions without specifying it, you must:
require.extensionsfor the last component of the extension to your hook functionrequire.extensionsfor the entire extension to any value.This is due to differences in how
Module._findPathandModule.loaddeal with extensions: the former adds each extension inrequire.extensionsto the basename and tests it for existence (viatryExtensions). The latter works the other way around; it removes everything up to the last part of the extension (viapath.extname) and then checks if it is present inrequire.extensions.I know it's policy not to fix bugs in
modulethat might help compile-to-js languages, but I figure someone else might come across the same problem and this report could be useful to them.