http-server command not found
Asked Answered
H

4

15

I'm trying to run a basic http-server to test out some html files and I keep running into the same error.

I did sudo npm install -g http-server a bunch of times, but each time I try

simple git:(master) http-server

I keep getting this error:

zsh: command not found: http-server

I've tried other variations such as http-server / 8000, I've tried using different command syntax but nothing seems to be working.

my npm version is 5.5.1, my node version is 8.3.0, and my OS is Mac OSX Sierra v10.12.6

Please let me know if you can help or if you see anything I'm doing wrong that I'm just not noticing.

edit: tiny update I was able to get a server going with python but I'd really like to know why this wasn't working.

edit 2: problem solved thanks!

Hydroquinone answered 20/10, 2017 at 14:32 Comment(0)
A
23

You may not have your npm binaries in PATH.

Make sure that your npm binaries are in path by running echo $PATH. You should see, somewhere in the printed output, something like:

/home/bob/.npm-packages/bin:/usr/lobal/bin:/other/paths/that/contain/bins

/home/bob/.npm-packages/bin is the directory where my npm binaries are installed whenever I run npm -g install whatever.

If you don't see something like that, read Fixing npm permissions which will walk you through making sure that your environment is set up correctly. Option 2 explicitly talks about fixing PATH.

Another handy thing that I usually do is add all this to my .bashrc or .bashprofile which is in your home directory:

  • On macOS /Users/username/
  • On *nix: /home/username/

.bashrc

NPM_PACKAGES="${HOME}/.npm-packages"
PATH="$NPM_PACKAGES/bin:$PATH"

However, since it looks like you are using zshell, you'll have to use whatever convention they follow for rc files.


You can either fix that or, you can install http-server at a package level for your project and then start it through an npm command.

Run npm install --save-dev http-server

and put in your package.json:

{
    "scripts": {
        "start": "http-server ."
    }
}

and then run

npm start
Autochthon answered 20/10, 2017 at 14:35 Comment(7)
You can also use i instead of install and -D instead of --save-dev if you don't hate your fingers.Antependium
I just checked and I didn't see it, how do I fix this?Hydroquinone
@Kirk true dat. But I'm usually verbose with command switches in answers to avoid the innevitable: "What does the 'i' stand for?".Autochthon
@JordanForbes awesome, don't forget to accept the answer if that solved everything.Autochthon
@Autochthon The comment was more just to explain the option was there, not to suggest it as an improvement. I agree it doesn't make sense to only show the shortcut options.Antependium
PATH="$(npm bin -g):$PATH" is handy if your npm bin is tied to a version, e.g. /usr/local/Cellar/node/11.1.0/bin.Kubiak
using the package.json scripts tag was the only way I could get this to work. Much appreciated.Daniel
E
14
npm install http-server -g.

please install http-server globally and there after u can run the command

http-server -o
Enfilade answered 26/4, 2019 at 13:54 Comment(1)
I had to use sudo npm install http-server -g and then this worked. Thanks!Ofay
K
7

If you do sudo npm install -g and you don't have an npm prefix set, the file is probably installed in /usr/local/bin, but it could be installed more or less anywhere. Whatever directory it's being installed to is probably not in your $PATH, so you should figure out where it's installed and then update your $PATH to include it.

I would very strongly recommend you avoid avoid running sudo with npm. Instead, you can update your prefix in your npm configuration (or update manually in ~/.npmrc to something like prefix=~/.npm but it's up to you. Then you also have to make sure that ~/.npm/bin is in your path.

Even though global packages can be handy sometimes, if they are tied to particular projects I think it's better to just install it as part of the project and run it there:

cd /path/to/project/with/http-server
npm install http-server

# any of:
npx http-server
node_modules/.bin/http-server

Also if you have a package.json#scripts, you can reference any locally installed binary as in:

"scripts": {
  "server": "http-server"
}

and then use npm run server.

Karyokinesis answered 20/10, 2017 at 14:43 Comment(0)
K
1

If you are here because you want to test your Angular PWA project locally you need to install http-server package globally using npm install --global http-server and then only you can run http-server

Kerby answered 20/1, 2021 at 9:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.