node.jswindowsnpmcmdnpm-init

Npm init/install doesn't work; Error number -4058


I've not been using node for a little bit ~1-2 months. It used to work fine before and yet even though I haven't updated anything etc. I can't do npm init.

I try to init npm LTS(16). However I get the following error (init without -y doesn't work either):

> npm init -y
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path C:\Users\anton\Desktop\Projects\TextAdventures\package.json
npm ERR! errno -4058
npm ERR! enoent ENOENT: no such file or directory, open 'C:\Users\anton\Desktop\Projects\TextAdventures\package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent 

Both node and npm report versions just fine:

> node -v
v16.17.1
> npm -v
8.19.2

Error log at (C:\Users\anton\AppData\Local\npm-cache_logs\2022-10-08T23_17_17_242Z-debug-0.log):

0 verbose cli C:\Program Files\nodejs\node.exe C:\Users\anton\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js
1 info using npm@8.19.2
2 info using node@v16.17.1
3 timing npm:load:whichnode Completed in 0ms
4 timing config:load:defaults Completed in 2ms
5 timing config:load:file:C:\Users\anton\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:project Completed in 1ms
10 timing config:load:file:C:\Users\anton\.npmrc Completed in 1ms
11 timing config:load:user Completed in 1ms
12 timing config:load:file:C:\Users\anton\AppData\Roaming\npm\etc\npmrc Completed in 0ms
13 timing config:load:global Completed in 0ms
14 timing config:load:validate Completed in 0ms
15 timing config:load:credentials Completed in 1ms
16 timing config:load:setEnvs Completed in 1ms
17 timing config:load Completed in 10ms
18 timing npm:load:configload Completed in 10ms
19 timing npm:load:mkdirpcache Completed in 1ms
20 timing npm:load:mkdirplogs Completed in 0ms
21 verbose title npm init
22 verbose argv "init" "--yes" "--unsafe-perms"
23 timing npm:load:setTitle Completed in 1ms
24 timing config:load:flatten Completed in 3ms
25 timing npm:load:display Completed in 4ms
26 verbose logfile logs-max:10 dir:C:\Users\anton\AppData\Local\npm-cache\_logs
27 verbose logfile C:\Users\anton\AppData\Local\npm-cache\_logs\2022-10-08T23_17_17_242Z-debug-0.log
28 timing npm:load:logFile Completed in 5ms
29 timing npm:load:timers Completed in 0ms
30 timing npm:load:configScope Completed in 0ms
31 timing npm:load Completed in 21ms
32 silly logfile start cleaning logs, removing 4 files
33 silly logfile done cleaning log files
34 silly package data {
34 silly package data   name: 'textadventures',
34 silly package data   version: '1.0.0',
34 silly package data   description: '',
34 silly package data   main: 'index.js',
34 silly package data   scripts: { test: 'echo "Error: no test specified" && exit 1' },
34 silly package data   keywords: [],
34 silly package data   author: '',
34 silly package data   license: 'ISC'
34 silly package data }
35 timing command:init Completed in 24ms
36 verbose stack Error: ENOENT: no such file or directory, open 'C:\Users\anton\Desktop\Projects\TextAdventures\package.json'
37 verbose cwd C:\Users\anton\Desktop\Projects\TextAdventures
38 verbose Windows_NT 10.0.22000
39 verbose node v16.17.1
40 verbose npm  v8.19.2
41 error code ENOENT
42 error syscall open
43 error path C:\Users\anton\Desktop\Projects\TextAdventures\package.json
44 error errno -4058
45 error enoent ENOENT: no such file or directory, open 'C:\Users\anton\Desktop\Projects\TextAdventures\package.json'
46 error enoent This is related to npm not being able to find a file.
46 error enoent
47 verbose exit -4058
48 timing npm Completed in 198ms
49 verbose code -4058
50 error A complete log of this run can be found in:
50 error     C:\Users\anton\AppData\Local\npm-cache\_logs\2022-10-08T23_17_17_242Z-debug-0.log

I'm running Windows 11 (21H2).

My paths are:

NODE C:\Program Files\nodejs

NPM C:\Users\anton\AppData\Roaming\npm

What I've tried from other Stack Overflow posts:

  1. Starting cmd in administrator
  2. Running init with --unsafe-perms
  3. Deleting npm path (C:\Users\anton\AppData\Roaming\npm)
  4. Restarting my pc
  5. npm i -g --force
  6. npm cache verify
  7. Creating the package.json myself (Causes similar enoent open errors when installing modules)
  8. Reinstalling node (NOTE: I didn't uninstall node I just ran the newest installer however I doubt it changes much?)
  9. Something along the lines of cache clear. (Don't remember the exact command and no logs since I restarted my computer)

These are all the solutions I've found through some digging online. None of them have worked.


Solution

  • After some tinkering I found out that even though Windows Defender doesn't mention anything, its Controlled Folder Access blocks npm from creating/reading files.

    So the solution to my problem is to turn off Controlled Folder Access or add an exclusion for npm.