Describe the solution you'd like
Would be good an official project template with Typescript. I have not enough experience to create it. Can somebody help me with it?
Base template: https://github.com/moleculerjs/moleculer-template-project
Thank you!
I'm working on a base project based on the generated from the cli. Will upload it soon.
Ok, this is what I have:
https://github.com/DrecDroid/moleculer-typescript-example
I've discovered moleculer two days ago so I don't know if molecular-runner would work with typescript files because typescript imports works differently depending on how tsconfig.json paths are configured. The example works programatically, it tries to import from SERVICE_PATH env var in the ./broker.ts file.
You can run it like with the cli template:
docker-compose up -d --build
There is this other way that works by defining the compilation with tsc in the dockerfile and setting the SERVICEDIR to the outDir defined in tsconfig.json file, this way it could work with moleculer-runner and compilations errors would be shown during docker building instead of during runtime.
Thanks for this awesome project btw!
Great, thanks, I'm checking!
Would be better to use runner to start broker & services like in JS template. You can start it as
ts-node ./node_modules/moleculer/bin/moleculer-runner.js --repl services
Or node -r ts-node/register node_modules/moleculer/bin/moleculer-runner --hot --repl --mask **/*.service.ts services
I've create this other repo with another approach and this time starting from a clean project generated from the cli.
https://github.com/DrecDroid/moleculer-typescript-like-cli
Differences from the javascript template:
@types/node, typescript as dependencies, they can't be devDependencies otherwise it will not compile on productionoutDir is specified in tsconfig.json the compiled files will be placed alongside the originals inside the docker container. This makes everything work like in the original javascript template.Great, but please add NPM script to dev & compile
Hi, @icebob, https://github.com/katsanva/typescript-moleculer-bootstrap please check this.
This is a sample app with moleculer and typescript. The app can be easily run and build with docker. I can easily update it to match yours existing js template, if you wish :)
Thanks @katsanva, at first I try to use @DrecDroid repo because it's more similar as JS project template.
In the examples we don't have a method call. When we add a method and want to call it with a this.[methodname](), typescript will throw an error, cause typescript doesn't know the method
@DrecDroid Sadly your template is not working (At least, not for me). Anyone working on this?
It doesn't work with npm commands just with docker
I just ported moleculer-template-project to typescript in my repo https://github.com/faeron/moleculer-template-project-typescript and it should work the same as the official template.
What did I test/should work?
Let me know if you miss something. Also I'm not an TypeScript expert, so any improvements are welcome.
This is great! I'm testing this today!
i will give feedback soon as possible, thanks i really was interested in this!
Nice job @faeron, i will also check it!
For others: Test it with
moleculer init faeron/moleculer-template-project-typescript my-ts-projectcommand.
What I found:
npm run dev doesn't start services. The correct dev script is: "dev": "ts-node ./node_modules/moleculer/bin/moleculer-runner.js --hot --repl services/**/*.service.ts"npm run dev doesn't use the moleculer.config.ts file. Try to set namespace value and check the log messages. It prints: Namespace: <not defined>. So it starts with default options and doesn't use options in config file.Thanks for the feedback.
.js extension. But I would like to have a way without cluttering the project folder.moleculer.config.json(which would not support the options that are functions)."dev": "ts-node ./node_modules/moleculer/bin/moleculer-runner.js --hot --repl --config moleculer.config.ts services/**/*.service.ts" script.Ok, very nice. Both issues fixed then. Do you guys have anything else that needs a change?
A project-template is now in place. I guess this issue can be closed?
Yes. Thanks @faeron
- 0b9a720 will allow loading ts file too with
"dev": "ts-node ./node_modules/moleculer/bin/moleculer-runner.js --hot --repl --config moleculer.config.ts services/**/*.service.ts"script.
@icebob has this commit been released to npm?
Not yet. I will release it this week.
@faeron when use npm run dev, I can't open a dev server and get a error:
Not supported file extension: .ts Error: Not supported file extension: .ts
I think, ts filecan't load by "ts-node ./node_modules/moleculer/bin/moleculer-runner.js --hot --repl --config moleculer.config.ts services/**/*.service.ts
@Elvenisboy as stated in some comments above, the typescript template is dependent of the newer release of moleculer-runner (which is not out yet).
- 0b9a720 will allow loading ts file too with
"dev": "ts-node ./node_modules/moleculer/bin/moleculer-runner.js --hot --repl --config moleculer.config.ts services/**/*.service.ts"script.
But @icebob is already aware of this. See 2 comments above.
@faeron OK 馃憣 , I know. Thanks for your help 馃構
Sorry, but did somebody get methods to run in actions ? For me TS2339 is thrown, as Typscript will not understand that the methods are injected ?!
Most helpful comment
@icebob has this commit been released to npm?