65

I have two separate projects that use npm - so I have both :
some_base_folder/projectA/package.json and some_base_folder/projectB/package.json

Each of those files has a scripts section in it.

If I go to some_base_folder/projectA/ and run npm run-script test it executes the test command from the scripts section of some_base_folder/projectA/package.json as it should.

What can I put as the value of "scripts": {test_projectA:'????' in some_base_folder/projectB/package.json so that when I am in some_base_folder/projectB/ and I run npm run-script test_projectA it will be execute the test script of Project A?

I tried ../projectA/npm run-script test but it says:

'..' is not recognized as an internal or external command,
operable program or batch file.

I am running under windows 7 but would prefer a solution that would also work properly on linux.

3 Answers 3

89

well it turns out to be quite simple:

"scripts": {
  test_projectA:"cd ../projectA && npm run-script test"
}
4
  • seems like the log statements from my child process are not being displayed. Any idea on how to do that?
    – sramzan
    Commented Apr 10, 2019 at 0:01
  • you can use --loglevel verbose
    – yellowsir
    Commented Sep 22, 2019 at 8:46
  • See also @yellowsir’s answer for a cross-package-manager solution (works with yarn as well).
    – sylbru
    Commented Sep 10, 2020 at 8:50
  • simple and effective. Worked like a charm for me
    – francovici
    Commented Mar 15 at 18:03
66

You should use --prefix.

npm run [command] --prefix path/to/your/other/folder

And for yarn:

yarn --cwd path/to/your/other/folder [command]
2
  • 2
    OP is trying to run this from within package.json not from command line
    – Moe
    Commented May 7, 2021 at 11:30
  • 9
    @MohamedMoselhy there's nothing stopping you from putting those commands in package.json scripts section
    – nanobar
    Commented May 7, 2021 at 11:57
14

I ended up using:

"scripts": {
   "job": "cd ./sub && \"$npm_execpath\" run subjob",
   ...
}

because this also works with yarn.

3
  • 1
    This wound up being a significant improvement over the top-voted answer for me. With the top-voted answer I was getting some errors about conflicting bcrypt library versions (I think because I was using the NPM libraries from where I ran the command to run the command in the other folder ... instead of its own). By using the environment variable as shown in this answer it fixed that. You should accept this answer IMHO. Commented May 31, 2019 at 21:19
  • 1
    Yes! Thanks for this! I’m using yarn but I didn’t want to hardcode the yarn command into my package.json, works perfectly.
    – sylbru
    Commented Sep 10, 2020 at 8:49
  • sadly this no longer works as yarn will forward parameters "as is" so npm run foo -- -bar has a different meaning then yarn run foo -- -bar
    – yellowsir
    Commented Jan 26, 2021 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.