Run npm script from another directory (2024)

👋 FYI, this note is over 6 months old. Some of the content may be out of date.

On this page

npm Jump to heading

To run an npm script from another directory, use --prefix:

npm --prefix <path> run <command>

yarn Jump to heading

To run a yarn script from another directory, use --cwd:

yarn --cwd <path> <command>

Example Jump to heading

If you have a package.json in the example directory:

{
"scripts": {
"start": "echo hello world"
}
}

In the following directory:

.├─ example│ ├─ package.json├─ package.json

1 directory, 1 file

Then to run script start from your working directory:

npm --prefix ./example run start

yarn --cwd ./example start

{
"scripts": {
"start": "npm --prefix ./example run start"
}
}

← Back home

Run npm script from another directory (2024)

FAQs

How to run npm from another directory? ›

To run an npm script from another directory, use --prefix :
  1. npm --prefix <path> run <command>
  2. yarn --cwd <path> <command>
  3. { "scripts": { "command": "echo hello world" } }
  4. npm --prefix ./path/ run command yarn --cwd ./path/ command.
Oct 23, 2020

How do I run node from a different folder? ›

Run the command in a different folder #

You can use the option --prefix <path/to/folder> to run NPM command in a particular folder. It works like a cross-platform cd <path/to/folder>; npm ... combination. "start": "node ."

How to run npm start in project directory? ›

To start an npm project, you can run npm init in the directory where you want to initiate the project. You will be prompted with some prompts about the name of the project and some other stuff that you can just skip by pressing enter . After that a package. json file will be created.

How to run npm install in directory? ›

npm install (in a package directory, no arguments): Install the dependencies to the local node_modules folder. In global mode (ie, with -g or --global appended to the command), it installs the current package context (ie, the current working directory) as a global package.

How to run npm in command line? ›

  1. npm run-script <command> [-- <args>] aliases: run, rum, urn.
  2. npm run test -- --grep="pattern"
  3. "scripts": {"test": "tap test/*.js"}
  4. "scripts": {"test": "node_modules/.bin/tap test/*.js"}
  5. . +-- package.json. `-- packages. +-- a. | `-- package.json. +-- b. | `-- package.json. `-- c. ...
  6. { "workspaces": [ "./packages/*" ] }

How do I run a command as a script in npm? ›

To define an NPM script, set its name and write the script under the 'scripts' property of your package. json file: To execute your Script, use the 'npm run <NAME-OF-YOUR-SCRIPT>' command. Some predefined aliases convert to npm run, like npm test or package json npm start, you can use them interchangeably.

How do I change the working directory in node? ›

chdir() Method. Parameters: This method accepts single parameter as mentioned above and described below: directory: It is required parameter that specifies the path to the directory to which current working directory to be changed.

How do I give a directory path in node? ›

path.dirname(path) #

Passing a non-string as the path argument will throw now. The path.dirname() method returns the directory name of a path , similar to the Unix dirname command.

How to install node js in specific folder? ›

Running a Node App

First, create a folder for your app. For my setup, I have a folder named projects in my Desktop folder. Inside the projects folder, I created a subfolder named my-node-app . Inside the my-node-app folder, create a file called hello-world.

What is the difference between npm start and npm run start? ›

npm start is the short form for npm run start . So, its one and the same thing. One interesting thing to note is, If the scripts object does not have a "start" property in package.

How to initialize npm in a directory? ›

You need to open a terminal and navigate to the directory you want to initialize. Then you need to type npm init . This will help you set up the project correctly which seems to be what you are trying to do manually. From there you can run npm install date-fns --save .

How to run npm on local? ›

Example: Let the local-dir is the local directory and project-dir is the project directory and local_module is the local module package you want to install, first go to the local-dir and type npm link and next go to the project directory and type npm link <local_module> this will link your local module to your project.

How to set npm home directory? ›

Option 2: Change npm's default directory to another directory
  1. Make a directory for global installations: mkdir ~/.npm-global.
  2. Configure npm to use the new directory path: npm config set prefix '~/.npm-global'
  3. Open or create a ~/.profile file and add this line: export PATH=~/.npm-global/bin:$PATH.

How to run a node project locally? ›

Running a Node. js Server Locally
  1. Step 1: Create a Simple Node. js Server. Initialize a Node. ...
  2. Step 2: Run the Server. Start the Server: In your terminal, run node server. js . ...
  3. Step 3: Stopping the Server. Stop the Server: To stop the server, simply press Ctrl + C in your terminal.
Feb 5, 2024

How to install and run npm packages? ›

To install a package, npm uses the following algorithm:
  1. load the existing node_modules tree from disk.
  2. clone the tree.
  3. fetch the package.json and assorted metadata and add it to the clone.
  4. walk the clone and add any missing dependencies.
  5. dependencies will be added as close to the top as is possible.
Sep 22, 2020

How to change npm installation directory? ›

Option 2: Change npm's default directory to another directory
  1. Make a directory for global installations: mkdir ~/.npm-global.
  2. Configure npm to use the new directory path: npm config set prefix '~/.npm-global'
  3. Open or create a ~/.profile file and add this line: export PATH=~/.npm-global/bin:$PATH.

How do I change the path in npm? ›

To set the PATH for node. js and npm follow these steps:
  1. Step 2: Search for environment variables in the global search.
  2. Step 3: Click Environment Variables.
  3. Step 4: On System Variables, select Path and click edit.
  4. Step 6: Reload your terminal window and now it should be fixed.
Apr 4, 2024

How do I run a program from another directory in command prompt? ›

To run a program from any folder, use "cd" to enter the folder that contains the program file first. Once you're in the folder, type "start programname.exe," replacing "programname.exe" with the full name of your program file.

How to run npm run dev on another port? ›

You can set port number using below ways:
  1. package.json. In the scripts section of package. json, there will be a command for dev. Usually it will be "dev": "next" . ...
  2. specify port number in run command. While running your application, specify the port number. You can use the command like below: npm run dev -- -p 3005.
Sep 3, 2020

Top Articles
You don’t need a smartphone to bank using Cellphone Banking
Article: Non-negotiable (straight), negotiable ('to order') and bearer bills of lading
Maxtrack Live
Kevin Cox Picks
7 Verification of Employment Letter Templates - HR University
Ups Dropoff Location Near Me
Week 2 Defense (DEF) Streamers, Starters & Rankings: 2024 Fantasy Tiers, Rankings
Restaurer Triple Vitrage
Lamb Funeral Home Obituaries Columbus Ga
Kaydengodly
New Slayer Boss - The Araxyte
America Cuevas Desnuda
Kristine Leahy Spouse
Jonathan Freeman : "Double homicide in Rowan County leads to arrest" - Bgrnd Search
Walgreens On Nacogdoches And O'connor
What Was D-Day Weegy
Housework 2 Jab
How Much Is Tj Maxx Starting Pay
Luna Lola: The Moon Wolf book by Park Kara
Google Flights Missoula
Marine Forecast Sandy Hook To Manasquan Inlet
Universal Stone Llc - Slab Warehouse & Fabrication
Laveen Modern Dentistry And Orthodontics Laveen Village Az
‘The Boogeyman’ Review: A Minor But Effectively Nerve-Jangling Stephen King Adaptation
Craigslist St. Cloud Minnesota
Ontdek Pearson support voor digitaal testen en scoren
Pokémon Unbound Starters
TJ Maxx‘s Top 12 Competitors: An Expert Analysis - Marketing Scoop
Craigslist Boerne Tx
Lawrence Ks Police Scanner
The Bold and the Beautiful
Craigslist Central Il
Mg Char Grill
Kokomo Mugshots Busted
Lowell Car Accident Lawyer Kiley Law Group
Uhaul Park Merced
Jennifer Reimold Ex Husband Scott Porter
Agematch Com Member Login
Asian Grocery Williamsburg Va
42 Manufacturing jobs in Grayling
Daily Jail Count - Harrison County Sheriff's Office - Mississippi
8005607994
Walgreens Agrees to Pay $106.8M to Resolve Allegations It Billed the Government for Prescriptions Never Dispensed
Red Dead Redemption 2 Legendary Fish Locations Guide (“A Fisher of Fish”)
Final Fantasy 7 Remake Nexus
Todd Gutner Salary
Sea Guini Dress Code
Graduation Requirements
What your eye doctor knows about your health
Marion City Wide Garage Sale 2023
Shad Base Elevator
Latest Posts
Article information

Author: Aracelis Kilback

Last Updated:

Views: 6221

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Aracelis Kilback

Birthday: 1994-11-22

Address: Apt. 895 30151 Green Plain, Lake Mariela, RI 98141

Phone: +5992291857476

Job: Legal Officer

Hobby: LARPing, role-playing games, Slacklining, Reading, Inline skating, Brazilian jiu-jitsu, Dance

Introduction: My name is Aracelis Kilback, I am a nice, gentle, agreeable, joyous, attractive, combative, gifted person who loves writing and wants to share my knowledge and understanding with you.