Skip to content
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

Require .\ does not work on Windows #6049

Closed
wmcmurray opened this issue Apr 5, 2016 · 12 comments
Closed

Require .\ does not work on Windows #6049

wmcmurray opened this issue Apr 5, 2016 · 12 comments
Labels
module Issues and PRs related to the module subsystem. windows Issues and PRs related to the Windows platform.

Comments

@wmcmurray
Copy link

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
@mscdex mscdex added module Issues and PRs related to the module subsystem. windows Issues and PRs related to the Windows platform. labels Apr 5, 2016
@Fishrock123
Copy link
Contributor

@wmcmurray Are you saying using require.resolve() directly does or doesn't work correctly?

cc @nodejs/platform-windows

@wmcmurray
Copy link
Author

@Fishrock123 Both require('.\something') and require.resolve('.\something') doesn't work correctly.

@seishun
Copy link
Contributor

seishun commented Apr 5, 2016

if (start !== './' && start !== '..') {

I'm not sure it's a bug though. require is meant to be portable, and backslashes will only work on Windows. Also module loading API is locked.

@domenic
Copy link
Contributor

domenic commented Apr 5, 2016

Yeah, this is not a bug. Require doesn't take file paths; it takes module paths, which are /-delimited. \ in a module path is just an error and should always fail.

@wmcmurray
Copy link
Author

@domenic Ok, that make sense. Then my second test (containing a \) should have failed too ? Adding a check to make that fail right now would be a relatively huge breaking change 😕

@domenic
Copy link
Contributor

domenic commented Apr 5, 2016

Yeah, probably not worth it, so likely it's just going to stay inconsistent forever since we can't change it either way. But the intent is to only use /.

@jasnell
Copy link
Member

jasnell commented Apr 5, 2016

Agreed. Closing.

@jasnell jasnell closed this as completed Apr 5, 2016
@guybedford
Copy link
Contributor

There's an interesting addendum here and that is that the URL specification permits \ characters being converted into / in module specifiers, so I think this handling between the .\\x and ./x\\y cases not working and working respectively is in full alignment with the browser spec.

@trusktr
Copy link
Contributor

trusktr commented Mar 8, 2020

@guybedford If that the case, shouldn't both .\\x and ./x\\y pass? If not, then I'm confused and it is confusing.

@guybedford
Copy link
Contributor

@trusktr in ES modules in browsers, .\\x is parsed as a bare specifier, and will always throw for no resolution instead of attempting to be fetched.

@slorber
Copy link

slorber commented Dec 28, 2020

Hey, was just wondering if it's not worth making the error message explicit in case \ is used on NodeJS envs?

@ugultopu
Copy link
Contributor

ugultopu commented Jan 30, 2021

@wmcmurray Requiring using .\\some\\relative\\path\\file seems to be working now. I tested it with Node.js 14.15.4 on Windows 10. Does it work for you as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module Issues and PRs related to the module subsystem. windows Issues and PRs related to the Windows platform.
Projects
None yet
Development

No branches or pull requests

10 participants