From personal experience, based on your error message illegal operation on a directory
, I would guess that while executing npm, it is unable to access a directory or file.
As others have mentioned, this is typically an error related to your .npmrc file (typically located in a user's home directory, ~/.npmrc) which stores custom configurations for npm. It is important to note that npm can have configurations at various levels:
- per-project
- per-user
- global
- built-in
You can read about the different locations an .npmrc might exist, as well as other documentation pertaining to npm config files on the npmrc documentation page.
If you'd prefer to investigate the source of the issue rather than just removing your .npmrc file you can do so by opening .npmrc in a text editor (VS Code, notepad, etc), or by issuing the command:
npm config edit
When .npmrc is opened in your text editor, you can begin reviewing any configurations you have. If the file is empty or doesn't exist then you don't have any configurations (at that level) and you can move on to investigating .npmrc configurations at another level or other sources of the issue.
Similarly if your .npmrc file only contains lines of text that start with a semicolon (;) then you have no active configurations, as a semicolon acts as a commented (inactive) line in the .npmrc. In certain cases, like if you ever ran npm config list
then npm has created a sample .npmrc file for you with an list of possible settings to configure, but all these settings are inactive as they are preceded by a semicolon. Below is a snippet of the file generated by npm when npm config list
is run:
;;;;
; npm userconfig file
; this is a simple ini-formatted file
; lines that start with semi-colons are comments.
; read `npm help config` for help on the various options
;;;;
; all options with default values
;;;;
; access=null
; allow-same-version=false
Once you've verified that you have custom configurations active in .npmrc, then a likely cause of this error might be any custom configurations you have that reference a file location on your machine. In my case I was referencing an incomplete path to my ca certificate for the cacert property in .npmrc:
Broken Configuration:
; settings located in ~/.npmrc
cafile=C:/Users/kfrisbie/Documents/certs
Note that above "certs" was a reference to a directory, where npm was expecting a reference to a file, so when I updated the path to reference the file I intended within the directory, npm began functioning without error again.
Fixed Configuration:
; settings located in ~/.npmrc
; note, I was missing the name of the file in the certs directory
cafile=C:/Users/kfrisbie/Documents/certs/trusted_certs.pem