Al Ex Tsm
Al Ex Tsm

Reputation: 2102

Mojolicious + MongoDB: Can't locate MongoDB.pm error

My app crashes each time it includes as much as "use MongoDB;" in my perl app file.

I have installed MongoDB successfully. I can check my databases use one or the other, check for collections, create new collections, all from the shell.

If I try to connect to mongoDb from mojolicious app like:

!/usr/bin/env perl

use Mojolicious::Lite;
use MongoDB;
use MongoDB::OID;

my $mongo_port = shift || 27017;

helper 'mongo' => sub {
    my ($self, $name) = @_;
    my $host = 'localhost:' . $mongo_port;
    my $conn = MongoDB::MongoClient->new(host => $host);
    my $db = $conn->get_database('test');
};

helper 'value2oid' => sub {
    my ($self, $value) = @_;
    MongoDB::OID->new($value);
};

If I have a working app and include as much as :

Use MongoDB;

I get:

Can't load application from file "/Users/eevitomperi/Desktop/Programming/Perl/mojoliciousApp/foodAbout/app.pl": Can't locate MongoDB.pm in @INC (you may need to install the MongoDB module) (@INC contains: /Library/Perl/5.18/darwin-thread-multi-2level /Library/Perl/5.18 /Network/Library/Perl/5.18/darwin-thread-multi-2level /Network/Library/Perl/5.18 /Library/Perl/Updates/5.18.2/darwin-thread-multi-2level /Library/Perl/Updates/5.18.2 /System/Library/Perl/5.18/darwin-thread-multi-2level /System/Library/Perl/5.18 /System/Library/Perl/Extras/5.18/darwin-thread-multi-2level /System/Library/Perl/Extras/5.18 .) at /Users/eevitomperi/Desktop/Programming/Perl/mojoliciousApp/foodAbout/app.pl line 4.
BEGIN failed--compilation aborted at /Users/eevitomperi/Desktop/Programming/Perl/mojoliciousApp/foodAbout/app.pl line 4.

I am completely new to mongo, mojolicious and perl so I guess I did not install some package?

Does the MongoDB files(mongo, mongod....) have to be within the mojolicious project ?

Not sure what I am missing and all documentation starts with the use of "Use MongoDB;" within mojolicious app so not sure what to do.

Hopefully someone can point out what I missed.

Upvotes: 0

Views: 1613

Answers (1)

Al Ex Tsm
Al Ex Tsm

Reputation: 2102

Install module:

cpanm Mojolicious::Plugin::Mongodb

Fix the following:

Can't write to /Library/Perl/5.18 and /usr/local/bin: Installing modules to /Users/eevitomperi/perl5
! To turn off this warning, you have to do one of the following:
!   - run me as a root or with --sudo option (to install to /Library/Perl/5.18 and /usr/local/bin)
!   - Configure local::lib your existing local::lib in this shell to set PERL_MM_OPT etc.
!   - Install local::lib by running the following commands

By running:

cpanm --local-lib=~/perl5 local::lib && eval $(perl -I ~/perl5/lib/perl5/ -Mlocal::lib)

Now I can connect to Mongo from mojolicious

Upvotes: 1

Related Questions