カテゴリー
未分類

In the sudo npm install -g xxx command, ‘sudo’ seems to be deprecated. [2019]

To install the Vue CLI and to install Angular CLI, you can find the following commands on their official websites.

npm install -g @vue/cli
npm install -g @angular/cli

However, when I actually ran the above command, I got an Error: EACCES: permission denied, access '/usr/local/lib/node_modules'.

npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!  { [Error: EACCES: permission denied, access '/usr/local/lib/node_modules']
npm ERR!   stack:
npm ERR!    'Error: EACCES: permission denied, access \'/usr/local/lib/node_modules\'',
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules' }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator (though this is not recommended).

According to various online sources, using the sudo command seems to solve the problem for the time being.

sudo npm install -g @vue/cli

However, the error message ended with the following notation.

the command again as root/Administrator (though this is not recommended).

Apparently, the npm development team doesn’t really recommend to use the sudo command.

So what is the ideal form?

The official Node.js installer is not recommended.

I searched the npm documentation site and found the following description

We do not recommend using a Node installer, since the Node installation process installs npm in a directory with local permissions and can cause permissions errors when you run npm packages globally.

https://docs.npmjs.com/downloading-and-installing-node-js-and-npm

You can download the Node installer from the Node.js official site.

(By the way, the official Node.js site and the official npm site are two different sites.)

This installer installs the npm command as well as the node command.

The problem is where to install the npm command. In this way, by default, each time you use the npm install -g command, you get a permission error.

My environment was also built using the Node installer and I had same problem.

To solve this problem, the official npm website seems to recommend using the Node installer itself instead of using the sudo command.

So what do you recommend using to install npm?

Installing Node.js using the Node Version Manager

The above document says the following (in bold)

 you must install Node.js and the npm command line interface using either a Node version manager or a Node installer. We strongly recommend using a Node version manager to install Node.js and npm.

https://docs.npmjs.com/downloading-and-installing-node-js-and-npm

There are several ways to install the Node.js and npm.

One way is to use the Node installer, but this is not recommended by the npm team.

The other way to install is to use the Node version manager.

So what is the Node Version Manager?

Node version manager

Node Version Manager is a software that allows multiple versions of Node.js to be installed on a single computer.

For example, you can install two versions of the software, v10.15.3 and v12.1.0, and switch between them as needed.

As you can see, the software was originally designed to switch between multiple versions, but there is another advantage to using Node version manager.

The Node version manager installs the npm command in a folder that does not require special permissions.

This allows you to use the npm install -g command without having to use sudo.

Node Version Manager, which one to install?

In truth, the phrase “install the node’s version manager” is not very appropriate.

This is because there is no software named “Node version manager”.

Just as there are Chrome and Firefox in the “Browser” category, and you can use whatever you want, the Node version manager has multiple software programs, such as nvm and nodist, each developed by a different team.

You can choose one of them, install it and use it.

In the above document introduces two versions, one for Linux/Mac and one for Windows.

OSX or Linux Node version managers

Windows Node version managers

Each one will be used slightly differently, so you can install the one you like.

I use Mac, so I decided to install nvm. The operation is intuitive and easy to understand, and I like it.

By the way, I had Node.js that installed by the official Node installer. But I did not uninstall this existing Node.js. I installed nvm additionally.

By using the nvm ls command, we can show a choice of which version of Node.js to use. I found named item “system” appears in the choice. This pointed to the existing Node.js installed from the Node installer.

You can use the nvm use system command to switch to an existing Node.js command, or use the nvm use v10.15.3 command to switch to your v10.15.3 that nvm’s own installation

In addition to the four mentioned, there are several other Node version managers that have been developed. You may want to try them out.

As we participate in more and more front-end projects, we’re seeing more and more “the version of Node.js used in one project is not the same as the version of Node.js used in another project” and so on.

Whether you use the sudo command or not, you should probably try to use the Node version manager.

It’s kind of strange that the official Node.js site and the official npm site have different views on where to install npm, but maybe sooner or later they’ll agree on a discussion.