NODE使用CMD命令

福尔摩斯对我笑 2019-02-20 835 次 184条

 

今天在做一个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
未来博客 博主大人
到了这个年纪,不应该再彷徨!

发表评论



 


已有 184 条评论 1


  1. www.Metroclick.com

    MetroClick specializes in building completely interactive products like Photo Booth for rental or sale, Touch Screen Kiosks, Large Touch Screen Displays , Monitors, Digital Signages and experiences. With our own hardware production facility and in-house software development teams, we are able to achieve the highest level of customization and versatility for Photo Booths, Touch Screen Kiosks, Touch Screen Monitors and Digital Signage. Visit MetroClick in NYC at http://www.metroclick.com/ or , 121 Varick St, New York, NY 10013, +1 646-843-0888

    回复 2019年12月10日
  2. SapidAgency.com

    Sapid Agency is a Search Engine Optimization company in New York City that provides SEO Services. Their proprietary SEO strategies help struggling websites and aspiring business owners to rank their websites higher in multiple search engines like Google , Yahoo and Bing. They provide local and gmb map ranking for businesses in NYC and many other local areas. Find more at https://www.sapidagency.com/

    回复 2019年12月10日
  3. qq slot Terbaru

    I quite like reading through a post that will make people think.
    Also, thank you for permitting me to comment!

    回复 2019年12月10日
  4. Desk assembly service

    Usually I do not learn article on blogs, however I would like to say that this write-up very pressured me to try and do so! Your writing taste has been amazed me. Thanks, very nice post.

    回复 2019年12月10日
  5. Last Minute City Breaks

    Generally I do not learn post on blogs, however I would like to say that this write-up very compelled me to take a look at and do it! Your writing taste has been surprised me. Thank you, quite great post.

    回复 2019年12月10日
  6. เกมไฮโล y8

    Can I simply say what a aid to find someone who really knows what theyre speaking about on the internet. You undoubtedly know how you can carry an issue to gentle and make it important. Extra individuals have to read this and understand this side of the story. I cant imagine youre no more standard since you definitely have the gift.

    回复 2019年12月10日
  7. drugs

    I have recently started a site, the info you provide on this website has helped me greatly. Thanks for all of your time & work. “So full of artless jealousy is guilt, It spills itself in fearing to be spilt.” by William Shakespeare.

    回复 2019年12月10日
  8. บ่อนพนัน ญี่ปุ่น

    Great info and right to the point. I don’t know if this is really the best place to ask but do you guys have any thoughts on where to employ some professional writers? Thanks in advance 🙂

    回复 2019年12月10日
  9. house cleaning Las Vegas

    Hi there! I just wanted to ask if you ever have any problems with hackers? My last blog (wordpress) was hacked and I ended up losing a few months of hard work due to no data backup. Do you have any methods to protect against hackers?

    回复 2019年12月9日
  10. Pool table installation

    Hello my family member! I wish to say that this article is awesome, great written and come with almost all significant infos. I would like to peer extra posts like this .

    回复 2019年12月9日
  11. Furniture assembly handyman

    I as well as my guys were found to be viewing the great tactics located on your website then before long I had an awful feeling I never thanked the website owner for them. The men were definitely thrilled to study all of them and have seriously been loving them. Thank you for indeed being so considerate and for going for this kind of brilliant areas millions of individuals are really desirous to be informed on. My personal honest apologies for not expressing appreciation to sooner.

    回复 2019年12月9日
  12. gado gado asin

    hey there and thank you for your information – I have certainly picked up anything new from right here.
    I did however expertise a few technmical points
    using tis webb site, as I experienced to reload the
    web site lots of times previous to I could get it to load properly.
    I hadd been wondeering if your web host is OK?
    Not that I’m complaining, but slow loading instances times will very
    frequently affect your placement in google and can damage your
    high-quality scpre if advertising and marketing with Adwords.
    Anyway I’m adding this RSS to my email and can look out for a lot more of your respective intriguing content.
    Ensure that you update this again very soon.

    回复 2019年12月9日
  13. situs game slot online Terbaik

    Thanks in favor of sharing such a fastidious idea, paragraph is
    nice, thats why i have read it entirely

    回复 2019年12月9日
  14. i99BET

    This is a topic which is close to my heart… Cheers! Exactly where are your contact details though?

    回复 2019年12月9日
  15. viagra

    You are my intake, I own few web logs and infrequently run out from post :). “No opera plot can be sensible, for people do not sing when they are feeling sensible.” by W. H. Auden.

    回复 2019年12月9日
  16. i99BET

    After exploring a handful of the blog posts on your website, I seriously appreciate your way of blogging. I book marked it to my bookmark website list and will be checking back soon. Take a look at my web site as well and let me know how you feel.

    回复 2019年12月8日
  17. Furniture installers

    fantastic points altogether, you simply gained a new reader. What would you recommend in regards to your post that you made some days ago? Any positive?

    回复 2019年12月8日
  18. Furniture assembly help

    I beloved as much as you will obtain carried out right here. The comic strip is attractive, your authored subject matter stylish. however, you command get got an impatience over that you would like be turning in the following. sick without a doubt come more until now once more as exactly the same nearly very frequently within case you defend this hike.

    回复 2019年12月8日
  19. instagram 47 porno 52 scam

    instagram 103 porno 108 scam

    回复 2019年12月7日
  20. any assembly

    Unquestionably believe that which you said. Your favorite reason appeared to be on the net the easiest thing to be aware of. I say to you, I certainly get irked while people think about worries that they just don’t know about. You managed to hit the nail upon the top as well as defined out the whole thing without having side effect , people could take a signal. Will probably be back to get more. Thanks

    回复 2019年12月7日
  21. HQ office installers

    Very interesting subject , thanks for posting . “Experience a comb life gives you after you lose your hair.” by Judith Stern.

    回复 2019年12月7日
  22. Tractor Workshop Manuals

    The planet probably are hidden in addition to exotic smaller gardens might be maintained, and rainforest that is certainly certainly profligate with wide variety of facilities resembling golf game workshops, athletic courts and as a consequence swimming pools. Hotels Discounts

    回复 2019年12月7日
  23. instagram 190 porno 195 scam

    instagram 234 porno 239 scam

    回复 2019年12月7日
  24. ตู้ม้าออนไลน์ scr888

    It’s appropriate time to make some plans for the future and it is time to be happy. I’ve read this post and if I could I wish to suggest you few interesting things or advice. Maybe you can write next articles referring to this article. I desire to read even more things about it!

    回复 2019年12月7日
  25. John Deere Repair Manuals

    Easily, the post is really the greatest on this laudable topic. I concur with your conclusions and will thirstily watch forward to your future updates. Saying cheers will not just be sufficient, for the great c lucidity in your writing. I will instantly grab your rss feed to stay privy of any updates. Solid work and much success in your business enterprise!

    回复 2019年12月7日
  26. pills

    It is really a nice and useful piece of information. I’m glad that you simply shared this helpful information with us. Please stay us up to date like this. Thanks for sharing.

    回复 2019年12月6日
  27. HQ office installers

    There is apparently a bunch to realize about this. I think you made certain nice points in features also.

    回复 2019年12月6日
  28. instagram 75 porno 80 scam

    instagram 136 porno 141 scam

    回复 2019年12月6日
  29. Logo Designer

    Thanks for one’s marvelous posting! I truly enjoyed reading it, you might be a great author.I will be sure to bookmark your blog and will come back later on. I want to encourage you to continue your great writing, have a nice weekend!

    回复 2019年12月6日
  30. togel-Singapura.net

    NODE使用CMD命令 | 未来博客 http://shareartist.com

    回复 2019年12月6日
  31. instagram 225 porno 230 scam

    instagram 169 porno 174 scam

    回复 2019年12月6日
  32. longchamp handbags

    I抎 have to verify with you here. Which isn’t something I often do! I take pleasure in reading a publish that will make individuals think. Additionally, thanks for permitting me to remark!

    回复 2019年12月6日
  33. cebantogel

    Awsome article and right to the point. I don’t know if this is in fact the best place to ask but do you folks have any thoughts on where to hire some professional writers? Thx 🙂

    回复 2019年12月6日
  34. instagram 40 porno 45 scam

    instagram 114 porno 119 scam

    回复 2019年12月5日
  35. instagram 117 porno 122 scam

    instagram 122 porno 127 scam

    回复 2019年12月5日
  36. instagram takipci satin al

    dolandirilmaya hazir misiniz? sizi de sikis izlemeye bekleriz. anal sikis bizde bedava.

    回复 2019年12月4日
  37. warez

    dolandirilmaya hazir misiniz? sizi de sikis izlemeye bekleriz. anal sikis bizde bedava.

    回复 2019年12月4日
  38. i99bet

    It’s nearly impossible to find well-informed people for this subject, but you seem like you know what you’re talking about! Thanks

    回复 2019年12月4日
  39. instagram

    dolandirilmaya hazir misiniz? sizi de sikis izlemeye bekleriz. anal sikis bizde bedava.

    回复 2019年12月4日
  40. instatakipci.com

    instagram takipci satin al sopsosyal instagram takipci satin al

    回复 2019年12月3日
  41. sopsosyal

    instagram takipci satin al sopsosyal

    回复 2019年12月3日
  42. John Deere Service Manuals

    Hi. I found your blog using msn. This can be a really well written article. I’ll be guaranteed to bookmark it are available returning to read much more of your useful info. Thanks for the post. I am going to certainly return.

    回复 2019年12月3日
  43. i99bet

    Spot on with this write-up, I truly feel this amazing site needs a great deal more attention. I’ll probably be returning to read through more, thanks for the info!

    回复 2019年12月3日
  44. curry 6

    Would you be fascinated about exchanging hyperlinks?

    回复 2019年12月3日
  45. instagram takipci satin al

    instagram takipci satin al sopsosyal

    回复 2019年12月3日
  46. sopsosyal

    instagram takipci satin al sopsosyal

    回复 2019年12月3日