title: Best way to run some code only once on server start in Nuxt.js (and not during build)
body: <p>While I wrote <a href="https://stackoverflow.com/a/65576249/2938364">this answer for StackOverflow</a> I came across the problem that I wanted to <strong>execute some code exactly once</strong> in Nuxt.js when the server is started (or while the server is starting).</p>
  <p>I thought about writing a module or a plugin, but I did not succeed, since both were executed every time I contacted the server, i.e., every time I made a call to <em>localhost:3000/myPage</em>.
  Probably I am missing the right module hook or some plugin setting?</p>
  <p>Since I could not find any information on this problem in the official documentation, I now came up with a quite ugly solution: I wrote a serverMiddleware that executes some code (the code that should be run only once) and then returns an empty handler - which now adds (little) unnecessary overhead to every request.</p>
  <p><strong>My question:</strong> What would have been the best way to execute some code exactly once on a Nuxt.js server during/after startup, but not during building?</p>
  <hr />
  <p><em>Minimal ugly working example</em>:</p>
  <p><code>nuxt.config.js</code>:</p>
  <pre><code>export default {
    ...
    serverMiddleware: [ &quot;~/serverMiddleware/run-once.js&quot; ]
    ...
  }
  </code></pre>
  <p><code>~/serverMiddleware/run-once.js</code>:</p>
  <pre><code>console.log(&quot;Yay, I only run once when the server is started!&quot;)
  // Since we are a serverMiddleware, we have to return a handler, even if this it does nothing
  // I think this is really ugly...
  export default function (req, res, next) {
    next()
  }
  </code></pre>
  <hr />
  <p>PS: I saw questions such as <a href="https://stackoverflow.com/questions/63689739/is-it-possible-for-nuxt-js-plugins-to-only-run-once">Is it possible for Nuxt JS plugins to only run once?</a> but it seems to not answer my question since the <code>vue-renderer:ssr:prepareContext</code> nuxt hook also executes every time I send a request to the server.</p>
  <p>Also, a module such as the following does not work since it is also executed during <code>nuxt build</code> and not only on <code>nuxt dev</code> or <code>nuxt start</code>:</p>
  <pre><code>export default function MyModuleThatShouldOnlyRunOnce (_moduleOptions) {
    console.log(&quot;I run on 'nuxt dev' and 'nuxt start', but sadly also on 'nuxt build' :(&quot;);
  }
  </code></pre>