How to use npm start to open localhost

Viewed 11798

I have just finished making a website with html, css, js and wanted to open it with npm start. Granted, I know if I click the html, the webpage will open. But I would like to use npm. What do I need to install to npm and do I need to change the start to open the site locally? I have very little knowledge with this, any help is appreciated.

Edit: I already have node.js installed

1 Answers

To start a local web server that serves the index.html do the following steps:

  1. Navigate into the folder in the command line, where the index.html is located.
  2. Run npm init, it will ask some questions what configurations do you want to have in your package.json. If you are not sure yet, just go for the default values, you will be able to change them later.
  3. Run npm install http-server --save-dev to have http-server as development dependency, which is able to serve the index.html file.
  4. In package.json add to the scripts the start npm script for starting of the http-server: "scripts": {"start": "http-server"}.
  5. Run npm start from the command line, the server will be started by default on http://localhost:8080/

Of course it is worth to put the html, css and js files in a separate folder, to serve them from that folder check the parameters of http-server.

Related