Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hot reload is not working with this demo #1174

Closed
icex opened this issue Jan 7, 2023 · 4 comments
Closed

Hot reload is not working with this demo #1174

icex opened this issue Jan 7, 2023 · 4 comments

Comments

@icex
Copy link

icex commented Jan 7, 2023

If the typescript code is invalid, it will crash the server instead of failing to load the service nicely.

[2023-01-07T01:53:47.637Z] INFO  192-168-0-62-97715/BROKER: The 'services/greeter.service.ts' file is changed. (Event: change)
[2023-01-07T01:53:48.138Z] INFO  192-168-0-62-97715/BROKER: Reload 1 service(s)
[2023-01-07T01:53:48.138Z] INFO  192-168-0-62-97715/BROKER: Hot reload 'greeter' service... services/greeter.service.ts
[2023-01-07T01:53:48.139Z] INFO  192-168-0-62-97715/GREETER: Service 'greeter' stopped.
[2023-01-07T01:53:48.139Z] INFO  192-168-0-62-97715/BROKER: Service 'greeter' is stopped.
[2023-01-07T01:53:48.173Z] ERROR 192-168-0-62-97715/BROKER: Failed to load service '/Users/bogdan/Sites/project-typescript-demo/services/greeter.service.ts' services/greeter.service.ts:18:1 - error TS2304: Cannot find name 'sd'.

18 sd
   ~~

/Users/bogdan/Sites/project-typescript-demo/node_modules/ts-node/src/index.ts:859
    return new TSError(diagnosticText, diagnosticCodes, diagnostics);
           ^
TSError: ⨯ Unable to compile TypeScript:
services/greeter.service.ts:18:1 - error TS2304: Cannot find name 'sd'.

18 sd
   ~~

    at createTSError (/Users/bogdan/Sites/project-typescript-demo/node_modules/ts-node/src/index.ts:859:12)
    at reportTSError (/Users/bogdan/Sites/project-typescript-demo/node_modules/ts-node/src/index.ts:863:19)
    at getOutput (/Users/bogdan/Sites/project-typescript-demo/node_modules/ts-node/src/index.ts:1077:36)
    at Object.compile (/Users/bogdan/Sites/project-typescript-demo/node_modules/ts-node/src/index.ts:1433:41)
    at Module.m._compile (/Users/bogdan/Sites/project-typescript-demo/node_modules/ts-node/src/index.ts:1617:30)
    at Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/bogdan/Sites/project-typescript-demo/node_modules/ts-node/src/index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at ServiceBroker.loadService (/Users/bogdan/Sites/project-typescript-demo/node_modules/moleculer/src/service-broker.js:777:14)
    at /Users/bogdan/Sites/project-typescript-demo/node_modules/moleculer/src/middlewares/hot-reload.js:33:19
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  diagnosticCodes: [ 2304 ]
}

A solution may be to add this to hot-reload.js line 33

try {
	return broker.loadService(service.__filename);
} catch {}
@icebob icebob transferred this issue from moleculerjs/project-typescript-demo Jan 7, 2023
@icebob icebob closed this as completed in 04fa132 Jan 7, 2023
@icebob
Copy link
Member

icebob commented Jan 7, 2023

Thanks, fixed.

@matpaul
Copy link

matpaul commented Mar 20, 2023

@icebob Hello, I use typescript template from scratch. Start with npm run dev.
If there are any compile errors process is shutdown and I need to restart it again.

A solution may be to add try catch block in runner.js line 438 and I use ts-node-dev

_.uniq(serviceFiles).forEach(f => {
 try {	
    this.broker.loadService(f)
  } catch (e) { } 
});`

@icebob
Copy link
Member

icebob commented Mar 26, 2023

Nice catch, could you please open a fix PR?

@matpaul
Copy link

matpaul commented Mar 27, 2023

@icebob, I rechecked my fix, and it only helps at the start if errors exist, but it breaks hot-reload.
There is another issue if I make an error in the code, and the hot middleware runs watcher 2 times(just try save two times on osx); it also crushes the process. I'll try to figure it out. Maybe it's worth using nodemon/ts-node-dev/tsc --watch, but it will reload everything (which is not so bad).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants