42

I cloned angular seed which is using node http-server and it is working perfectly using following configuration.

Command : npm start (from root of project)

Following configuration in package.json file:

"start": "http-server -a localhost -p 8000 -c-1",
Link to file

However I'm unable to start this server directly. eg: from root of the project, none of these commands work:

>  angular-seed npm http-server  
>  angular-seed node http-server
>  angular-seed http-server

Shouldn't this(http-server) be available here(root, where it got installed from)? Could someone please explain me how it is working and how I can use it directly from root of the project.

I'm sure it will work fine if I install it globally but I'm not interested in that.

JS-JMS-WEB
  • 2,555
  • 3
  • 17
  • 26

2 Answers2

71

When you're running npm install in the project's root, it installs all of the npm dependencies into the project's node_modules directory.

If you take a look at the project's node_modules directory, you should see a directory called http-server, which holds the http-server package, and a .bin folder, which holds the executable binaries from the installed dependencies. The .bin directory should have the http-server binary (or a link to it).

So in your case, you should be able to start the http-server by running the following from your project's root directory (instead of npm start):

./node_modules/.bin/http-server -a localhost -p 8000 -c-1

This should have the same effect as running npm start.

If you're running a Bash shell, you can simplify this by adding the ./node_modules/.bin folder to your $PATH environment variable:

export PATH=./node_modules/.bin:$PATH

This will put this folder on your path, and you should be able to simply run

http-server -a localhost -p 8000 -c-1
nwinkler
  • 52,665
  • 21
  • 154
  • 168
  • 2
    This works for me, but only when using `\\` instead of '/'. Thanks – e-shfiyut Jun 14 '18 at 13:45
  • 3
    Could you please update it for those who use Windows command? There is no export. Thanks a lot! – Jonalcaide Jun 11 '19 at 20:21
  • 2
    Sorry, can't do that - I don't use Windows. I'm sure that you can find a way to set the PATH variable in Windows, though. Please feel free to update the answer with Windows instructions when you have found out how to do this. – nwinkler Jun 12 '19 at 10:58
  • Windows "Command Shell (cmd)" can use `SET PATH=.\node_modules\.bin;%PATH%` and Windows/macOS/Linux "Powershell (pwsh)" can use `$env:PATH=$("./node_modules/.bin" + [System.IO.Path]::SeparatorChar + "$env:PATH")` HTH! (PS. worth noting that pwsh is the preferred shell on Windows platform, today.) – Shaun Wilson Apr 11 '21 at 22:26
16

To start server locally paste the below code in package.json and run npm start in command line.

"scripts": { "start": "http-server -c-1 -p 8081" },

shubham kapoor
  • 589
  • 6
  • 16