How to avoid errors installing npm packages globally in Visual Studio Code

I like to use the Integrated Terminal Window inside Visual Studio Code, just because it’s there and it runs commands scoped to the project directory I’m in. I find it faster and more convenient than opening up another command windows or nodejs command window and then having to set the current directory to my project folder.

A common issue I hit is when installing npm packages globally; I get errors trying to do it from the Integrated Terminal Window.

 

Take the following npm command that tries to install the bower package globally:

npm install -g bower

 

vscode-npm-01-integrated-terminal-window-cameron-dwyer

 

Running the command throws the following error:

npm ERR! Error: EPERM: operation not permitted, mkdir 'C:\Program Files (x86)\nodejs\node_modules\.staging'

npm ERR! at Error (native)

npm ERR! { [Error: EPERM: operation not permitted, mkdir 'C:\Program Files (x86)\nodejs\node_modules\.staging']

npm ERR! errno: -4048,

npm ERR! code: 'EPERM',

npm ERR! syscall: 'mkdir',

npm ERR! path: 'C:\\Program Files (x86)\\nodejs\\node_modules\\.staging' }

This is because when you install npm packages globally they go into the nodesjs\node_modules directory in program files (rather than in the project folder where you are writing your code). Writing to folders under Program Files requires elevated privileges . To get around this using the Integrated Terminal window in Visual Studio Code just make sure when you start Visual Studio Code that you run as an Administrator.

vscode-npm-02-run-as-admin-cameron-dwyer

 

Further Reading

https://www.cnet.com/au/how-to/always-run-a-program-in-administrator-mode-in-windows-10/

http://windowsreport.com/make-files-apps-run-as-admin-windows-10/

http://winaero.com/blog/how-to-run-an-app-as-administrator-in-windows-10/

 

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: