Aspex's avatar
Level 4

Getting started pulling in node-modules

Hi,

I'm totally new with Laravel's "dependencies", and this far I've only used native "laravel" -wrapped packages. For these packages the installation process has been simple: just "composer require" the laravel -modified package and add the service provider to the list and everything has just worked out of box.

However, now I'd need to start using PDFKit -package, which does not have laravel-wrapped package (http://pdfkit.org). I of course can complete the npm install step, but as the instructions go to the following part ...

"Creating a PDFKit document is quite simple. Just require the pdfkit module in your CoffeeScript or JavaScript source file and create an instance of the PDFDocument class.

PDFDocument = require 'pdfkit' doc = new PDFDocument"

... I'm a bit lost.

The question is: Where and how should I add that "PDFDocument = require 'pdfkit' -line in Laravel? I suppose I'd have to use bootstrap.js file, but that only leads into errors.

From where could I learn more about how to utilize node / js dependencies with Laravel?

Thank you,

0 likes
4 replies
lide's avatar

Maybe this tutorial helps you?

Add package to package.json file and then use npm install command. Offcourse you need node and npm installed your instance.

tomasz.r's avatar

Okay, first things first - require doesn't exist in browsers. If you dont use compilers, it won't work.

You need to have Node.js installed on your PC, then you need to compile your JS with a tool like Webpack (it's included with Laravel). Laravel also has this Laravel Mix, which makes things even easier.

You could for example add window.PDFDocument = require 'pdfkit' in your bootstrap.js file. Binding it to window lets you instantiate it anywhere (of course after binding it) as var doc = new PDFDocument.

You then compile your assets, they then probably end up in /public/js/app.js, you link to that script in your layout, and here you go.

Aspex's avatar
Level 4

Thank you for your answer. What can I possibly be missing cos I get a huge error message when adding that line to my bootstrap.js -file and run Gulp?

I do have the package installed to my node_modules -folder with npm install...

{ [Error: ./~/pdfkit/js/document.js Module not found: Error: Can't resolve 'fs' in '/home/vagrant/www/test/node_modules/pdfkit/js' resolve 'fs' in '/home/vagrant/www/test/node_modules/pdfkit/js' Parsed request is a module using description file: /home/vagrant/www/test/node_modules/pdfkit/package.json (relative path: ./js) Field 'browser' doesn't contain a valid alias configuration after using description file: /home/vagrant/www/test/node_modules/pdfkit/package.json (relative path: ./js) resolve as module /home/vagrant/www/node_modules doesn't exist or is not a directory /home/vagrant/node_modules doesn't exist or is not a directory /home/node_modules doesn't exist or is not a directory /node_modules doesn't exist or is not a directory /home/vagrant/www/test/node_modules/pdfkit/js/node_modules doesn't exist or is not a directory /home/vagrant/www/test/node_modules/node_modules doesn't exist or is not a directory /home/vagrant/www/test/node_modules/pdfkit/node_modules doesn't exist or is not a directory looking for modules in /home/vagrant/www/test/node_modules using description file: /home/vagrant/www/test/package.json (relative path: ./node_modules) Field 'browser' doesn't contain a valid alias configuration after using description file: /home/vagrant/www/test/package.json (relative path: ./node_modules) using description file: /home/vagrant/www/test/package.json (relative path: ./node_modules/fs) as directory /home/vagrant/www/test/node_modules/fs doesn't exist no extension Field 'browser' doesn't contain a valid alias configuration /home/vagrant/www/test/node_modules/fs doesn't exist .js Field 'browser' doesn't contain a valid alias configuration /home/vagrant/www/test/node_modules/fs.js doesn't exist .json Field 'browser' doesn't contain a valid alias configuration /home/vagrant/www/test/node_modules/fs.json doesn't exist [/home/vagrant/www/node_modules] [/home/vagrant/node_modules] [/home/node_modules] [/node_modules]

etc....

ejdelmonico's avatar

If the scripts you are trying to pull in with webpack are not ES6 or CommonJS modules, you will need to use the script-loader package. You will add those as entry points in the config. There is plenty of docs on the issue.

Please or to participate in this conversation.