How to Use npm live-server

2016/03/032 min read
bookmark this
Responsive image

Table of Contents

  1. Introduction
  2. Installing live-server
  3. Running live-server
  4. Troubleshooting
  5. Conclusion

Introduction

NPM live-server is very useful when you want to set up a server to serve files during development, such as serving HTML/JavaScript/CSS files as a web server. To use it, you'll need to have npm installed first. On my development machine, I'm using Windows 7 with Node.js version v6.5.0 and npm version 3.10.3.

Installing live-server

After installing Node.js, type the following command to install the npm package live-server globally on your machine.

npm install -g live-server

The above command will install the npm module live-server globally on your machine. Once installed, you can use it in any directory folder on your PC.

Running live-server

Navigate to the folder you want to serve and type the following command:

live-server

Now, after you type the command, if the browser shows your folder contents, then you're good to go.

Troubleshooting

If the command line displays "live-server is not recognized as an internal or external command", then the live-server or Node.js execution path is probably not set up in your Windows environment variables.

To fix this, verify your System Properties → Environment Variables → Path Variable. Check to see if either the Node.js or live-server path is there. If it is there, then restart the command line and run it again — sometimes the command line needs a restart.

If the path is not there, you can reference the version that works on my computer. The following is the path in my Windows environment path:

C:\Program Files\nodejs\

The following is how I run live-server in my picture folder.

Conclusion

The npm live-server package is a simple and effective tool for serving files during development. Install it globally with npm install -g live-server, navigate to your project folder, and run live-server to get started. If you encounter path issues on Windows, make sure the Node.js path is added to your system environment variables.