Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke an npm script from a parent folder?

I created an express-based backend (in folder A) and a related react-based front-end project (in folder B) respectively. Now I put B inside A for the following benefits:

  1. I don't need to copy files from font-end build to server project anymore because A/server.js can serve files from A/B/build directly.

  2. No need to worry about the cross origin request errors.

  3. They look like one project and are easier to manage in GitHub. But can I run npm run buildjs from folder A, which actually runs npm run build in folder B? I guess it has much to do with the npm run-script usage.

like image 263
Shawn Chen Avatar asked Feb 23 '26 18:02

Shawn Chen


2 Answers

This can be done using --prefix <path>. From folder A:

npm run --prefix ./B build

You could add the following to A/package.json:

{
    ...
    "scripts": {
        "buildjs": "npm run --prefix ./B build"
    },
    ...
}
like image 91
binaryfunt Avatar answered Feb 25 '26 08:02

binaryfunt


I noticed that you can also go up using the --prefix. For example:

npm run --prefix ../ build:frontend

Only downside is that you can't pass arguments using the -- <args> syntax.

like image 20
Hans Avatar answered Feb 25 '26 09:02

Hans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!