Skip to content

[windows only] Require fails to load native addons with path longer than 260 characters #2964

Closed
@justinmchase

Description

@justinmchase

I'm having trouble where somewhere down the line a dependency of mine is attempting to load a native addon. Due to deep nesting the process of requiring this addon fails with the error "The filename or extension is too long".

However, I found that I am able to work around this by modifying the global Module to always prepend "\\\\?\\" to paths when attempting to load native addons.

This works because paths with that funky prefix kicks win32 path handling into extended-length path mode, according to MSDN:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#short_vs._long_names

To specify an extended-length path, use the "\?" prefix. For example, "\?\D:\very long path".

My workaround looks like this basically:

var os = require('os');
if (os.platform() == 'win32') {
    var prefix = "\\\\?\\";
    var Module = require('module');
    Module._extensions['.node'] = function (module, modulePath) {
        if (modulePath.indexOf(prefix) !== 0) {
            modulePath = prefix + modulePath;
        }
        return process.dlopen(module, modulePath);
    }
}

It appears to allow me to successfully load any native addon now, regardless of path length. I'm thinking of submitting a PR which would patch lib/module.js#L355 to essentially have this same functionality.

What do you think? Are there any gotcha's or can anyone think of a reason why this wouldn't work in the general case?

Metadata

Metadata

Assignees

No one assigned

    Labels

    moduleIssues and PRs related to the module subsystem.windowsIssues and PRs related to the Windows platform.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions