perlservermojoliciousmorbo

Can't find application class in @INC when starting Mojolicious-generated app


I generated a mojolicious app with the command mojo generate app first-app. It generated the app structure that should allow a running example when running the dev server with morbo ./script/first-app.

The file structure looks like this: mojo app structure

My error when running morbo ./script/first-app in the root of the project directory is this error:

Can't load application from file "/home/djnorrisdev/Documents/mojo-practice/first-app/script/first-app":
    Can't find application class "first-app" in @INC. (
    /home/djnorrisdev/Documents/mojo-practice/first-app/lib
    /home/djnorrisdev/perl5/perlbrew/perls/perl-5.30.0/lib/site_perl/5.30.0/x86_64-linux
    /home/djnorrisdev/perl5/perlbrew/perls/perl-5.30.0/lib/site_perl/5.30.0
    /home/djnorrisdev/perl5/perlbrew/perls/perl-5.30.0/lib/5.30.0/x86_64-linux
    /home/djnorrisdev/perl5/perlbrew/perls/perl-5.30.0/lib/5.30.0)
Compilation failed in require at (eval 72) line 1

(Line breaks added for readability. An indented line is a continuation of the previous line.)

I tried using the full path for morbo (as mentioned in a 6 year old SO post), but that gives the same error as above. That command was this: /home/djnorrisdev/perl5/perlbrew/pls/perl-5.30.0/bin/morbo ./script/first-app

I'm guessing anyone familiar with a full mojolicious app would be familiar with the script file, but here's the contents of script/first-app :

#!/usr/bin/env perl

use strict;
use warnings;

use Mojo::File 'curfile';
use lib curfile->dirname->sibling('lib')->to_string;
use Mojolicious::Commands;

# Start command line interface for application
Mojolicious::Commands->start_app('first-app');

Considering this is a mojolicious-generated app, I would assume it should not get an @INC error and run with morbo without issue. Does anyone have insight into this?


Solution

  • For your setup to work, first-app.pm would have to contain package first-app;, but that's not legal code because of the dash. Håkon Hægland suggests that mojo generate app first-app should not have worked (presumably to avoid this very problem), so it's unclear how you came to have the setup you describe.

    To fix the problem, you could rerun mojo generate app with a more suitable name (such as FirstApp) and start over.

    Alternatively, you should theoretically be able to fix the problem with as little as three changes:

    1. Rename lib/first-app.pm to a more conventional name such as lib/FirstApp.pm.
    2. Change the argument passed to ->start_app to 'FirstApp'.
    3. Change the package directive in now-named lib/FirstApp.pm to package FirstApp;.

    A module's path, a module's package directive, and the use statement used to load the module —the value passed to ->start_app in this case— must all match. The three changes ensures this for the module in question.

    [Note: I have no experience with Mojo.]