NPM Serve - GeeksforGeeks (2024)

Last Updated : 14 May, 2024

Summarize

Comments

Improve

The command npm serve is not a built-in part of npm. In reality, it is just another way of calling the package serve that can be found on npm.

Serve is used for quickly setting up local servers to host static web projects that are in development. Basically, it turns the current working directory into a virtual server where you can open HTML, CSS, JavaScript files and other static assets in a browser.

Prerequisites:

Installation

There are two ways to install serve:

Global installation: This lets you use serve from any directory in your terminal. Just run the following command:

npm install --global serve

Local installation (project-specific): For the sake of keeping dependencies localized, only install serve in your project’s folder. For this open your terminal and navigate to the root directory of your project:

npm install serve

After the installation the development server can be initiated by running serve in the project directory, the directory where the HTML file located. Normally, serve uses port 5000 as default but to verify this you can check either the output or browser address bar.

Specifying a Directory:

You can designate the directory that will be served if your HTML file is located inside a subdirectory of your project-

serve my-folder/

Features of npm serve

  • Easy setup: npm install -g serve is all you need to do before serving your files
  • Customizable port: When you are working with multiple projects simultaneously then you can assign a unique custom port number to each one.
  • Zero-configuration: npm serve does not requires configuration files. It will use your current working directory as the static file root, you can also provide a path to another folder if needed.
  • HTTPS support: It even has SSL/TLS support built-in, so you can do everything over an encrypted tunnel (self-signed certificates are supported).

Example

In this example we will see the basic usage of serve.

Step 1: Create an HTML file named index.html in your project directory with some content, for example-

NPM Serve - GeeksforGeeks (1)

Folder Example

Example:

HTML
<!--index.html--><!DOCTYPE html><html><head> <title>My Local Project</title></head><body> <h1>Hello from my local server!</h1></body></html>

Step 2: Install serve only within your project’s directory

npm install serve

NPM Serve - GeeksforGeeks (2)

Step 3: Open your terminal in the project directory and run-

serve

NPM Serve - GeeksforGeeks (3)

Output:

NPM Serve - GeeksforGeeks (4)

Note: If you see this kind of error in terminal

NPM Serve - GeeksforGeeks (5)

For security reasons, PowerShell scripting is not allowed on your system this is what mean by this error message.

Steps to enable script execution on your system (Recommended for Personal Use)

Step 1: Open PowerShell as Administrator:

Search Windows Powershell, right click on it and choose to open Windows Powershell with run as administrator.

Since we are changing security setting so we need to run it as administrator.

Step 2: Set the Execution Policy:

Type the following command and press Enter:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

It sets the execution policy so that remote signed scripts (those with a digital signature from a trusted publisher) can run without being blocked. For confirmation if ask press Y and hit enter.

Step 3: Test npm serve Again:

Close any regular PowerShell windows (the ones not running as administrators), then open a new one.

Try running npm serve again – if it is signed, it should now work.



NPM Serve - GeeksforGeeks (7)

Improve

Please Login to comment...

NPM Serve - GeeksforGeeks (2024)
Top Articles
Définitions : désoler, être désolé, se désoler - Dictionnaire de français Larousse
What is DuckDuckGo VPN?
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Pearson Correlation Coefficient
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Margart Wisoky

Last Updated:

Views: 6516

Rating: 4.8 / 5 (58 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Margart Wisoky

Birthday: 1993-05-13

Address: 2113 Abernathy Knoll, New Tamerafurt, CT 66893-2169

Phone: +25815234346805

Job: Central Developer

Hobby: Machining, Pottery, Rafting, Cosplaying, Jogging, Taekwondo, Scouting

Introduction: My name is Margart Wisoky, I am a gorgeous, shiny, successful, beautiful, adventurous, excited, pleasant person who loves writing and wants to share my knowledge and understanding with you.