{"id":2102,"date":"2022-08-30T15:22:04","date_gmt":"2022-08-30T15:22:04","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/26\/where-do-npm-background-scripts-go-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:22:04","modified_gmt":"2022-08-30T15:22:04","slug":"where-do-npm-background-scripts-go-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/where-do-npm-background-scripts-go-collection-of-common-programming-errors\/","title":{"rendered":"Where do npm background scripts go?-Collection of common programming errors"},"content":{"rendered":"<p>I have a program which relies on another program being run so I added this <code>build<\/code> command to my <code>package.json<\/code> file<\/p>\n<pre><code>\"scripts\": {\n    \"start\": \"brunch watch --server\",\n    \"build\": \"coffee server\/modules\/events\/book_server.coffee &amp;\"\n  }\n<\/code><\/pre>\n<p>so before I run my main script with <code>npm start<\/code> I run <code>npm run-script build<\/code> which makes my main script work, however when I quit my main script and then look for background processes with <code>bg<\/code> I get the message<\/p>\n<pre><code>&gt; -bash: bg: current: no such job\n<\/code><\/pre>\n<p>so I decided to run <code>npm run-script build<\/code> again however the previous process must still have been running because I got the error message<\/p>\n<pre><code>events.js:66\n        throw arguments[1]; \/\/ Unhandled 'error' event\n                       ^\nError: listen EADDRINUSE\n  at errnoException (net.js:776:11)\n  at Server._listen2._connectionKey (net.js:917:26)\n  at process.startup.processNextTick.process._tickCallback (node.js:244:9)\n<\/code><\/pre>\n<p>and when I tried to run my main program again I got the error<\/p>\n<pre><code>&gt; TypeError: Cannot read property 'port' of null\n<\/code><\/pre>\n<p>My question is, where are these background scripts?<\/p>\n<p>So far I have looked in all the obvious places including <code>package.json<\/code>&#8216;s directory the directory for <code>book_server.coffee<\/code> and in <code>node_modules<\/code> but with no luck.<\/p>\n<ol>\n<li>\n<p>So the background processes go into the background just running inside the OS but not attached to your shell&#8217;s tty. To find them, use <code>jobs -l<\/code> or the <code>ps<\/code> command with something like <code>ps -ef<\/code>. You can use <code>grep<\/code> to filter the output, but what you need to find is the process ID (PID) of your build process so you can stop it with <code>kill<\/code> . You may also want to read up on <code>pgrep<\/code> and <code>pkill<\/code> which are handy for this process.<\/p>\n<p>Note that in your example you use <code>bg<\/code> when it&#8217;s not appropriate. <code>bg<\/code> is for this sequence: 1. start a job in your shell&#8217;s foreground, 2. suspend that job with CTRL-Z, 3. use <code>bg<\/code> to tell the shell &#8220;allow this job to continue executing, but detached from my tty in the background&#8221;. (again, <code>jobs<\/code> is what you are looking for here).<\/p>\n<p>For the bigger picture, there&#8217;s no need or benefit of running that <code>coffee<\/code> command in the background as it is just a simple compliation step that should take on the order of a few milliseconds.<\/p>\n<p>For an amazingly-detailed &#8220;reread every year&#8221; level of depth, check out The TTY demystified.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-26 03:15:16. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have a program which relies on another program being run so I added this build command to my package.json file &#8220;scripts&#8221;: { &#8220;start&#8221;: &#8220;brunch watch &#8211;server&#8221;, &#8220;build&#8221;: &#8220;coffee server\/modules\/events\/book_server.coffee &amp;&#8221; } so before I run my main script with npm start I run npm run-script build which makes my main script work, however when [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2102","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2102"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2102\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}