NODE使用CMD命令

福尔摩斯对我笑 2019-02-20 11014 次 1173条

 

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

发表评论



 


已有 1173 条评论 1


  1. cialis without prescription

    cialis tadalafil 20mg [url=http://buyscialisrx.com/]cialis coupon[/url] tadalafil patent number buy cialis online tadalafil
    anvisa tadalafil argentina precio http://buyscialisrx.com/ cuanto dura
    el efecto de tadalafil

    回复 2020年2月26日
  2. galvanized tc bolts

    Does your blog have a contact page? I’m having problems locating it but, I’d like to shoot you an email. I’ve got some recommendations for your blog you might be interested in hearing. Either way, great site and I look forward to seeing it grow over time.

    回复 2020年2月26日
  3. modern construction industry

    With havin so much content and articles do you ever run into any problems of plagorism or copyright infringement? My site has a lot of completely unique content I’ve either written myself or outsourced but it seems a lot of it is popping it up all over the internet without my authorization. Do you know any solutions to help protect against content from being stolen? I’d genuinely appreciate it.

    回复 2020年2月25日
  4. Svensk app och hemsideutveckling

    Interesting blog! Is your theme custom made or did you download it from somewhere?
    A design like yours with a few simple adjustements would really make my
    blog stand out. Please let me know where you got your
    theme. Bless you

    回复 2020年2月25日
  5. maid service las vegas

    Hello there! I know this is kinda off topic but I was wondering which blog platform are you using for this site? I’m getting sick and tired of WordPress because I’ve had issues with hackers and I’m looking at options for another platform. I would be awesome if you could point me in the direction of a good platform.

    回复 2020年2月25日
  6. superb maids llc

    Hello there! This post could not be written any better! Reading through this post reminds me of my good old room mate! He always kept chatting about this. I will forward this write-up to him. Fairly certain he will have a good read. Thank you for sharing!

    回复 2020年2月25日
  7. motherhood and work life

    Hey there great blog! Does running a blog such as this require a great deal of work? I have very little knowledge of coding but I had been hoping to start my own blog soon. Anyways, if you have any recommendations or techniques for new blog owners please share. I understand this is off topic however I simply needed to ask. Thanks a lot!

    回复 2020年2月24日
  8. Evacycle 回复 2020年2月24日
  9. MLB Jerseys 回复 2020年2月24日
  10. Tractor Workshop Manuals

    I am always thought about this, thankyou for putting up.

    回复 2020年2月23日
  11. drenaje linfatico madrid

    Greetings from Idaho! I’m bored to tears at work so I decided to browse your blog on my iphone during lunch break.
    I enjoy the knowledge you present here and can’t wait to take a look when I get home.
    I’m amazed at how fast your blog loaded on my phone ..
    I’m not even using WIFI, just 3G .. Anyways, excellent
    blog!

    回复 2020年2月22日
  12. Evacycle 回复 2020年2月22日
  13. Darrin Gilford

    This writing article support review is basically standard. This review is extremely useful to students accumulated some essay writing details for good essay. Such a essay writing articles are extremely impressive in order to viewers. Thank you pertaining to sharing your notions. anchor |

    回复 2020年2月22日
  14. Human Races 回复 2020年2月22日
  15. utkvon

    vcppdo canadian online pharmacy viagra viagra online canada pharmacy
    canadian pharmacy buy viagra order medication online

    回复 2020年2月22日
  16. https://peteconnelly.com/2020/02/17/how-to-detox-from-marijuana-abuse-2/

    As soon as I discovered this web site I went on reddit to share some of the love with them.

    回复 2020年2月21日
  17. Nike Outlet 回复 2020年2月21日
  18. best penis enlargement pills

    Do you have a spam problem on this site; I also am a blogger, and I was wondering your situation; we have
    created some nice practices and we are looking to swap techniques with others, be
    sure to shoot me an email if interested.

    回复 2020年2月21日
  19. daftar Lexus888

    You need to be a part of a contest for one of the greatest sites on the web.
    I am going to recommend this site!

    回复 2020年2月21日
  20. Evacycle 回复 2020年2月21日
  21. holiday vacations catalog

    Thanks on your marvelous posting! I genuinely enjoyed reading it, you can be a great author.I will remember to bookmark your blog and will eventually come back in the foreseeable future. I want to encourage you continue your great writing, have a nice holiday weekend!

    回复 2020年2月19日
  22. travel ins pre existing medical conditions

    Hmm is anyone else experiencing problems with the pictures on this blog loading? I’m trying to find out if its a problem on my end or if it’s the blog. Any responses would be greatly appreciated.

    回复 2020年2月19日
  23. Denver Rabasca

    Thanks for the best blog.it was very useful for me.keep sharing such ideas in the future as well. address |

    回复 2020年2月19日
  24. content marketing agency

    My programmer is trying to convince me to move to .net from PHP. I have always disliked the idea because of the costs. But he’s tryiong none the less. I’ve been using Movable-type on a number of websites for about a year and am worried about switching to another platform. I have heard very good things about blogengine.net. Is there a way I can transfer all my wordpress posts into it? Any help would be really appreciated!

    回复 2020年2月19日
  25. sbi travel insurance

    I like what you guys are up too. This type of clever work and reporting! Keep up the wonderful works guys I’ve included you guys to my blogroll.

    回复 2020年2月19日
  26. annual travel insurance including cruise cover

    Today, while I was at work, my sister stole my iphone and tested to see if it can survive a twenty five foot drop, just so she can be a youtube sensation. My apple ipad is now broken and she has 83 views. I know this is entirely off topic but I had to share it with someone!

    回复 2020年2月19日
  27. timberland boots

    I must express my appreciation to this writer for rescuing me from this type of predicament. Just after checking throughout the the net and seeing advice which were not productive, I believed my life was well over. Existing minus the approaches to the problems you have sorted out all through your main posting is a critical case, as well as those which might have in a wrong way damaged my entire career if I hadn’t discovered your blog post. Your main mastery and kindness in maneuvering all the details was tremendous. I don’t know what I would’ve done if I had not encountered such a thing like this. It’s possible to at this point look forward to my future. Thank you very much for your expert and amazing guide. I won’t think twice to propose your web site to anybody who needs and wants guide about this situation.

    回复 2020年2月19日
  28. Celaxryn

    Great web site. A lot of helpful information here.
    I’m sending it to several pals ans also sharing in delicious.
    And naturally, thank you in your effort!

    回复 2020年2月19日
  29. Evacycle 回复 2020年2月19日
  30. Evacycle 回复 2020年2月19日
  31. holidays with teenagers

    Hello! I just wanted to ask if you ever have any trouble with hackers? My last blog (wordpress) was hacked and I ended up losing several weeks of hard work due to no backup. Do you have any solutions to prevent hackers?

    回复 2020年2月19日
  32. traveling with asthma

    Thanks on your marvelous posting! I really enjoyed reading it, you can be a great author.I will remember to bookmark your blog and definitely will come back later in life. I want to encourage you to ultimately continue your great posts, have a nice day!

    回复 2020年2月18日
  33. compare cruise insurance

    Heya this is kinda of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding experience so I wanted to get guidance from someone with experience. Any help would be enormously appreciated!

    回复 2020年2月18日
  34. digital agency near me

    Wow that was odd. I just wrote an very long comment but after I clicked submit my comment didn’t appear. Grrrr… well I’m not writing all that over again. Anyhow, just wanted to say excellent blog!

    回复 2020年2月18日
  35. Yeezy 回复 2020年2月18日