Moleculer: Official Typescript project template

Created on 19 Jun 2018  路  27Comments  路  Source: moleculerjs/moleculer

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!

Enhancement help wanted

Most helpful comment

  1. 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?

All 27 comments

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:

  • Requires @types/node, typescript as dependencies, they can't be devDependencies otherwise it will not compile on production
  • The services and the moleculer.config files are all in typescript
  • Adds a typescript compilation step in Dockerfile, because no outDir 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?

  • generation of new template with moleculer-cli with existing questions (as transporters only nats and amqp was tested manually, the rest should work too because nothing there has been touched)
  • debug of services and tests in vscode (launch.json)
  • all npm scripts (test,ci,build,start)
  • docker build
  • docker-compose

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-project command.

What I found:

  1. 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"
  2. 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.

  1. I could not reproduce it (for me the script used the --mask option correctly), but with your suggested script it is also working, so I will use this.
  2. I will try to fix that somehow. A workaround/simple fix could be to compile the config file before running moleculer-runner and provide the file with .js extension. But I would like to have a way without cluttering the project folder.
    Another option would be the usage of moleculer.config.json(which would not support the options that are functions).
  1. https://github.com/moleculerjs/moleculer/commit/0b9a7208a088d63aa58ae9ff6c787993c1027e54 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.

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

  1. 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).

  1. 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 ?!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maitrucquynhq111 picture maitrucquynhq111  路  3Comments

ngraef picture ngraef  路  3Comments

ngraef picture ngraef  路  4Comments

thatisuday picture thatisuday  路  3Comments

abdavid picture abdavid  路  4Comments