1

I am trying to use a npm package in my laravel5 app, but I am not aware with node and require, can someone please help me to understand that how can I use the npm packages.

I am trying to use the following package:

https://www.npmjs.com/package/node-movie

Here is my code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script data-main="{!! url() !!}/vendor/lib/require.js" src="{!! url() !!}/vendor/lib/require.js"></script>
<script type="text/javascript">

            var imdb = require('imdb-api');
            var movie;
            imdb.getReq({ name: 'The Toxic Avenger' }, function(err, things) {
                movie = things;
            });
            console.log(movie);
        </script>

I am getting the following error in console:

Uncaught Error: Module name "imdb-api" has not been loaded yet for context: _. Use require([])
Anshul Mishra
  • 1,706
  • 2
  • 15
  • 38

1 Answers1

1

First thing:

You're trying to use node on the clientside; this means you don't know what node is.

To get started with it: How do I get started with Node.js

Second:

You should understand what asynchronous means in javascript and node and how to work with it. Your console.log(movie) will log undefined every time even if your code works.

You can search for articles pertaining to async in node; here's an example article: https://www.codementor.io/nodejs/tutorial/manage-async-nodejs-callback-example-code

Third:

To use node with laravel (after you learn how to use node) you can start up a node server that works as a REST API to provide your laravel app with what you require from node packages (you 'request' the information from the node server).

Easier alternative:

Find a pure javascript implementation for that npm package (i.e. search for a solution that doesn't involve node).

Community
  • 1
  • 1
Kesarion
  • 2,808
  • 5
  • 31
  • 52