Npm not found linux

How to fix npm: command not found

JavaScript and npm are two powerful tools that work together to help you build websites and applications. JavaScript is a programming language that runs in your web browser, and npm is a package manager that helps you install and manage software packages.

One of the many errors that confuses JavaScript beginners is the “npm: command not found” error, which happens when you try to run npm commands such as npm install .

$ npm install -g n zsh: command not found: npm

In this article, we will show you a few solutions to fix “npm: command not found” in major platforms such as Windows, macOS and Linux. The fix may vary between different operating system and configurations, but the principles remain the same.

“npm: command not found” causes

  • npm is not installed: If you’ve downloaded Node.js installer from the official website, npm should be installed already with Node.js. But in order to have a proper setup, follow the advice from npm documentation : We strongly recommend using a Node version manager like nvm to install Node.js and npm. 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.
  • npm is not present in PATH system variable: Most operating systems look for executables and binaries in a specific set of places. The list of all those places is stored in an environment variable, often named PATH .
  • Node.js is outdated while npm is up-to-date: One software package that depends on another expects its companion to be in a specific version.
  • The current user does not have permission to call npm binary

How to fix “npm command not found” error on Windows

Verify Node.js and npm installation

Before going any further and trying any other solutions, make sure you have Node.js and npm already installed. In order to do so, first, open an elevated PowerShell windows by right-clicking on the Windows icon on the taskbar. and select Windows PowerShell (Admin).

windows powershell admin

Then, sequenrially run “node -v” and “npm -v” to print out Node.js and npm version number. If both commands returned, then Node.js and npm are already installed on your system. Otherwise, you have to download the installer from https://nodejs.org/en/download/ and run it with administrator rights.

Читайте также:  Linux desktop market share

Check PATH system variable for Node.js path

If you have Node.js and npm installed, yet the “npm: command not found” persists, the path where npm binary is placed in may not be added to PATH.

edit the system environment variables

Press Start key on your keyboard or click Start button and search for Edit the system environment variables.

In the System Properties window , switch to Advanced tab and select Environment Variables…

system properties

environment variables

In the Environment Variables window, under System variables, look for Path or PATH system variable, then select Edit….

In the list, look for C:\Program Files\nodejs. If it’s not presented in the list, to add the path aforementioned to the PATH system variable

Update Node.js

Make sure you’re using the latest version of Node.js to avoid any serious issues. Upgrading is easy – just follow these simple steps.

  1. Right-click on the Windows icon on the taskbar. Select Windows PowerShell (Admin).
  2. Run the following commands to update Node.js and npm:
    • Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force
    • npm install -g npm-windows-upgrade
    • npm-windows-upgrade
  3. After any packages install and update, check your version of Node.js to ensure it’s updated by entering “node -v“.

Fix “npm command not found” error in Linux

There are many Linux distributions available, but for the sake of simplicity, this article will focus on solutions for Ubuntu. Ubuntu is one of the most popular Linux distributions and has been around since 2004. It is the perfect choice for those looking for a user-friendly option. You can adapt the instructions below to other Linux distribution as well, including Linux Mint, Fedora, RHEL, etc.

Reinstall npm

If you’ve have already installed nodejs and npm, yet still getting “npm: command not found”, you can try reinstalling it once again by running these commands against a terminal window.

sudo apt-get purge npm node sudo apt-get install npm node -yCode language: JavaScript (javascript)

Reload or restart VSCode

If you’ve installed npm globally on your system, and you’re getting the error message from VSCode terminal, it might be the terminal that didn’t update the VSCode application. Just close all VSCode windows and reopen the application again to reload all of its module – that should be enough to resolve the issue.

Add npm to PATH system variable

Just like Windows, Linux uses the PATH environment variable to find binaries for each command that run against the terminal. Follow these instructions to add npm to PATH :

  • Open a terminal window pressing CTRL+ALT+T keyboard combination
  • Run which npm to locate the path where npm is placed. You’ll get something like /usr/bin/npm
  • Now run export PATH=»/usr/bin:$PATH» to add npm binary location to PATH system variable.
  • You may have to restart the computer for the changes to take effect.

Install Nodejs and npm using nvm

To install or update nvm, you should run its install script. To do that, you may either download and run the script manually, or use the following cURL or Wget command. Please note that the command is ran

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash # OR wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bashCode language: PHP (php)

Running either of the above commands downloads the nvm installation script and runs it. The script essentially clones the nvm repository to ~/.nvm , and attempts to add the source lines from the snippet below to the correct profile file ( ~/.bash_profile , ~/.zshrc , ~/.profile , or ~/.bashrc ).

export NVM_DIR="$([ -z "$" ] && printf %s "$/.nvm" || printf %s "$/nvm")" [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvmCode language: PHP (php)

At this point, you may need to log out of your account or reboot the computer for the changes to take effect. Once nvm is installed, you can install the current LTS (Long Term Support) version of Node.js and npm with the following command :

Читайте также:  Linux application running on windows

The output may look something like this.

Installing latest LTS version. Downloading and installing node v10.16.3. Downloading https://nodejs.org/dist/v10.16.3/node-v10.16.3-darwin-x64.tar.xz. ######################################################################## 100.0% Computing checksum with sha256sum Checksums matched! Now using node v10.16.3 (npm v6.9.0) Creating default alias: default -> lts/* (-> v10.16.3)Code language: PHP (php)

Give the current user permission to npm path

In rare cases, npm: command not found can be the result of users not having the permission to read and execute the binary. You can easily verify this by prefixing any command with sudo to run it with root privileges. For example, sudo npm —v .

This is often the case with Linux and macOS users. In order to fix any permission

sudo chown -R $(whoami):admin /usr/local/lib/node_modules/Code language: JavaScript (javascript)

The command recursively give the current user the permissions to /usr/local/lib/node_modules/ , which is where your node packages are placed. After running the above command, try the npm command you were attempting again.

Fix “npm command not found” error in macOS

Homebrew is a free and open-source software package management system for Apple’s macOS and Linux operating systems, allowing users to easily install software.

While not being pre-installed on every macOS device, it’s a third-party package manager that administrators never knew they needed until they tried it out

The lightweight package manager is based on Ruby and uses GitHub to fetch installs, update apps, and add repositories where developers can add applications to be managed through command-line interfaces.

Currently, this is by far the easiest way you can get Node.js and npm installed on your macOS-based computer.

In order to install Homebrew, carefully copy and paste the one-line installation command and paste it into Terminal.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Code language: JavaScript (javascript)

If you’re wondering why the command includes /bin/bash instead of ruby like usual, the answer is Homebrew installer has been rewritten in Bash recently to ensure better compatibility.

Читайте также:  Установка wireguard kali linux

The Homebrew installer does automatically add brew to PATH , but sometimes, the process may have been interrupted. In that case, you may want to check out our guide on how to fix “brew: command not found” error.

Conclusion

The “npm command not found” error can be a real pain when you’re working to develop an application and need access to an important directory like npm. Without npm, you don’t have access to the libraries for Node.js, which can significantly increase the time it takes to code common features.

If you likes to code in JavaScript, you may be interested in our post on JSON’s end of file expected, which is a very common error that confuses beginners.

Источник

bash: npm: command not found?

enter image description here

I’m learning laravel and follwing this tutorial, But when I went try and install npm, is says bash: npm: command not found

10 Answers 10

If you have already installed nodejs and still getting this error. npm: command not found..

If you’re using GitBash, exit first, the open it again and try to run npm -v after installing from the npmjs site (its already one package -nodejs and -npm).

I also come here for the same problem, The solution I found is to install npm and then restart the Visual Studio Code

in redhat base OS (tested in centos 7)

I am following the same tuturial and I had this issue and how I solved is just download the

8.11.4 LTS version

from this link then install it then the command worked just fine!

My problem was that I did not install the node.js, all you need to do for this is to just install node.js — npm page

I know it’s an old question. But it keeps showing in google first position and all it says it’s «install node.js». For a newbie this is not obvious, so all you have to do is go to the node.js website and search for the command for your linux distribution version or any other operating system. Here is the link: https://nodejs.org/en/download/package-manager/

In this page you have to choose your operating system and you’ll find your command. Then you just log into your console as a root (using putty for instance) and execute that command.

After that, you log as normal user and go again inside your laravel application folder and run again npm install command, and it should work. Hope it helps.

After installing Node, you should restart your VScode and run npm install command.

After trying so many things (I’d already installed npm/Node), it was as simple as making sure I’d shut down VSCode and then reopening it. It worked in Terminal after then.

If you already installed npm globally on your system, and you are still getting the above error message by using VSCode terminal. Just close your VSCode application and reopen again, that should resolve the issue.

Источник

Оцените статью
Adblock
detector