471

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?

Liam
  • 27,717
  • 28
  • 128
  • 190
Blaise
  • 21,314
  • 28
  • 108
  • 169

6 Answers6

783

This one-liner should work:

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

Corresponding doc

Philipp Kief
  • 8,172
  • 5
  • 33
  • 43
Amaury Liet
  • 10,196
  • 3
  • 18
  • 24
196

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.

Amit Baranes
  • 7,398
  • 2
  • 31
  • 53
itiic
  • 3,284
  • 4
  • 20
  • 31
  • 7
    This was the most helpful answer to me, as it documents that you need to pass the command *after* the prefix flag and the path. The example of "npm start --prefix path/to/your/app" is an edge case due to the way that npm lets you use "npm start" or "npm test" as a short hand for "npm run start/test". That example doesn't help if someone needs to run a command other than start or test. – LexJacobs Jan 09 '21 at 20:34
  • Can you tell me, suppose `server.js` is not in the `root` folder, how can I map to the `server.js` path to root `package.json` file, so that when I run command from `root` folder, like `npm start` it will execute `server.js` no matter where it is located? – Sabbir Sobhani Mar 21 '22 at 04:54
  • To clarify, should this look like `npm run --prefix /path/to/project ${start}` or `npm run --prefix /path/to/project start` ? – Caleb Jay Aug 31 '23 at 08:40
  • Please select option B – itiic Aug 31 '23 at 09:41
31

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 
Or Duan
  • 13,142
  • 6
  • 60
  • 65
24

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

& inside package.json add the following script

"scripts": {
   "preinstall":"cd $(pwd)"
}
Rafael17
  • 273
  • 2
  • 2
19

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.

babca
  • 738
  • 7
  • 11
  • 6
    Please when using "-1" button, add a comment so visitors have a clue "why it's not ok"... – Cyril CHAPON Mar 07 '19 at 13:57
  • 13
    I didn't downvote. But I believe that it's downvoted because of the side effects. The solution is not pure, it changes the current directory. – Vlad DX Mar 07 '19 at 14:52
  • 1
    This is actually a very common solution, the only improvement that would be done is to add parenthesis to avoid mentioned side effects. However solutions above are better in my opition, as they purely rely on NPM. – Rafał Wrzeszcz Apr 05 '19 at 16:14
  • You can add `cd -` on the end to remove the side effect. – babca Apr 07 '19 at 15:44
  • 3
    @babca unless `npm start` fails... you need to break that into multiple statements – Andreas Jan 30 '20 at 08:54
4

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
zangw
  • 43,869
  • 19
  • 177
  • 214