Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

The serverless command is not found in the shell

Errorserverless: command not found

Short answer

The shell can only run serverless if it is installed globally and the folder npm links global commands into is on PATH. Install with npm i serverless -g, then compare npm's prefix bin folder with your PATH. If it is missing, add it to PATH.

  • Applies to Node.js 22, npm 10, Serverless Framework 4, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check whether npm has the package globally and where it would put the command. Compare the folder with your PATH.

    npm ls -g serverless
    echo "$(npm prefix -g)/bin"
    echo "$PATH"
  2. 2Match what you see, then jump to its fix

    1. 1npm ls -g does not list serverlessThe Framework is not installed globallyFix
    2. 2npm ls -g lists serverless, but the shell does not find itnpm's global bin folder is not on PATHFix
    3. 3npm i -g fails with EACCES, or it was run once with sudo into a folder that is not on PATHA global install needs root, so it was never done or went elsewhereFix

What does the error look like?

  • serverless: command not found
  • bash: serverless: command not found

Cause 1 of 3

The Framework is not installed globally

1Check

The Serverless getting-started guide says to install it with the global flag, and needs Node.js first.

node -v
npm ls -g serverless

2Fix

Install it globally. If permission errors stop you, see the next cause.

npm i serverless -g

3Verify

The command prints the Framework version.

serverless --version

Confirmed by serverless.com

Did this fix it?

Cause 2 of 3

npm's global bin folder is not on PATH

1Check

npm's documentation says global executables are linked into {prefix}/bin. If that folder is not in PATH, the shell cannot see them.

ls "$(npm prefix -g)/bin"
echo "$PATH" | tr ':' '\n' | grep -n "$(npm prefix -g)/bin"

2Fix

Add the folder to PATH in your shell profile, then reload it.

echo 'export PATH="$(npm prefix -g)/bin:$PATH"' >> ~/.profile
source ~/.profile

3Verify

The shell resolves the command.

command -v serverless

Confirmed by docs.npmjs.com

Did this fix it?

Cause 3 of 3

A global install needs root, so it was never done or went elsewhere

1Check

npm's guide recommends a Node version manager as the best way to avoid global permission problems, and describes changing the default directory as the manual option.

npm config get prefix

2Fix

Use a version manager, or set a prefix in your home folder as npm's guide describes and add its bin folder to PATH.

npm config set prefix ~/.local
echo 'PATH=~/.local/bin:$PATH' >> ~/.profile
source ~/.profile
npm i serverless -g

3Verify

The command resolves without sudo.

command -v serverless && serverless --version

Confirmed by docs.npmjs.com

Did this fix it?

How do I stop it coming back?

  • Install Node.js with a version manager, so global packages live in your home folder.
  • Put the install and PATH steps in the project README or the dev container setup.
  • In CI, install the Framework in the job and print its version as the first step.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

npm install worked but the command is not found

bash: vite: command not found

npm puts a local install's command in the project's node_modules/.bin, which is not on your shell PATH, so run it with npx or from an npm script.

3 causes, each with a fix

Back end

npm or a script fails in WSL with /usr/bin/env: 'bash\r'

/usr/bin/env: 'bash\r': No such file or directory

The \r in the message is a Windows line ending at the end of a script's first line, so Linux looks for a program called bash followed by a carriage return.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.