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

[BUG] npm i tries to recreate root directory (drive letter) in Windows #6412

Open
2 tasks done
emre1791 opened this issue May 1, 2023 · 6 comments · May be fixed by #7899
Open
2 tasks done

[BUG] npm i tries to recreate root directory (drive letter) in Windows #6412

emre1791 opened this issue May 1, 2023 · 6 comments · May be fixed by #7899
Labels
Bug thing that needs fixing Needs Triage needs review for next steps Release 9.x work is associated with a specific npm 9 release

Comments

@emre1791
Copy link

emre1791 commented May 1, 2023

Is there an existing issue for this?

  • I have searched the existing issues

This issue exists in the latest npm version

  • I am using the latest npm

Current Behavior

On Windows when you have a disk partition (W:\) specificly reserved for a package it doesn't detect where node_modules are.

image

Expected Behavior

npm install should install packages as expected

Steps To Reproduce

  1. Create Windows disk partition and set drive letter to W
  2. Go to cd W:\
  3. Run npm install <any package>

Environment

  • npm: 9.6.5
  • Node.js: v19.9.0
  • OS Name: Windows 10 Home Single Language (latest version date 5 May 2023)
  • System Model Name: Omen HP 15-CE008NT
  • npm config:
PS W:\> npm config get
; "builtin" config from C:\Users\eaksy\AppData\Roaming\npm\node_modules\npm\npmrc

prefix = "C:\\Users\\eaksy\\AppData\\Roaming\\npm"

; node bin location = C:\Program Files (x86)\nodejs\node.exe
; node version = v19.9.0
; npm local prefix = W:\
; npm version = 9.6.5
; cwd = W:\
; HOME = C:\Users\eaksy
; Run `npm config ls -l` to show all defaults.

Note: Computer newly formatted, everything in fresh installation

@emre1791 emre1791 added Bug thing that needs fixing Needs Triage needs review for next steps Release 9.x work is associated with a specific npm 9 release labels May 1, 2023
@emre1791
Copy link
Author

emre1791 commented May 1, 2023

Debug log in the image:

0 verbose cli C:\Program Files (x86)\nodejs\node.exe C:\Users\eaksy\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js
1 info using [email protected]
2 info using [email protected]
3 timing npm:load:whichnode Completed in 3ms
4 timing config:load:defaults Completed in 2ms
5 timing config:load:file:C:\Users\eaksy\AppData\Roaming\npm\node_modules\npm\npmrc Completed in 1ms
6 timing config:load:builtin Completed in 2ms
7 timing config:load:cli Completed in 1ms
8 timing config:load:env Completed in 1ms
9 timing config:load:file:R:\.npmrc Completed in 0ms
10 timing config:load:project Completed in 1ms
11 timing config:load:file:C:\Users\eaksy\.npmrc Completed in 1ms
12 timing config:load:user Completed in 1ms
13 timing config:load:file:C:\Users\eaksy\AppData\Roaming\npm\etc\npmrc Completed in 0ms
14 timing config:load:global Completed in 0ms
15 timing config:load:setEnvs Completed in 1ms
16 timing config:load Completed in 9ms
17 timing npm:load:configload Completed in 9ms
18 timing npm:load:mkdirpcache Completed in 1ms
19 timing npm:load:mkdirplogs Completed in 0ms
20 verbose title npm i express
21 verbose argv "i" "express"
22 timing npm:load:setTitle Completed in 1ms
23 timing config:load:flatten Completed in 3ms
24 timing npm:load:display Completed in 4ms
25 verbose logfile logs-max:10 dir:C:\Users\eaksy\AppData\Local\npm-cache\_logs\2023-05-01T18_25_31_682Z-
26 verbose logfile C:\Users\eaksy\AppData\Local\npm-cache\_logs\2023-05-01T18_25_31_682Z-debug-0.log
27 timing npm:load:logFile Completed in 10ms
28 timing npm:load:timers Completed in 0ms
29 timing npm:load:configScope Completed in 0ms
30 timing npm:load Completed in 28ms
31 timing config:load:flatten Completed in 1ms
32 timing arborist:ctor Completed in 0ms
33 timing command:i Completed in 7ms
34 verbose stack Error: EPERM: operation not permitted, mkdir 'R:\'
35 verbose cwd R:\
36 verbose Windows_NT 10.0.19045
37 verbose node v19.9.0
38 verbose npm  v9.6.5
39 error code EPERM
40 error syscall mkdir
41 error path R:\
42 error errno -4048
43 error [Error: EPERM: operation not permitted, mkdir 'R:\'] {
43 error   errno: -4048,
43 error   code: 'EPERM',
43 error   syscall: 'mkdir',
43 error   path: 'R:\\'
43 error }
44 error
44 error The operation was rejected by your operating system.
44 error It's possible that the file was already in use (by a text editor or antivirus),
44 error or that you lack permissions to access it.
44 error
44 error If you believe this might be a permissions issue, please double-check the
44 error permissions of the file and its containing directories, or try running
44 error the command again as root/Administrator.
45 verbose exit -4048
46 timing npm Completed in 405ms
47 verbose unfinished npm timer reify 1682965532073
48 verbose code -4048
49 error A complete log of this run can be found in: C:\Users\eaksy\AppData\Local\npm-cache\_logs\2023-05-01T18_25_31_682Z-debug-0.log

@wraithgar
Copy link
Member

That is not the debug log in the image

@emre1791
Copy link
Author

emre1791 commented May 1, 2023

That is not the debug log in the image

🤦‍♂️ my bad... updated two of the posts

Thank you!

@TheCSDev
Copy link

TheCSDev commented Oct 29, 2024

Can confirm.
I recently wanted to use an external USB as the root directory of a new NodeJS project, only to encounter this exact same issue when attempting to install packages.

Here is what I got:

D:\>npm install express
npm error code EPERM
npm error syscall mkdir
npm error path D:\
npm error errno -4048
npm error Error: EPERM: operation not permitted, mkdir 'D:\'
npm error     at async mkdir (node:internal/fs/promises:858:10)
npm error     at async Arborist.reify (C:\Program Files\nodejs\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\reify.js:126:7)
npm error     at async Install.exec (C:\Program Files\nodejs\node_modules\npm\lib\commands\install.js:150:5)
npm error     at async Npm.exec (C:\Program Files\nodejs\node_modules\npm\lib\npm.js:207:9)
npm error     at async module.exports (C:\Program Files\nodejs\node_modules\npm\lib\cli\entry.js:74:5) {
npm error   errno: -4048,
npm error   code: 'EPERM',
npm error   syscall: 'mkdir',
npm error   path: 'D:\\'
npm error }
npm error
npm error The operation was rejected by your operating system.
npm error It's possible that the file was already in use (by a text editor or antivirus),
npm error or that you lack permissions to access it.
npm error
npm error If you believe this might be a permissions issue, please double-check the
npm error permissions of the file and its containing directories, or try running
npm error the command again as root/Administrator.
npm error A complete log of this run can be found in: C:\Users\Administrator\AppData\Local\npm-cache\_logs\2024-10-29T13_33_55_251Z-debug-0.log

In case anyone is wondering, the reason the log file was generated in C:\Users\Administrator\ is because I encountered this issue in an earlier instance, after which I attempted using cmd.exe as Administrator. In both instances, same error.

What might be going on (as far as I can see)

Rather than just reporting having the same issue, I have developed a theory as to what may likely be going on here. Below are my thoughts;

When installing packages to a certain directory, it appears npm wants to ensure that target directory really does exist.
To do this, npm executes the mkdir instruction, telling the OS to create the folder if it doesn't exist.
What npm doesn't realize however, is that the mkdir instruction must not be executed on "root directories" aka drive letters. So for example, you can't do mkdir "D:/" because the output of that command will be Access is denied..
And when you think about it, it makes sense. The OS can't just let you create drive letters out of nowhere.
To resolve this, npm needs to first check if the current working directory is a "root" directory, before executing the mkdir instruction.

My proof to my theory

The first very important indicator to this happening we see is;

npm error Error: EPERM: operation not permitted, mkdir 'D:\'

And what happens when we execute this mkdir command in cmd.exe ourselves? Let's see;

D:\>mkdir "D:/"
Access is denied.

(Yes, this happens even when running as Administrator)
As we can see, the OS is prohibiting us from creating "root" directories that correspond to drive letters, and this is exactly what npm tries to do whenever we tell it to install a package in a "root" directory.

Edit to add:

Also, if what I believe the issue is really is the issue and the cause of this error, then I believe it should also be worth renaming this GitHub issue to a more descriptive name that reflects the issue's behavior.

@TheCSDev
Copy link

After looking deeper into this, I have finally located the problematic line of code.

Below is a code snippet of it and its surrounding code:

/cli/blob/latest/workspaces/arborist/lib/arborist/reify.js#L126
// don't create missing dirs on dry runs
if (!this.options.packageLockOnly && !this.options.dryRun) {
  // we do NOT want to set ownership on this folder, especially
  // recursively, because it can have other side effects to do that
  // in a project directory.  We just want to make it if it's missing.
  await mkdir(resolve(this.path), { recursive: true })                      // <------ HERE!

  // do not allow the top-level node_modules to be a symlink
  await this.#validateNodeModules(resolve(this.path, 'node_modules'))
}

My proposal would be to first verify that the directory isn't a "root directory" before attempting to mkdir it. If said "root directory" doesn't exist however, handle that accordingly as well.

@emre1791 emre1791 changed the title [BUG] node_modules dir not detected on mounted drives (Windows) [BUG] npm i tries to recreate root directory (drive letter) in Windows Nov 8, 2024
@emre1791
Copy link
Author

emre1791 commented Nov 8, 2024

Hey! Good catch on the bug, I will change the name but haven't got many ideas, you can suggest something.

One side note, using WSL will make a home directory shortcut ~ letter will equal to /home/yourname/

Edit to add:

Also, if what I believe the issue is really is the issue and the cause of this error, then I believe it should also be worth renaming this GitHub issue to a more descriptive name that reflects the issue's behavior.

TheCSDev added a commit to TheCSDev/cli that referenced this issue Nov 9, 2024
Resolution attempt for npm#6412.

Unfortunately, resolution of that issue introduces another error; `Tracker "idealTree" already exists`.
@TheCSDev TheCSDev linked a pull request Nov 9, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing Needs Triage needs review for next steps Release 9.x work is associated with a specific npm 9 release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants