How to start http-server locally
Asked Answered
P

2

44

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.

Predictory answered 9/7, 2015 at 21:15 Comment(1)
npm install http-server, and in package.json, add: "scripts": {"start": "http-server"}, then run: npm start, browse: 127.0.0.1:8080Whangee
P
73

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
Pigheaded answered 10/7, 2015 at 12:24 Comment(4)
This works for me, but only when using `\` instead of '/'. ThanksTag
Could you please update it for those who use Windows command? There is no export. Thanks a lot!Corned
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.Pigheaded
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.)Mayapple
H
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" },

Hime answered 13/4, 2019 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.