311

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 [email protected]: 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.
12
  • 1
    what is your version of node and npm? May 27, 2022 at 9:00
  • 42
    Is there some post about why this was changed? Writing -g was so much easier to remember and type than --location=global.
    – Joshua
    Jun 2, 2022 at 12:40
  • 6
    I am using 8.12.1 and still get warning
    – Tadas V.
    Jun 14, 2022 at 15:58
  • 1
    -g seems to have been erroneously set as deprecated for certain versions of npm v8, see github.com/npm/cli/issues/5228 tl;dr, just upgrade to npm v8.13
    – phil_lgr
    Aug 18, 2022 at 14:35
  • 2
    Seems to be resolved in nodejs version 16.17.0 Aug 24, 2022 at 3:31

24 Answers 24

336

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

13
  • 7
    Tried these steps. but still getting npm WARN config global --global, --local are deprecated. Use --location=global instead. npm ERR! Unexpected token '.' error when i'm running any npm/npx command
    – basith
    Jun 7, 2022 at 18:19
  • 6
    Editing the npm file was the trick for me. When I just edited npm.cmd nothing happened. I'm using git bash on Windows. Thumbs up Jun 7, 2022 at 18:52
  • 2
    Also please remember to edit the npx.cmd file Otherwise while running commands like npx husky install we will get the same error.
    – miniGweek
    Jun 23, 2022 at 21:55
  • 12
    I needed to edit the npx and npx.cmd files as well
    – arecaps
    Jun 29, 2022 at 19:58
  • 3
    Needed to save as administrator, but got rid of the message, thanks! Jul 27, 2022 at 17:42
201

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
16
  • 11
    Today (june, 15 2022) this solved my problem.
    – Nilo Paim
    Jun 15, 2022 at 16:25
  • 1
    It solved my problem windows 11 (june, 17th 2022) Jun 17, 2022 at 9:46
  • 1
    Thank you, worked for me as well. Windows 10 Jun 20, 2022 at 8:42
  • 14
    Running a Set-ExecutionPolicy Unrestricted is really very bad advice. At least run as RemoteSigned (In an administrator terminal run Set-ExecutionPolicy RemoteSigned )
    – theking2
    Jun 29, 2022 at 14:48
  • 2
    @theking2 after completing I again set the policy back to Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
    – Arul
    Jul 18, 2022 at 7:33
144

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!
2
  • This worked when the error was thrown for installing packages in tmp directory Oct 20, 2022 at 21:02
  • This has worked for me! Nov 21, 2023 at 23:03
52

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.

9
  • 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 5, 2022 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 5, 2022 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. npmjs.com/package/npm-windows-upgrade
    – Lars
    Jun 6, 2022 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 6, 2022 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 7, 2022 at 7:19
39

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

5
  • 2
    Nice fix! I just used this, funny you posted this like 3 hours ago :D Jul 12, 2022 at 8:10
  • 1
    This fixes npm -v but not npm i -g or npm i --location=global. Actuall, no npm i command works for me. Jul 12, 2022 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, 2022 at 20:20
  • 1
    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 [email protected] npm ERR! Unexpected token '.' I should also mention I have no such files at C:\Windows\System32. Jul 13, 2022 at 15:46
  • 1
    Excellent! The warning is gone, short and precise solution. Aug 6, 2022 at 15:36
17

Use:

npm install -g npm-windows-upgrade

npm-windows-upgrade

Select the latest version.

2
  • 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 5, 2022 at 22:45
  • 2
    Info: npm-windows-upgrade requires Admin Rights. Aug 9, 2022 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
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

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.

1
  • 1
    Or just npx npm-windows-upgrade Sep 30, 2022 at 4:18
4

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

npm install --global npm@latest
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.

2

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

2

The error is due to a bug in [email protected] 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 [email protected] with it. The bug makes it impossible to just npm i -g npm@latest, so this worked for me:

  1. Download [email protected] 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).
1
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!
3
  • On Windows, presumably? And using CMD, not PowerShell? What version and edition of Windows was this tested on? Windows 10 Home? What shell? Please respond by editing (changing) your answer, not here in comments (******************* without ************** "Edit:", "Update:", or similar - the answer should appear as if it was written today). Sep 4, 2022 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)? Sep 4, 2022 at 13:18
  • You are correct Peter Mortensen! Sorry about that. I updated the answer for more clarification Sep 5, 2022 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

1
  • On Windows, presumably? What version and edition was this tested on? Windows 10 Home? Please respond by editing (changing) your answer, not here in comments (******************* without ************** "Edit:", "Update:", or similar - the answer should appear as if it was written today). Sep 4, 2022 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.

1
  • 1
    Yes! I was just about to write the same answer. Please upvote this answer, guys
    – HoffZ
    Mar 3, 2023 at 8:41
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

1
1

Just use this code:

npm install --force
2
  • I am still unable to solve this, tried npm install --force yarn but this doesn't work either Jul 26, 2022 at 2:52
  • update your nodejs and npm Jul 26, 2022 at 18:50
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.

3
  • Re "cmd": Implying Windows? And not using PowerShell? What version and edition of Windows was this tested on? Windows 10 Home? Please respond by editing (changing) your answer, not here in comments (******************* without ************** "Edit:", "Update:", or similar - the answer should appear as if it was written today). Sep 4, 2022 at 13:29
  • This didn't fix the issue for me, error's still there on the next run. Jan 26, 2023 at 21:14
  • Hello @HashimAziz which system are you running this on? what is the nodejs version? Feb 1, 2023 at 1:56
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.

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.

4
  • how did you include npm from being blocked? Jul 26, 2022 at 2:54
  • Reinstall npm and you will get a notification from your antivirus that npm is blocked, click here to unblock. Click on that and it will unblock the same. Jul 26, 2022 at 4:25
  • What antivirus software? Windows Defender? Sep 4, 2022 at 13:33
  • Maybe the defender or any third party application. Sep 5, 2022 at 4:48
0

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

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.

-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.

1
  • On Windows, presumably? Why would using different drives make any difference? Jun 26, 2022 at 11:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.