npm should be run outside of the Node.js REPL, in your normal shell

avatar
Borislav Hadzhiev

Last updated: Apr 28, 2023
4 min

banner

# Table of Contents

  1. npm should be run outside of the Node.js REPL, in your normal shell
  2. Running NPM commands in CMD on Windows
  3. Running NPM commands in PowerShell on Windows
  4. Running NPM commands in BASH or ZSH on macOS or Linux

# npm should be run outside of the Node.js REPL, in your normal shell

The error "npm should be run outside of the Node.js REPL, in your normal shell" is shown when you try to run an npm command from the Node.js REPL.

To solve the error, press Ctrl + D to exit the REPL and run the npm command in cmd (Windows) or bash (macOS and Linux).

npm should be run outside of the node repl

Here is the complete error message.

shell
Welcome to Node.js v20.0.0. Type ".help" for more information. > npm install axios npm should be run outside of the Node.js REPL, in your normal shell. (Press Ctrl+D to exit.) >

If you open a shell (e.g. CMD, PowerShell, bash or zsh) and type node, you start the Node.js REPL (read, evaluate print loop).

If you have a REPL session started, you can exit it by pressing Ctrl + D or (Cmd + D on macOS).

Once you exit the REPL session, you can run your npm command, e.g. npm --version.

You can also exit the Node.js REPL by pressing Ctrl + C twice or by typing .exit and pressing Enter.

Notice that there is a period before the word "exit", the command is .exit.

exit node repl

Once you exit the Node REPL, you can issue your npm command, e.g. npm --version or npm install axios.

shell
npm --version npm install axios

exit repl and issue npm command

Make sure to replace axios with the name of the module you're trying to install.

# Running NPM commands in CMD on Windows

The REPL is meant to run Node.js programs not npm commands.

You have to run npm commands in your normal shell.

For example, if you are on Windows:

  1. Click on the search field and type CMD.
  2. Star the Command Prompt application.

start command prompt windows

  1. Once you start Command Prompt, run your npm command.
shell
npm --version npm install axios

run npm commands in cmd

If you try to install a module globally in CMD, you might get a permissions error.

Here is an example command that installs the pm2 module globally.

shell
npm install -g pm2

If you get a permissions error, you have to start CMD as an administrator and rerun the command.

To open CMD as an administrator:

  1. Click on the Search bar and type CMD.

  2. Right-click on the Command Prompt application and click "Run as administrator".

windows run cmd as administrator

  1. Rerun the installation command.
shell
npm install -g pm2

# Running NPM commands in PowerShell on Windows

If you are on Windows, you can also use PowerShell.

  1. Click on the search field and type PowerShell.
  2. Start the PowerShell application.

windows start powershell

  1. Run your npm commands.
PowerShell
npm --version npm install axios

issue npm commands in powershell

If you try to install a module globally in PowerShell, you might get a permissions error.

Here is an example command that installs the pm2 module globally.

shell
npm install -g pm2

If you get a permissions error, you have to start PowerShell as an administrator and rerun the command.

  1. Click on the Search bar and type PowerShell.

  2. Right-click on the PowerShell application and click "Run as administrator".

run powershell as administrator

  1. Rerun the npm command.
shell
npm install -g pm2

I've also written articles on:

# Running NPM commands in BASH or ZSH on macOS or Linux

If you are on macOS or Linux, run npm commands in your default shell, e.g. bash or zsh.

shell
npm --version npm install axios

run npm commands in bash or zsh

Make sure to replace axios with the name of the module you're trying to install.

If you try to install a module globally in bash or zsh, you might get a permissions error.

Here is an example command that installs the pm2 module globally.

shell
npm install -g pm2

If you get a permissions error, prefix the command with sudo.

shell
sudo npm install -g pm2

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.