6

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

Danzz
  • 127
  • 1
  • 1
  • 6

2 Answers2

10

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.

Milan Tenk
  • 2,415
  • 1
  • 17
  • 24
0
  1. Navigate into the current folder in the command line, where the index.html is located.

  2. Run npm install to install all the dependencies (if any) for the project.

  3. Run npm init, for the following next questions, if you are not sure yet, just go for the default values, press enter, you will be able to change them later if needed.

  4. Run npm install http-server --save-dev to have http-server as development dependency, which is able to serve the index.html file.

  5. In package.json add to the scripts the start npm script for starting of the http-server: "scripts": {"start": "http-server"}.

  6. Run npm run build to save the build and notice how a index.pack.js file is created.

  7. Finally npm start to start the app on the localhost, (default http://localhost:8080/).

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Subhashis
  • 1
  • 3