npm script command to run a script command from another package.json

Npm

Npm Problem Overview


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.

Npm Solutions


Solution 1 - Npm

well it turns out to be quite simple:

"scripts": {
  test_projectA:"cd ../projectA && npm run-script test"
}

Solution 2 - Npm

You should use --prefix.

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

And for yarn:

yarn --cwd path/to/your/other/folder [command]

Solution 3 - Npm

I ended up using:

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

because this also works with yarn.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionepelegView Question on Stackoverflow
Solution 1 - NpmepelegView Answer on Stackoverflow
Solution 2 - NpmDominicView Answer on Stackoverflow
Solution 3 - NpmyellowsirView Answer on Stackoverflow