The serverless command is not found in the shell
serverless: command not foundShort 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.
Fix it fast
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"2Match what you see, then jump to its fix
- 1
npm ls -g does not list serverlessThe Framework is not installed globallyFix - 2
npm ls -g lists serverless, but the shell does not find itnpm's global bin folder is not on PATHFix - 3
npm 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
- 1
What does the error look like?
serverless: command not foundbash: 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 serverless2Fix
Install it globally. If permission errors stop you, see the next cause.
npm i serverless -g3Verify
The command prints the Framework version.
serverless --versionConfirmed by serverless.com
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 ~/.profile3Verify
The shell resolves the command.
command -v serverlessConfirmed by docs.npmjs.com
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 prefix2Fix
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 -g3Verify
The command resolves without sudo.
command -v serverless && serverless --versionConfirmed by docs.npmjs.com
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
yarn command not found, or yarn global binaries not found
zsh: command not found: yarn
The shell cannot find the yarn binary or a tool installed with yarn global.
4 causes, each with a fixBack end
The nest command is not found in the terminal
bash: nest: command not found
The shell cannot find a nest executable on its PATH.
3 causes, each with a fixBack 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 fixBack end
node and npm vanish or are not found when nvm is installed
nvm current prints system in a new shell
nvm adds node and npm to PATH only for the version it has activated.
4 causes, each with a fixBack 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 fixBack end
Application Load Balancer returns 502 for a Node.js target
HTTPCode_ELB_502_Count
An ALB returns 502 when the target gives it something it cannot use: a refused or reset connection, a connection closed mid-request, a bad response, or a header over 32 K.
4 causes, each with a fix