Skip to content

Require .\ does not work on Windows #6049

@wmcmurray

Description

@wmcmurray

Windows uses backslashes inside paths and it seems to be supported by the require function. BUT ! When you use a backslash inside the "current path syntax" (usually ./) it does not work. I found this using the require.resolve method directly.

Test case :

file /test/index.js :

var test, tests = [
  './stuff/please-require-me',
  './stuff\\please-require-me',
  '.\\stuff\\please-require-me'
  ];

for(i in tests){
  try {
    test = require(tests[i]);
  } catch(e){
    test = e.message;
  }
  console.log(tests[i]+' = ', test);
}

file /test/stuff/please-require-me.js :

module.exports = 'OK';

My test output :

$ node index.js
./stuff/please-require-me =  OK
./stuff\please-require-me =  OK
.\stuff\please-require-me =  Cannot find module '.\stuff\please-require-me'

As you can see in the second test, the backslash works, but in the third test it fails because it is in the "current path syntax".

Additional notes

I also made a quick check to see if the .\ is supposed to work, and it does :

C:\Users\William Mcmurray>cd ./Desktop
C:\Users\William Mcmurray\Desktop>cd ../
C:\Users\William Mcmurray>cd .\Desktop
C:\Users\William Mcmurray\Desktop>

Specs

  • Version: v5.10.0
  • Platform: Windows 10, 64-bit

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
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions