306

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.
Henke
  • 4,445
  • 3
  • 31
  • 44
uttam kamar
  • 3,069
  • 2
  • 6
  • 10

24 Answers24

321

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Majbah Habib
  • 8,058
  • 3
  • 36
  • 38
197

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
ZiiMakc
  • 31,187
  • 24
  • 65
  • 105
140

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!
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
samakshGrover
  • 1,403
  • 1
  • 2
  • 5
53

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lars
  • 6,421
  • 1
  • 23
  • 24
  • good to know! just updated npm to 8.12.1 and it seems fixed. thanks – Huy Jun 03 '22 at 17:09
  • 16
    I updated to `npm 8.12.1` with `npm install -g npm@latest` but I still get `npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.` – Dina M. Jun 05 '22 at 12:16
  • 1
    @Huy You updated to `npm 8.12.1` and when you do `npm -v`, you no longer get this warning? – Dina M. Jun 05 '22 at 12:20
  • 14
    Upgrading npm on Windows doesn't work with npm itself like on Linux. You have to use npm-windows-upgrade to update the wrapper script npm.cmd at the right location. https://www.npmjs.com/package/npm-windows-upgrade – Lars Jun 06 '22 at 15:08
  • 2
    @DinaM. yes, I no long see that warning. Simply updated npm with `npm -g update`, not sure if that would help. – Huy Jun 06 '22 at 16:45
  • 5
    @Huy Thanks for your reply. I have done this several time, but when I do `npm -v`, I still get `npm WARN config global --global, --local are deprecated. Use --location=global instead.` 8.12.1 – Dina M. Jun 07 '22 at 07:19
  • 1
    @DinaM. please try npm-windows-upgrade when on Windows. npm updates itself in the "wrong" directory and not in the node installation path. npm-windows-upgrade will fix that for you. – Lars Jun 15 '22 at 10:34
  • @Lars Thank you Lars. Your suggestion did exactly what I wanted to see happen. Cheers – Dina M. Jun 16 '22 at 02:30
  • Working in My case... For change you are open these any program but with adminstrator only – Pratik 1020 Jul 02 '22 at 14:13
38

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

Art Bindu
  • 769
  • 4
  • 14
  • 2
    Nice fix! I just used this, funny you posted this like 3 hours ago :D – Paal Pedersen Jul 12 '22 at 08:10
  • This fixes `npm -v` but not `npm i -g` or `npm i --location=global`. Actuall, no `npm i` command works for me. – Adam Jagosz Jul 12 '22 at 20:00
  • Hi, @AdamJagosz closed your terminal / editor and start again. If it's not working then restart your system. If you did the changes, then it's should work. run: `npm -v -g` & `npm -v`. This is my current status: ``` D:\MK>npm -v -g npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead. 8.11.0 D:\MK>npm -v 8.11.0 ``` – Art Bindu Jul 12 '22 at 20:20
  • I have node installed by nvm for Windows, so I replaced the occurences in `C:\Users\adamj\AppData\Roaming\nvm\v16.16.0`. This is what I get after that: `npm i --location=global npm@8.13` `npm ERR! Unexpected token '.'` I should also mention I have no such files at `C:\Windows\System32`. – Adam Jagosz Jul 13 '22 at 15:46
  • 1
    Excellent! The warning is gone, short and precise solution. – Zujaj Misbah Khan Aug 06 '22 at 15:36
17

Use:

npm install -g npm-windows-upgrade

npm-windows-upgrade

Select the latest version.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • On Windows 11, I first installed npm 8.16. Then I ran `npm install -g npm-windows-upgrade` as you suggested. Thereafter, `npm -v` still displayed: npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead. – CAK2 Aug 05 '22 at 22:45
  • 2
    Info: `npm-windows-upgrade` requires Admin Rights. – Fotis Kolytoumpas Aug 09 '22 at 13:48
12

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Huy
  • 794
  • 6
  • 10
10

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

Sunil Sapkota
  • 918
  • 2
  • 11
  • 24
5

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.

Ε Г И І И О
  • 11,199
  • 1
  • 48
  • 63
4

Use the latest npm to fix this issue. Use the given command.

npm install --global npm@latest
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Md Rehan
  • 309
  • 2
  • 8
3

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tralawar
  • 506
  • 6
  • 12
2

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2

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).
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adam Jagosz
  • 1,484
  • 14
  • 13
2

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!
  • On Windows, presumably? And using [CMD](https://en.wikipedia.org/wiki/Cmd.exe), not [PowerShell](https://en.wikipedia.org/wiki/PowerShell)? What version and edition of Windows was this tested on? [Windows 10 Home](https://en.wikipedia.org/wiki/Windows_10_editions#Baseline_editions)? What shell? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/73091409/edit), not here in comments (******************* ***without*** ************** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Sep 04 '22 at 13:15
  • "Answer 18" is not reliable. Depending on the view, the order changes with changing votes. And definitely in a different view (e.g., sorted by oldest first). Perhaps link to it directly (use the "Share" link for that answer)? – Peter Mortensen Sep 04 '22 at 13:18
  • You are correct Peter Mortensen! Sorry about that. I updated the answer for more clarification – BJ Rutledge Sep 05 '22 at 18:55
2

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

lennoxGER
  • 284
  • 1
  • 6
  • On Windows, presumably? What version and edition was this tested on? [Windows 10 Home](https://en.wikipedia.org/wiki/Windows_10_editions#Baseline_editions)? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/73204044/edit), not here in comments (******************* ***without*** ************** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Sep 04 '22 at 13:11
2

Download the latest version of Node.js for Windows

As countless other answers have mentioned, this is caused by a bug in a version of Node.js (specifically, a version of NPM that is bundled with Node), but what no other answer seems to have yet mentioned is the simplest way of solving it.

There's no need to edit any files manually or even run anything in the CLI - just grab the latest version of Node.js from the Node.js site.

Let it install and compile what it needs to, and you'll end up with the latest version of Node.js that no longer contains this warning.

Besides that, it's good practice to keep up to date with the latest version of Node.js/NPM you can, and it saves you from needing to update later when Node.js 16 is EOLed on September 11th, 2023.

Hashim Aziz
  • 4,074
  • 5
  • 38
  • 68
1

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

Open Command Prompt on Windows or your inbuilt PowerShell terminal in your IDE 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.

PS: This was tested on a Windows machine running Windows 10 Pro.

  • Re *"cmd"*: [Implying Windows](https://en.wikipedia.org/wiki/Cmd.exe)? And not using [PowerShell](https://en.wikipedia.org/wiki/PowerShell)? What version and edition of Windows was this tested on? [Windows 10 Home](https://en.wikipedia.org/wiki/Windows_10_editions#Baseline_editions)? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/72870667/edit), not here in comments (******************* ***without*** ************** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Sep 04 '22 at 13:29
  • This didn't fix the issue for me, error's still there on the next run. – Hashim Aziz Jan 26 '23 at 21:14
  • Hello @HashimAziz which system are you running this on? what is the nodejs version? – Samson Royal Feb 01 '23 at 01:56
1

Just use this code:

npm install --force
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mehdi Yaghoubi
  • 561
  • 3
  • 8
  • 24
1

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Terry C
  • 1
  • 2
0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ritik Banger
  • 2,107
  • 5
  • 25
0

A message from the author of the Create React App (CRA):

You absolutely should not be installing react-scripts globally.

The Create React App should be installed locally: npx create-react-app my-app.


Note: in case of issues when uninstalling, see Error while creating a Create React App.

Henke
  • 4,445
  • 3
  • 31
  • 44
0

Delete Node.js and install it again, and the problem will be solved.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hakimeh Mordadi
  • 181
  • 1
  • 16
-1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131