Blaise
Blaise

Reputation: 22212

How can I get npm start at a different directory?

I usually cd into the app directory and then run npm start.

It is my feeling that there ought to be some way to run npm start with a path parameter. But, the npm start documentation contains no such feature.

I tried myself only to find npm start ./myapp does not work. Is there a way to do that?

Upvotes: 512

Views: 364285

Answers (8)

Moritz Oster
Moritz Oster

Reputation: 39

For pnpm the following works:

pnpm --dir path/to/your/app start

or:

pnpm -C path/to/your/app start

Upvotes: 0

Noam Manos
Noam Manos

Reputation: 16971

Although npm --prefix is probably the OOTB way, you can also push the project path directory before calling a command that requires that path:

pushd ${project_path}; npm run your:app

Upvotes: 0

Amaury Liet
Amaury Liet

Reputation: 11173

This one-liner should work:

npm start --prefix path/to/your/app

Corresponding doc

Upvotes: 848

itiic
itiic

Reputation: 3712

Below Command where project is a folder which contains package.json file

npm run --prefix project ${COMMAND}

is working as well. Useful in Docker based applications.

Upvotes: 229

babca
babca

Reputation: 776

This one-liner should work too:

(cd /path/to/your/app && npm start)

Note that the current directory will be changed to /path/to/your/app after executing this command. To preserve the working directory:

(cd /path/to/your/app && npm start && cd -)

I used this solution because a program configuration file I was editing back then didn't support specifying command line arguments.

Upvotes: 22

Or Duan
Or Duan

Reputation: 13810

I came here from google so it might be relevant to others: for yarn you could use:

yarn --cwd /path/to/your/app run start 

Upvotes: 30

Rafael17
Rafael17

Reputation: 281

npm start --prefix path/to/your/app

& inside package.json add the following script

"scripts": {
   "preinstall":"cd $(pwd)"
}

Upvotes: 24

zangw
zangw

Reputation: 48366

Per this npm issue list, one work around could be done through npm config

name: 'foo'
config: { path: "baz" },
scripts: { start: "node ./$npm_package_config_path" }

Under windows, the scripts could be { start: "node ./%npm_package_config_path%" }

Then run the command line as below

npm start --foo:path=myapp

Upvotes: 4

Related Questions