最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to invoke an npm script from a parent folder? - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Apr 21, 2020 at 10:28 binaryfunt 7,1275 gold badges44 silver badges65 bronze badges asked Jun 6, 2017 at 8:19 Shawn ChenShawn Chen 1,0972 gold badges14 silver badges26 bronze badges 2
  • is the front-end package also a service, or just static files? – Jiang YD Commented Jun 6, 2017 at 9:16
  • They are static files – Shawn Chen Commented Jun 6, 2017 at 10:09
Add a comment  | 

3 Answers 3

Reset to default 14

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"
    },
    ...
}

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.

  1. copy is not a problem, just make it be automatic. Introduce a deployment step is not bad.
  2. there will not be any cross origin problem because you are serving front-end as static files. or at least the cross origin problem should do no matter with it
  3. people first. split or not split projects, depends on developers
发布评论

评论列表(0)

  1. 暂无评论