NODE使用CMD命令

福尔摩斯对我笑 2019-02-20 43188 次 3688条

 

今天在做一个Electron项目时,需要用到CMD命令,查看了下文档,发现可以用nodejs原生的child_proccess(子进程)去执行命令。

查看了下官网文档,进行了初试:

var exec = require('child_process').exec;
function  execute(){

    exec('cd node_server',function(error, stdout, stderr) {
            if(error){
                console.log(error);
            }
            else{
                console.log("成功");
            }
        });

}
execute();

果然,不出所料,成功运行了,不过这时候贪心的我,仍不满足,我想执行两条cmd命令。

因为我想直接运行我的node项目,于是我又进行了尝试

var exec = require('child_process').exec;
function  execute(){

    exec('cd node_server; node main',function(error, stdout, stderr) {
            if(error){
                console.log(error);
            }
            else{
                console.log("成功");
            }
        });

}
execute();

果然,是没办法直接运行两条命令的,后来我又尝试分开运行,在回调里继续请求:

var exec = require('child_process').exec;
var exec = require('child_process').exec;
function  execute(){

    exec('cd node_server',function(error, stdout, stderr) {
            if(error){
                console.log(error);
            }
            else{
                console.log("成功");

                exec('node main',function(error, stdout, stderr) {
                    if(error){
                        console.log(error);
                    }
                    else{
                        console.log("成功");
                        
                    }
                });

            }
        });

}
execute();

不幸的是,仍然不可行,查了相关资料,说是因为异步的原因。

于是,倔强的我去仔细查看了下文档,发现exec中的cwd参数:

cwd 子进程的当前工作目录。默认值: null。

想了想如果我将目录定位到我的node项目里,那不就可以变相的解决这个问题了嘛。

对,你猜的没错,就这样搞定了。

我直接将目录定位到了项目目录,再执行cmd命令,便可以实现该功能。

var exec = require('child_process').exec;
function  execute(){

    exec('npm run production', {cwd: process.cwd()+'\\node_server'},function(error, stdout, stderr) {
            if(error){
                console.log(error);
            }
            else{
                console.log("成功");
            }
        });

}
execute();

安利下NODE的官方文档,写的还是非常不错的。http://nodejs.cn/api/child_process.html#child_process_child_process_exec_command_options_callback

标签:

非特殊说明,本博所有文章均为博主原创。

头像也是LOGO
未来博客 博主大人
到了这个年纪,不应该再彷徨!

发表评论



 


已有 3688 条评论 1


  1. jordan shoes

    I just wanted to write down a small remark to be able to thank you for some of the remarkable concepts you are sharing at this site. My long internet search has at the end of the day been recognized with high-quality content to go over with my pals. I ‘d state that that most of us website visitors are extremely endowed to be in a really good place with so many perfect professionals with insightful secrets. I feel somewhat privileged to have used your entire website page and look forward to many more brilliant moments reading here. Thank you once again for a lot of things.

    回复 2019年10月29日
  2. birkin bag

    I want to express appreciation to this writer just for bailing me out of this particular setting. Because of browsing throughout the internet and getting views which are not pleasant, I believed my life was done. Being alive devoid of the answers to the problems you’ve solved by way of your main short article is a serious case, as well as the kind which may have in a wrong way damaged my entire career if I hadn’t noticed your website. Your good training and kindness in taking care of all the stuff was useful. I don’t know what I would have done if I had not come across such a stuff like this. I can also at this time relish my future. Thanks very much for the reliable and amazing help. I will not hesitate to endorse your blog post to anyone who should get guidance about this issue.

    回复 2019年10月29日
  3. canada goose

    I’m commenting to make you be aware of of the terrific experience my wife’s girl undergone using your blog. She even learned many details, which included what it is like to possess an incredible helping style to get folks really easily know certain multifaceted things. You actually exceeded my expected results. Thanks for presenting these powerful, trusted, revealing as well as cool thoughts on the topic to Jane.

    回复 2019年10月28日
  4. golden goose outlet

    Thank you for all your hard work on this blog. My mum loves making time for investigations and it’s easy to understand why. My spouse and i hear all regarding the compelling means you convey good tips on your web blog and even foster response from visitors about this content so my child is actually learning a lot. Take advantage of the remaining portion of the new year. You are always performing a really great job.

    回复 2019年10月28日
  5. nike shox

    I precisely had to say thanks once again. I am not sure what I might have followed in the absence of these opinions provided by you over my topic. It became an absolute troublesome circumstance in my circumstances, but taking a look at the very expert tactic you resolved it took me to jump with joy. I am just grateful for this help and in addition hope that you really know what an amazing job you happen to be getting into teaching many people all through your blog post. Probably you haven’t come across any of us.

    回复 2019年10月28日
  6. louboutin shoes

    I intended to send you one bit of note in order to thank you very much again on your beautiful methods you’ve contributed on this site. It’s simply particularly generous with people like you giving without restraint exactly what most of us would have offered as an e book to help make some cash for their own end, mostly considering that you might well have tried it if you considered necessary. These smart ideas in addition acted to provide a great way to fully grasp other people online have similar zeal similar to my personal own to realize good deal more around this condition. I am certain there are millions of more fun sessions up front for individuals that scan through your blog.

    回复 2019年10月27日
  7. lebron shoes

    I simply wanted to develop a comment in order to express gratitude to you for all the precious ideas you are giving out on this site. My prolonged internet search has finally been recognized with pleasant strategies to talk about with my classmates and friends. I ‘d admit that many of us readers actually are undoubtedly blessed to be in a remarkable community with so many marvellous professionals with interesting ideas. I feel very much fortunate to have encountered the website page and look forward to so many more fun times reading here. Thanks once more for all the details.

    回复 2019年10月27日
  8. yeezy shoes

    I wanted to write a brief message in order to thank you for all of the nice guidelines you are giving here. My rather long internet research has finally been honored with high-quality tips to write about with my colleagues. I would mention that most of us website visitors actually are undeniably lucky to exist in a fantastic website with many lovely individuals with good tips. I feel somewhat blessed to have used your website and look forward to so many more enjoyable moments reading here. Thanks a lot once again for all the details.

    回复 2019年10月27日
  9. hermes online

    I intended to create you this very little note so as to give thanks the moment again on your superb ideas you’ve discussed above. It has been so incredibly open-handed of you to make openly all that a few people might have advertised for an ebook to earn some money for themselves, principally since you could possibly have tried it if you wanted. Those points additionally worked to provide a easy way to be sure that someone else have the identical dream just as my very own to understand great deal more with reference to this matter. I am certain there are lots of more enjoyable occasions ahead for those who view your blog.

    回复 2019年10月26日
  10. golden goose sneakers

    I am glad for commenting to let you be aware of what a outstanding experience my cousin’s girl enjoyed studying your webblog. She picked up numerous pieces, not to mention how it is like to possess an awesome helping mood to make other folks just understand a variety of hard to do things. You undoubtedly surpassed readers’ expected results. I appreciate you for supplying those valuable, trustworthy, educational as well as easy tips about the topic to Sandra.

    回复 2019年10月26日
  11. air jordan shoes

    A lot of thanks for all your hard work on this website. Betty enjoys going through research and it is easy to understand why. A number of us learn all concerning the dynamic manner you give very important guidance through your web blog and in addition foster contribution from some other people on that area and our simple princess is in fact being taught a whole lot. Take advantage of the remaining portion of the new year. You are carrying out a remarkable job.

    回复 2019年10月26日
  12. off white clothing

    I actually wanted to compose a simple remark so as to thank you for all the splendid suggestions you are sharing on this website. My considerable internet look up has at the end of the day been paid with useful tips to talk about with my company. I would suppose that we site visitors actually are definitely blessed to exist in a great network with many marvellous people with good methods. I feel rather fortunate to have encountered your entire website page and look forward to plenty of more brilliant moments reading here. Thanks a lot once more for a lot of things.

    回复 2019年10月26日
  13. nhl jerseys

    I as well as my friends have already been reading through the best items on your site and then immediately came up with an awful suspicion I never expressed respect to the website owner for those tips. All of the men became as a consequence warmed to see them and now have without a doubt been tapping into these things. Appreciate your truly being so kind and for having such ideal guides most people are really desirous to be aware of. My honest apologies for not expressing gratitude to sooner.

    回复 2019年10月25日
  14. off white x nike

    I enjoy you because of every one of your hard work on this web site. My mum loves going through internet research and it is simple to grasp why. We know all of the powerful way you produce precious secrets through your blog and even improve response from other ones on this area of interest so our simple princess is without question understanding a lot of things. Take pleasure in the remaining portion of the year. You’re conducting a remarkable job.

    回复 2019年10月25日
  15. jordans

    I wish to show thanks to this writer just for rescuing me from this particular condition. Right after checking throughout the world-wide-web and obtaining strategies that were not productive, I assumed my entire life was gone. Being alive minus the solutions to the issues you have sorted out by means of your good review is a crucial case, as well as the ones that could have negatively affected my career if I hadn’t encountered your blog post. Your primary expertise and kindness in playing with all things was very useful. I am not sure what I would’ve done if I had not come across such a step like this. I can at this point look ahead to my future. Thanks for your time so much for this skilled and results-oriented guide. I will not be reluctant to recommend the sites to any person who needs to have assistance on this area.

    回复 2019年10月25日
  16. nike vapormax

    Thanks so much for providing individuals with remarkably remarkable opportunity to read articles and blog posts from this blog. It really is so nice plus full of a lot of fun for me personally and my office co-workers to visit your website at the least thrice in 7 days to read the fresh things you have. And lastly, I am just actually impressed for the very good secrets served by you. Some two facts in this post are essentially the most effective I have had.

    回复 2019年10月25日
  17. curry 6

    I simply wished to appreciate you all over again. I’m not certain what I would’ve achieved in the absence of these creative ideas provided by you about my topic. It truly was the troublesome situation in my circumstances, nevertheless seeing your professional technique you processed that forced me to leap for happiness. Now i am grateful for this work and as well , expect you know what a great job that you’re putting in teaching men and women thru a blog. Most likely you haven’t got to know all of us.

    回复 2019年10月24日
  18. nfl jerseys

    My husband and i got so peaceful Louis could finish off his basic research via the ideas he acquired through your weblog. It’s not at all simplistic to just choose to be giving freely techniques that many people could have been selling. And we also take into account we now have the writer to be grateful to for that. The main explanations you have made, the easy site menu, the friendships your site aid to instill – it is all remarkable, and it’s leading our son in addition to the family reckon that that idea is interesting, and that’s wonderfully important. Many thanks for everything!

    回复 2019年10月24日
  19. coach outlet sale

    I have to express some appreciation to the writer for bailing me out of this problem. Because of looking out through the online world and obtaining solutions that were not beneficial, I believed my entire life was well over. Being alive without the presence of solutions to the issues you’ve sorted out all through your good write-up is a crucial case, as well as those that could have badly damaged my entire career if I hadn’t noticed your web blog. Your actual capability and kindness in playing with the whole thing was tremendous. I don’t know what I would’ve done if I hadn’t come upon such a subject like this. I can now relish my future. Thanks very much for your reliable and results-oriented help. I will not hesitate to endorse your web blog to any individual who desires tips on this subject matter.

    回复 2019年10月24日
  20. air jordan

    I am also commenting to let you know what a fabulous experience my girl gained reading through the blog. She figured out many issues, not to mention what it’s like to possess an awesome coaching nature to make other individuals just learn about selected specialized topics. You truly surpassed our desires. I appreciate you for providing the practical, trustworthy, edifying and as well as fun guidance on your topic to Mary.

    回复 2019年10月24日
  21. golden goose outlet

    Thanks so much for providing individuals with a very marvellous possiblity to check tips from this blog. It’s usually very superb and also packed with a lot of fun for me personally and my office co-workers to visit your blog particularly three times weekly to learn the new things you will have. And indeed, I am also at all times contented considering the effective points you give. Certain 2 tips in this post are basically the most beneficial I have had.

    回复 2019年10月23日
  22. air max 97

    I wish to express my thanks to the writer just for bailing me out of such a crisis. Right after looking through the the web and seeing things which are not powerful, I was thinking my life was well over. Being alive without the solutions to the problems you have sorted out as a result of your review is a serious case, as well as ones that could have in a negative way damaged my entire career if I hadn’t come across your website. Your actual expertise and kindness in controlling all the details was useful. I’m not sure what I would’ve done if I hadn’t come across such a solution like this. I can also at this point relish my future. Thanks very much for your high quality and sensible help. I won’t hesitate to propose your web blog to any individual who wants and needs guide about this subject.

    回复 2019年10月23日
  23. longchamp handbags

    I am only commenting to let you know what a really good experience my cousin’s child went through browsing your blog. She noticed many issues, which included what it is like to possess an ideal giving character to have the mediocre ones smoothly comprehend certain hard to do issues. You truly did more than visitors’ expected results. Thank you for producing these invaluable, dependable, explanatory and in addition fun tips about this topic to Jane.

    回复 2019年10月22日
  24. kobe 11

    I simply wanted to thank you very much yet again. I do not know the things that I would’ve gone through in the absence of the entire information documented by you about my field. It seemed to be an absolute terrifying difficulty for me personally, nevertheless spending time with the very professional fashion you resolved it made me to cry over fulfillment. I’m grateful for this advice and as well , hope you are aware of a powerful job you’re carrying out educating most people by way of your blog. Most probably you’ve never encountered any of us.

    回复 2019年10月21日
  25. jordan retro

    I intended to post you a bit of remark in order to thank you so much yet again regarding the amazing views you have contributed above. This is certainly shockingly generous of people like you to supply openly exactly what many people could possibly have distributed for an e book to get some bucks for their own end, especially considering that you might have tried it if you decided. The things as well worked to be the great way to know that the rest have similar desire much like my personal own to realize more and more in regard to this issue. I’m sure there are some more fun sessions in the future for individuals who examine your blog.

    回复 2019年10月20日
  26. nike react

    I intended to send you that little bit of remark just to say thank you again over the pleasing guidelines you’ve discussed on this site. It has been simply remarkably generous with you in giving unreservedly what many individuals would’ve supplied for an ebook to help make some cash for their own end, and in particular considering the fact that you could have tried it if you ever desired. Those smart ideas additionally worked to become a great way to recognize that other people have similar passion just as my very own to know the truth significantly more with respect to this condition. I think there are millions of more pleasurable instances ahead for folks who go through your website.

    回复 2019年10月19日
  27. kobe shoes

    Thanks for your whole labor on this site. My mom takes pleasure in carrying out investigation and it is easy to see why. Most of us hear all relating to the compelling ways you provide priceless strategies through this website and even encourage participation from other ones on that issue plus our own daughter is undoubtedly starting to learn so much. Enjoy the rest of the year. You are doing a first class job.

    回复 2019年10月18日
  28. adidas gazelle

    A lot of thanks for each of your efforts on this web page. My aunt really loves doing research and it’s simple to grasp why. Most of us learn all of the powerful mode you make practical ideas on your blog and even welcome contribution from visitors on the content and our simple princess is without a doubt discovering a lot of things. Have fun with the remaining portion of the new year. You’re the one carrying out a fantastic job.

    回复 2019年10月17日
  29. hogan outlet online

    I am also writing to make you know of the exceptional experience my cousin’s daughter found going through your blog. She learned plenty of things, not to mention what it’s like to have an incredible teaching character to have other folks quite simply know specific multifaceted matters. You truly surpassed visitors’ desires. Thanks for displaying these warm and friendly, healthy, edifying not to mention unique tips on the topic to Evelyn.

    回复 2019年10月17日
  30. cheap jordans

    I must express some appreciation to you just for bailing me out of such a incident. Right after looking throughout the world-wide-web and seeing ideas that were not helpful, I assumed my entire life was well over. Living without the presence of approaches to the difficulties you have sorted out all through the short article is a critical case, and the ones which could have badly affected my entire career if I had not come across your web blog. The knowledge and kindness in taking care of all things was helpful. I’m not sure what I would have done if I hadn’t come across such a stuff like this. I can at this moment look forward to my future. Thank you so much for this expert and amazing help. I will not hesitate to suggest your web blog to anybody who should receive counselling on this matter.

    回复 2019年10月16日
  31. jimmy choo shoes

    I want to get across my passion for your generosity giving support to people who should have help on this question. Your special dedication to passing the solution all over became remarkably important and have frequently helped most people much like me to reach their goals. Your own interesting advice means so much a person like me and much more to my peers. Many thanks; from all of us.

    回复 2019年10月15日
  32. air max 270

    I would like to show some appreciation to this writer for rescuing me from such a matter. Because of surfing around throughout the online world and meeting suggestions which are not powerful, I believed my entire life was gone. Being alive minus the approaches to the problems you’ve resolved by way of your entire website is a crucial case, and those which could have negatively affected my entire career if I had not discovered your site. Your primary knowledge and kindness in touching all the things was useful. I’m not sure what I would have done if I hadn’t come across such a subject like this. I am able to at this moment look ahead to my future. Thanks for your time very much for this impressive and results-oriented guide. I won’t think twice to propose your blog post to any person who should get counselling on this subject.

    回复 2019年10月14日
  33. harden shoes

    I and also my friends have already been studying the great information and facts on your web page while before long got a terrible feeling I had not thanked you for them. All the boys were certainly warmed to study them and have now in actuality been using those things. I appreciate you for really being indeed kind and for getting these kinds of excellent things most people are really desperate to understand about. My very own honest apologies for not expressing appreciation to sooner.

    回复 2019年10月13日
  34. nike kyrie 5

    My spouse and i felt so fortunate Raymond could do his preliminary research by way of the ideas he had from your web page. It’s not at all simplistic just to be making a gift of guides that many people today may have been selling. And now we understand we’ve got the writer to appreciate for this. The main explanations you’ve made, the easy website navigation, the relationships you will help engender – it’s got all powerful, and it is making our son in addition to our family reckon that this situation is amusing, and that’s pretty pressing. Thank you for the whole thing!

    回复 2019年10月12日
  35. nike air force 1

    I have to express appreciation to this writer just for rescuing me from this circumstance. As a result of researching throughout the internet and getting principles that were not beneficial, I figured my life was gone. Being alive devoid of the strategies to the issues you have resolved through your good site is a critical case, as well as ones which might have adversely damaged my entire career if I had not encountered your blog. Your talents and kindness in handling every item was vital. I don’t know what I would have done if I hadn’t discovered such a subject like this. I’m able to at this moment look forward to my future. Thank you so much for your reliable and result oriented guide. I won’t be reluctant to endorse your blog to anybody who needs counselling about this problem.

    回复 2019年10月10日
  36. supreme clothing

    I not to mention my buddies were found to be looking at the best guidelines located on your site while all of the sudden came up with a horrible feeling I had not expressed respect to the web blog owner for those secrets. All the men were for this reason joyful to read them and have now extremely been loving these things. We appreciate you being quite kind and then for making a decision on varieties of superb resources millions of individuals are really wanting to understand about. My very own sincere apologies for not saying thanks to you sooner.

    回复 2019年10月9日
  37. irving shoes

    I simply wished to say thanks once again. I’m not certain the things that I could possibly have implemented without the entire secrets documented by you over my area of interest. It truly was a terrifying matter for me personally, but spending time with the professional manner you handled it made me to leap with delight. I’m just happier for the information as well as trust you really know what an amazing job you have been putting in educating people thru your web blog. Probably you have never encountered any of us.

    回复 2019年10月8日
  38. fila shoes

    Thanks a lot for giving everyone remarkably brilliant opportunity to discover important secrets from this blog. It is often very lovely and also jam-packed with a lot of fun for me and my office friends to visit your website nearly thrice in a week to learn the newest things you have got. Of course, we are actually astounded concerning the amazing advice you serve. Selected 1 areas in this article are easily the best we have ever had.

    回复 2019年10月8日