Refactor the app.js file


Hello all,

I have a question about the app.js file in Jovo proyect. In this file I have all the intents and all the functions and I want to ask If I can separate all the code in this file in others files and in this way get a structure and a much more readable code.

Thank you very much in advance.




I try to structure my voice app in a way such that all intents have a separate file e.g. launchintent.js, welcomintent.js where each file contains an exported handler. These files are then imported and used in the app.setHandler() method in app.js.

I am using typescript btw and I don’t know if this is the best way to do it, but it provides quite a nice structure to the code.


Hi Michael,

Thanks for your answer!

I don’t understand this:

each file contains an exported handler.

How do you import a file with the intent in the app.js?



This should help:


Hi Jan!

Thank you for the tutorial but I have a doubt.

¿states is a new folder in the proyect at the same level as src, db, test, etc?

I have followed the tutorial and I have the following error:

Message: Cannot find module './states/Intent.js'



It would need to be a folder inside src


I have the following code:

const INTENT_1_STATE = require(’./states/Intent1.js’);
const INTENT_2_STATE = require(’./states/Intent2.js’);

    this.toStateIntent('INTENT_1_STATE', 'Intent1');
    this.toStateIntent('INTENT_2_STATE', 'Intent2');


But It doesn’t work. I want to launch the intents that have your file in the states folder.

Thank you in advance.


How does your ./states/Intent1.js’, for example?


module.exports = {
Intent1() {
if (isNotEmpty(this.$inputs.actuador) && this.$inputs.actuador.value !== ‘’) {, this.$inputs.actuador.value);

isNotEmpty() and YesOrNo() are functions found in the file > ./states/Intent1.js’


Seems like you haven’t added a state. This is why this.toStateIntent('INTENT_1_STATE', 'Intent1'); wouldn’t work


I have followed the tutorial like this:

The problem is to launch the two intents. If I launch only one intent, it works correctly, but if I launch the two intents in the app.setHandler() like this:

this.toStateIntent(‘INTENT_1_STATE’, ‘Intent1’);
this.toStateIntent(‘INTENT_2_STATE’, ‘Intent2’);

The program doesn’t work.


Intent redirects shouldn’t be done like this, rather like this with a return statement:

    return this.toStateIntent(‘INTENT_1_STATE’, ‘Intent1’);

There shouldn’t be another redirect happening. Each interaction (request) should only have one response


But then, in app.setHandler, can I only run one Intent? I couldn’t run Intent2 as well?


Could you explain the goal of this a little more? Typically, you wouldn’t run two intents in parallel, because you would only create one response per request.

More here:


Of course Jan, sorry if I’m not explaining myself well.

Imagine that Intent1 is used to make a hotel reservation and Intent2 to confirm if you want to do anything else after the reservation.

Then, when we launch the application, the user says:
“I want to make a reservation tomorrow” and the assistant answers: “Okay. Do you need anything else?” (Intent1).

The user answers: “Nothing else, thank you” and the assistant answers: “Have a good day!” (Intent2).

For that reason, I need both Intents to be executed because if I do that execution I get the following error:

Could not find the route “INTENT_2_STATE” in your handler function.

if I can only execute one intent like this:

return this.toStateIntent(‘INTENT_1_STATE’, ‘Intent1’);

I will have the error. That is why I had introduced the two intents in the app.setHandler().
If I have all the intents in the app.js file it doesn’t give me any error, but I wanted to refactor the code so that the app.js file wouldn’t be so big and so unreadable.

I hope you can understand me.



Have you read through the requests and responses docs? Also, I think before diving too deep into separating handlers into files, it would be great to get some more understanding of how voice interactions work in Jovo. The best way to understand how it works is to follow our Adventure Game tutorial (which has a similar concept, as it’s asking the user for input):


I think you still don’t understand me… :pensive:

That was an example.

I did that tutorial and I understand it. I have my application working properly. The only thing I wanted is that instead of having all the intents in the same file, I could separate each intent in each file and call them from app.js when necessary as said @michaelwapp , but I see that I can’t do it so I’ll have to leave all the intents in the app.js file even if it’s a file with many lines of code and difficult to read.


@alberto I answered your dm, but if anyone else is interested, here is how I structure my voice app.


Where one intent e.g. launchintent.ts looks like this:

import {Handler, Log} from 'jovo-core';

export const launchintent: Handler = {
    LAUNCH() {'Started LaunchIntent');
        return this.toIntent('WelcomeIntent');

and the app.ts looks like this:

// ------------------------------------------------------------------
// ------------------------------------------------------------------
import {App} from 'jovo-framework';
import {Alexa} from 'jovo-platform-alexa';
import {GoogleAssistant} from 'jovo-platform-googleassistant';
import {JovoDebugger} from 'jovo-plugin-debugger';
import {FileDb} from "jovo-db-filedb";
// ----
// ----
import {launchintent} from "./states/launchtintent";
import {welcomeintent} from "./states/welcomeintent";
// ------------------------------------------------------------------
// ------------------------------------------------------------------

const app = new App();

    new Alexa(),
    new GoogleAssistant(),
    new JovoDebugger(),
     new FileDb(),

// ------------------------------------------------------------------
// ------------------------------------------------------------------


export {app};


Hi, how can i pass parameters beween states?


you can do it with this.$data.someKey = someValue;

more here: