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
Ethereum Difficulty Chart - ETH Difficulty
LoL MMR in Season 2022
Television Archive News Search Service
Www.craigslist Virginia
Yogabella Babysitter
Best Team In 2K23 Myteam
Craigslist Cars And Trucks For Sale By Owner Indianapolis
DENVER Überwachungskamera IOC-221, IP, WLAN, außen | 580950
Flights to Miami (MIA)
Call of Duty: NEXT Event Intel, How to Watch, and Tune In Rewards
Erskine Plus Portal
Olivia Ponton On Pride, Her Collection With AE & Accidentally Coming Out On TikTok
Mycarolinas Login
Hillside Funeral Home Washington Nc Obituaries
Indiana Immediate Care.webpay.md
2024 Non-Homestead Millage - Clarkston Community Schools
Cooking Fever Wiki
Insidekp.kp.org Hrconnect
Mineral Wells Independent School District
Does Breckie Hill Have An Only Fans – Repeat Replay
Best Forensic Pathology Careers + Salary Outlook | HealthGrad
Locate At&T Store Near Me
Voy Boards Miss America
Divina Rapsing
Fsga Golf
Rqi.1Stop
Little Rock Skipthegames
What Equals 16
14 Top-Rated Attractions & Things to Do in Medford, OR
Dailymotion
2487872771
Current Time In Maryland
Orange Pill 44 291
Solve 100000div3= | Microsoft Math Solver
Moses Lake Rv Show
Frostbite Blaster
Whitehall Preparatory And Fitness Academy Calendar
Dynavax Technologies Corp (DVAX)
5 Tips To Throw A Fun Halloween Party For Adults
Anya Banerjee Feet
Mars Petcare 2037 American Italian Way Columbia Sc
Captain Billy's Whiz Bang, Vol 1, No. 11, August, 1920&#10;America's Magazine of Wit, Humor and Filosophy
Nsav Investorshub
Jack In The Box Menu 2022
Suffix With Pent Crossword Clue
1Exquisitetaste
Courtney Roberson Rob Dyrdek
Hkx File Compatibility Check Skyrim/Sse
Rage Of Harrogath Bugged
Bank Of America Appointments Near Me
Oak Hill, Blue Owl Lead Record Finastra Private Credit Loan
Tyrone Unblocked Games Bitlife
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.