vite-node --watch doesn't close process before restarting

See original GitHub issue

Describe the bug

I created a simple Typescript Express app that I wanted to try vite-node with. It runs fine but when I edit the src code, I get this error:

[vite-node] Failed to execute file:                       
Error: listen EADDRINUSE: address already in use :::4000

Which shows that vite-node isn’t closing the previous process when in watch mode.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-7yrhdv?file=package.json,server.ts&initialPath=__vitest__

changing what is logged out starts to accumulate multiple log entries.

System Info

n/a

Used Package Manager

npm

Validations

Issue Analytics

  • State:open
  • Created 10 months ago
  • Reactions:2
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
sheremet-vacommented, Nov 21, 2022

So, Vite considers editing the main entry here as a full reload type of HMR, so it won’t call dispose, and it calls accept too late. But I found a way to close server before ViteNode reevaluates file:

if (import.meta.hot) {
  import.meta.hot.on("vite:beforeFullReload", () => {
    server.close();
  });
}

Maybe it’s a good idea to also add dispose just in case:

import.meta.hot.dispose(() => {
  server.close()
});

It will be called only if HMR is triggered not as a full reload. According to Vite docs it’s meant to clear all side effects.

We should probably add this information to docs.

0reactions
david-pluggecommented, Nov 19, 2022

Alright, i have to revert what i´ve said. The error still appears, i just didn´t fully tested it.

Using vite-node for running servers in development doesnt seems to work at the moment. What could eventually solve it would be an option to completely get rid of the current node process that runs the code and spawn a new process with the latest code (basically rebuild the whole app and run it like nodemon). I´ve tried disabling hmr but that just disables all reload capabilities.

I´d love to use vite for this due to the speed and plugin ecosystem but i wonder if this kind of usage is in the scope of vite-node or if we should rather use something else to run our server (something like nodemon/ts-node-dev).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vite does not shutdown properly when calling close ... - GitHub
This is problematic when using the ViteDevServer with Jest as the process doesn't end cleanly after the server is closed.
Read more >
Node.js Port 3000 already in use but it actually isn't?
The issue is because there were node services is running in the background even if nodemon restarts. This will terminate all the running...
Read more >
STOP Using Create React App - YouTube
Create React App is a plague and we're here to cure it. Please use Vite, NextJS, or Remix instead. Seriously, anything is better...
Read more >
How to build and deploy with Vite JS - YouTube
What Is Vite ? Vite is the French word for fast and is a Javascript development server and bundler that delivers source files...
Read more >
Configuring Vitest
To configure vitest itself, add test property in your Vite config. ... Vitest doesn't do static analysis, and cannot fail before your ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found