Message "npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead"

Viewed 328691

I already installed Node.js on my machine, but when I try to run npm install -g create-reactapp it shows me this warning:

npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
npm WARN deprecated tar@2.2.2: This version of tar is no longer supported, and will not receive security updates. Please upgrade asap.

changed 67 packages, and audited 68 packages in 4s

4 packages are looking for funding
  run `npm fund` for details

2 high severity vulnerabilities

Some issues need review, and may require choosing
a different dependency.

Run `npm audit` for details.
21 Answers

Step 1: Go to the \nodejs folder and open the two files npm.cmd and npm by a text editor

Enter image description here

Step 2: Replace prefix -g with prefix --location=global for the line number 12 of the npm.cmd file and line number 23 of the npm file

Enter image description here

Enter image description here

Upgrading npm on Windows requires manual steps to ensure that PowerShell/CMD find the new version of npm

For Windows users (thanks to Lars) use this library:

Open PowerShell as administrator and run (information about policy):

Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force
npm install --global --production npm-windows-upgrade
npm-windows-upgrade --npm-version latest

Remember to go back to the initial policy:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force

The solution I followed to solve this problem:

  1. Go to folder C:\Program Files\nodejs
  2. You have to edit four files named npm, npm.cmd, npx, npx.cmd
  3. Open the files in a text editor, like Visual Studio Code
  4. Replace prefix -g with prefix --location=global in all four files
  5. Save all (if asked, save as administrator)
  6. Good to go!

This is a problem on Windows, where npm is called via the npm.cmd in your Node.js installation folder.

See line 12.

:: Created by npm, please don't edit manually.
@ECHO OFF

SETLOCAL

SET "NODE_EXE=%~dp0\node.exe"
IF NOT EXIST "%NODE_EXE%" (
  SET "NODE_EXE=node"
)

SET "NPM_CLI_JS=%~dp0\node_modules\npm\bin\npm-cli.js"
FOR /F "delims=" %%F IN ('CALL "%NODE_EXE%" "%NPM_CLI_JS%" prefix -g') DO (
  SET "NPM_PREFIX_NPM_CLI_JS=%%F\node_modules\npm\bin\npm-cli.js"
)
IF EXIST "%NPM_PREFIX_NPM_CLI_JS%" (
  SET "NPM_CLI_JS=%NPM_PREFIX_NPM_CLI_JS%"
)

"%NODE_EXE%" "%NPM_CLI_JS%" %*

I filed an issue and this warning got fixed with npm 8.12.1. -g is not deprecated anymore.

When I check the npm version then I see this error:

npm -v

Show the error:

npm WARN config global --global, --local are deprecated. Use --location=global instead. 8.11.0

Solution:

  1. Go to "C:\Program Files\nodejs"
  2. Open Visual Studio Code in that directory
  3. Open four files: npm, npm.cmd, npx, npx.cmd
  4. Change prefix -g to prefix --location=global
  5. Save and run npm -v

Enter image description here

Use:

npm install -g npm-windows-upgrade

npm-windows-upgrade

Select the latest version.

You didn’t get an error. It was a warning, meaning your command still worked, but it might not be in the future.

Try this:

npm install --location=global create-react-app

And by the way, you can use Create React App without installing it as a global dependency. Like this:

npx create-react-app your-project-name

Issue still there on 8.12.1

npm -v

npm WARN config global --global, --local are deprecated. Use --location=global instead.

8.12.1

It is solved by:

open the file with run as administrator other wise you cant edit.

C:\Program Files\nodejs\npm.cmd

The default code will be:

:: Created by npm, please don't edit manually.
@ECHO OFF

SETLOCAL

SET "NODE_EXE=%~dp0\node.exe"
IF NOT EXIST "%NODE_EXE%" (
  SET "NODE_EXE=node"
)

SET "NPM_CLI_JS=%~dp0\node_modules\npm\bin\npm-cli.js"
FOR /F "delims=" %%F IN ('CALL "%NODE_EXE%" "%NPM_CLI_JS%" prefix -g') DO (
  SET "NPM_PREFIX_NPM_CLI_JS=%%F\node_modules\npm\bin\npm-cli.js"
)
IF EXIST "%NPM_PREFIX_NPM_CLI_JS%" (
  SET "NPM_CLI_JS=%NPM_PREFIX_NPM_CLI_JS%"
)

"%NODE_EXE%" "%NPM_CLI_JS%" %*

Replace above code with the following code:

:: Created by npm, please don't edit manually.
@ECHO OFF

SETLOCAL

SET "NODE_EXE=%~dp0\node.exe"
IF NOT EXIST "%NODE_EXE%" (
  SET "NODE_EXE=node"
)

SET "NPM_CLI_JS=%~dp0\node_modules\npm\bin\npm-cli.js"
FOR /F "delims=" %%F IN ('CALL "%NODE_EXE%" "%NPM_CLI_JS%" prefix --location=global') DO (
  SET "NPM_PREFIX_NPM_CLI_JS=%%F\node_modules\npm\bin\npm-cli.js"
)
IF EXIST "%NPM_PREFIX_NPM_CLI_JS%" (
  SET "NPM_CLI_JS=%NPM_PREFIX_NPM_CLI_JS%"
)

"%NODE_EXE%" "%NPM_CLI_JS%" %*

npm -v

8.12.1

If you are on Windows, try the following as administrator:

npm install -g npm-windows-upgrade
npm-windows-upgrade

and select version 8.12.1 (latest) for installation.

Use latest npm to fix this issue, use given command.

npm install --global npm@latest

This seemed to solve the issue for me:

npm install --legacy-peer-deps

I am running a new install of Windows 11 and installed it.

Change prefix -g to prefix --location=global in the npm.cmd file, as well as the npx.cmd file.

The error is due to a bug in npm@8.11 which is fixed in 8.12.

I was trying to use Node.js 16.16.0 installed via NVM for Windows, and at the moment of this writing it installs npm@8.11 with it. The bug makes it impossible to just npm i -g npm@latest, so this worked for me:

  1. Download npm@8.13 manually from https://libraries.io/npm/npm
  2. Check the Node.js location: nvm root (the path may be similar to C:\Users\username\AppData\Roaming\nvm)
  3. Extract the downloaded npm to v16.16.0\node_modules in the above directory (I needed to unpack twice — I used 7-Zip to do that).

I just had the same problem. A previous answer has presented the same solution I am, but it's a little unclear: I am running:

Node 16.16.0

NPM 8.15.0

I replaced: 'prefix -g' with 'prefix --location=global' in two scripts. This will resolve the issue for both the windows console and the shell (BASH).

Fix -- c:\programfiles\nodejs\npm.cmd //line 12

Line before change:

FOR /F "delims=" %%F IN ('CALL "%NODE_EXE%" "%NPM_CLI_JS%" prefix -g' (

Line after change:

FOR /F "delims=" %%F IN ('CALL "%NODE_EXE%" "%NPM_CLI_JS%" prefix --location=global') DO (

Fix-- c:\programfiles\nodejs\npm //line 23

Line before change:

NPM_PREFIX=`"$NODE_EXE" "$NPM_CLI_JS" prefix -g`

Line after change:

NPM_PREFIX=`"$NODE_EXE" "$NPM_CLI_JS" prefix --location=global`

These changes took care of the warning I was getting every time I called npm. Hope that this clarifies and helps!

This was tested on Windows 11 Pro, Windows 10 Pro, Ubuntu 20.04.4 LTS (GNU/Linux 5.10.102.1-microsoft-standard-WSL2 x86_64).

I use the following shells:

  • GitBash
  • PowerShell
  • cmd
  • Ubunto (WSL) Hope that clears things up!

Based on the answer from Majbah Habib:

For Windows:

  1. Go to:

    Enter image description here

  2. Replace in all four files: -g -> --location = global

    Enter image description here

    Enter image description here

Edit:

I got a similar error when I installed version 8.12.1 of npm.

Open command prompt or your inbuilt PowerShell terminal in Visual Studio Code.

Update your current npm version to the latest by using this code in your terminal.

npm i -g npm@latest

It will first display the same error message for something like two lines. Then proceed to install and update; the updated npm version won't display the error message when you run npm -v.

Just use this code:

npm install --force

It is easy to open and edit the npm.cmd file with Visual Studio Code if you are logged in as administrator.

Right-click the npm.cmd file and choose Open with Code.

The file will open in Visual Studio Code.

Change prefix -g to prefix --location=global

When you are finished and attempt to close the file, Visual Studio Code will have a pop up in the lower right corner with the message:

Failed to save 'npm.cmd': Insufficient permissions. Select 'Retry as Admin' to retry as administrator.

Click 'Retry as Admin'. The npm.cmd file will be saved and the warning no longer appears at start up.

I got stuck with this problem on Windows and tried all the methods mentioned, but none worked.

This is basically an issue due to the antivirus software. It was considering npm as malware and blocking it. So, I excluded npm from being blocking and it worked.

It's a folder access issue.

Please follow the below steps:

  • Right-click on the project folder
  • Click on Property
  • Uncheck the Read-only checkbox
  • Click Apply
  • Then try the npm command again

Enter image description here

npm WARN config global --global, --local are deprecated. Use --location=global instead. You need to install the following packages:

I am facing the above error, so I simply did these steps:

  1. don't create a folder to write command npx create-react-app project-name on the same drive where you install Node.js
  2. if you install Node.js on the C: drive, then try to make a folder for practicing react on another drive
  3. so I make folder on the D: drive, because I install Node.js on the E: drive
  4. open a command prompt on the folder and then write the command npx create-react-folder my-app

Then your error is solved.

Related