NODE使用CMD命令

福尔摩斯对我笑 2019-02-20 33183 次 3016条

 

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

发表评论



 


已有 3016 条评论 1


  1. 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日
  2. 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日
  3. 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日
  4. 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日
  5. 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日
  6. 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日
  7. 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日
  8. 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日
  9. 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日
  10. 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日
  11. 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日
  12. 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日
  13. 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日
  14. 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日
  15. 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日
  16. 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日