{"id":315,"date":"2023-12-26T22:30:20","date_gmt":"2023-12-26T22:30:20","guid":{"rendered":"https:\/\/frontendmasters.com\/blog\/?p=315"},"modified":"2023-12-26T22:30:41","modified_gmt":"2023-12-26T22:30:41","slug":"vs-code-auto-run-commands","status":"publish","type":"post","link":"https:\/\/frontendmasters.com\/blog\/vs-code-auto-run-commands\/","title":{"rendered":"Make VS Code Automatically Run Your Project Startup Commands When You Open That Project&#8217;s Folder"},"content":{"rendered":"\n<p>Do you have a project where every time you open up that project&#8217;s root folder in VS Code, you need to run something like <code>npm run dev<\/code> to get started working? It&#8217;s likely that spins up the compiler and server and such needed to see and work on the site. Myself, I&#8217;ve got lots of projects like that.<\/p>\n\n\n\n<p>I think it&#8217;s tremendously useful to automate this, and fortunately VS Code makes it easy. Say our whole goal is literally to run <code>npm run dev<\/code> when a particular projects root folder is opened. To do this: create a file called <code>tasks.json<\/code> in the root project folder&#8217;s <code>.vscode<\/code> folder, then add this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json\">{\n  <span class=\"hljs-attr\">\"version\"<\/span>: <span class=\"hljs-string\">\"2.0.0\"<\/span>,\n  <span class=\"hljs-attr\">\"tasks\"<\/span>: &#91;\n    {\n      <span class=\"hljs-attr\">\"type\"<\/span>: <span class=\"hljs-string\">\"npm\"<\/span>,\n      <span class=\"hljs-attr\">\"script\"<\/span>: <span class=\"hljs-string\">\"dev\"<\/span>,\n      <span class=\"hljs-attr\">\"runOptions\"<\/span>: {\n        <span class=\"hljs-attr\">\"runOn\"<\/span>: <span class=\"hljs-string\">\"folderOpen\"<\/span>\n      }\n    }\n  ]\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It should look something like this within your project:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"762\" src=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/12\/Screenshot-2023-12-26-at-1.47.18%E2%80%AFPM.png?resize=1024%2C762&#038;ssl=1\" alt=\"\" class=\"wp-image-318\" style=\"width:517px;height:auto\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/12\/Screenshot-2023-12-26-at-1.47.18%E2%80%AFPM.png?resize=1024%2C762&amp;ssl=1 1024w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/12\/Screenshot-2023-12-26-at-1.47.18%E2%80%AFPM.png?resize=300%2C223&amp;ssl=1 300w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/12\/Screenshot-2023-12-26-at-1.47.18%E2%80%AFPM.png?resize=768%2C572&amp;ssl=1 768w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/12\/Screenshot-2023-12-26-at-1.47.18%E2%80%AFPM.png?w=1378&amp;ssl=1 1378w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p>(It&#8217;s the same folder that holds the <code>settings.json<\/code> file you can use for per-project settings. I use that for stuff like what files\/folders to ignore in the sidebar and search.)<\/p>\n\n\n\n<p>Now next time you open that project folder, it&#8217;ll run that script:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"978\" height=\"214\" src=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/12\/Screenshot-2023-12-26-at-1.48.30%E2%80%AFPM.png?resize=978%2C214&#038;ssl=1\" alt=\"\" class=\"wp-image-319\" style=\"width:518px;height:auto\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/12\/Screenshot-2023-12-26-at-1.48.30%E2%80%AFPM.png?w=978&amp;ssl=1 978w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/12\/Screenshot-2023-12-26-at-1.48.30%E2%80%AFPM.png?resize=300%2C66&amp;ssl=1 300w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/12\/Screenshot-2023-12-26-at-1.48.30%E2%80%AFPM.png?resize=768%2C168&amp;ssl=1 768w\" sizes=\"auto, (max-width: 978px) 100vw, 978px\" \/><\/figure>\n\n\n\n<p>You may get a permissions popup to approve if it&#8217;s your first time.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"798\" height=\"278\" src=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/12\/1_7J7QCysn346-PVHuBlSClA.webp?resize=798%2C278&#038;ssl=1\" alt=\"\" class=\"wp-image-320\" style=\"width:515px;height:auto\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/12\/1_7J7QCysn346-PVHuBlSClA.webp?w=798&amp;ssl=1 798w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/12\/1_7J7QCysn346-PVHuBlSClA.webp?resize=300%2C105&amp;ssl=1 300w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/12\/1_7J7QCysn346-PVHuBlSClA.webp?resize=768%2C268&amp;ssl=1 768w\" sizes=\"auto, (max-width: 798px) 100vw, 798px\" \/><\/figure>\n\n\n\n<p>You can also manually approve or disapprove this functionality. From the Command Palette search for &#8220;Manage Automatic Tasks&#8221; and you&#8217;ll get this.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"644\" height=\"258\" src=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/12\/Screenshot-2023-12-26-at-1.50.11%E2%80%AFPM.png?resize=644%2C258&#038;ssl=1\" alt=\"\" class=\"wp-image-321\" style=\"width:379px;height:auto\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/12\/Screenshot-2023-12-26-at-1.50.11%E2%80%AFPM.png?w=644&amp;ssl=1 644w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/12\/Screenshot-2023-12-26-at-1.50.11%E2%80%AFPM.png?resize=300%2C120&amp;ssl=1 300w\" sizes=\"auto, (max-width: 644px) 100vw, 644px\" \/><\/figure>\n\n\n\n<p>You can also search for &#8220;task&#8221; in the Command Palette and the tasks that you&#8217;ve defined will appear as commands you can run on-demand.<\/p>\n\n\n\n<p><a href=\"https:\/\/code.visualstudio.com\/docs\/editor\/tasks\">This is all well-documented in the VS Code documentation.<\/a> Note that the full command &#8220;npm run dev&#8221; doesn&#8217;t appear in that <code>tasks.json<\/code> file. That&#8217;s because npm is a special &#8220;auto detected&#8221; task type in VS Code, which is also true for Gulp, Grunt, and Jake. You aren&#8217;t limited to those though, you can any shell or process command. Also note that <code>tasks<\/code> is an array, so you can set up and run multiple commands. <\/p>\n\n\n\n<p>Me, I just like keeping it simple and running a single command. But if this appeals to you, check out those docs as there is lots of configurable power here. My experience beyond this in local development that needs to run a lot of commands to get going is centered around <a href=\"https:\/\/github.com\/tmux\/tmux\/wiki\">tmux<\/a>, so check that out if you find yourself in a situation where you are needing run, say, more than 4 commands to kick off a dev enviornment.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do you have a project where every time you open up that project&#8217;s root folder in VS Code, you need to run something like npm run dev to get started working? It&#8217;s likely that spins up the compiler and server and such needed to see and work on the site. Myself, I&#8217;ve got lots of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":323,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[52,43],"class_list":["post-315","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-post","tag-command-line","tag-vs-code"],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2023\/12\/tasks-thumb.jpg?fit=1000%2C500&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/comments?post=315"}],"version-history":[{"count":4,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/315\/revisions"}],"predecessor-version":[{"id":324,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/315\/revisions\/324"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media\/323"}],"wp:attachment":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media?parent=315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/categories?post=315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/tags?post=315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}