Print.js: Module not found: 'print.js'

Created on 5 May 2017  Â·  28Comments  Â·  Source: crabbly/Print.js

I'm trying to import the lib in es6 like this:

import printJS from 'print.js'
printJS()

But I get:

Uncaught Error: Cannot find module "print.js"
    at webpackMissingModule

Tried it on the latest v1.0.18 version

Most helpful comment

@Kuchiriel I had the same issue, maybe this helps you as well:

// This import fails
import printJS from 'node_modules/print.js/src/index.js';

// This import works fine
import printjs from 'print.js/src';

// This import works fine
import printjs from 'print.js/dist/print.min.js';

All 28 comments

I need to rename the package to printjs, as print.js is creating this error. In the mean time, you can import using the full path to the node modules, print.js folder.

@crabbly I am trying to import the full path, and the problem stills.

import printJS from 'node_modules/print.js/src/index.js';

Works fine.

@SpencerCarstens Exactly. Importing the full path works fine. Thx for the feedback.
Once I have renamed the package it will be easier though. I'll post an update here soon.

@SpencerCarstens How to import it in Angular 2 ?

What I posted above should work fine... unless you are asking something more specific?

@Kuchiriel I had the same issue, maybe this helps you as well:

// This import fails
import printJS from 'node_modules/print.js/src/index.js';

// This import works fine
import printjs from 'print.js/src';

// This import works fine
import printjs from 'print.js/dist/print.min.js';

@sanderdebruijne, Thank you very much! <3

I am getting below error when i call PrintJS in Angular 2

Error: __WEBPACK_IMPORTED_MODULE_5_print_js_src__.a.printJS is not a function

I renamed the library to print-js. Try using npm install print-js --save instead.

I am getting the below error, please advice.

```
downloadFile(){
this.downloadService.downloadFile().subscribe(blob => {
FileSaver.saveAs(blob, "New File.pdf");
var pdfFile = new Blob([blob], {
type: 'application/pdf'
});
var pdfUrl = URL.createObjectURL(pdfFile);
PrintJS.printJS({ printable: pdfUrl, type: 'pdf', showModal: true });

  }
)

}
```

core.js:1427 ERROR TypeError: __WEBPACK_IMPORTED_MODULE_7_print_js___default.a.printJS is not a function

Hey @nsksaisaravana , you didn't post how you imported the library. However, just try printJS({ printable: pdfUrl, type: 'pdf', showModal: true }); instead. The library assigns the global variable printJS that you can use directly.

@crabbly Thanks a lot and works like charm, saved my day.

@crabbly All working good but i am getting this error.

ERROR DOMException: Failed to execute 'open' on 'XMLHttpRequest': Invalid URL

image

I am getting this error too. It works, but I really do not like to see errors in my console.

@nsksaisaravana @nicolas-schreiber , Thanks for pointing this error. It has been fixed. Run npm update print-js to get the new release. It was just published.
Please let me know if you still get any errors after that. Thx again.

@crabbly Yup, I still have the same Issue, but with Blobs:
screen shot 2018-02-08 at 16 13 29

@crabbly Awesome, well done and works like a charm !!!! Please support for all browsers.

@nsksaisaravana Hi, have you solved this problem? -- __WEBPACK_IMPORTED_MODULE_5_print_js___default(...) is not a function

@slyfalcon I was using Angular 5 and there were no issues but I don't know whether the current version is having the issue.

Hi @slyfalcon , make sure you are importing the library correctly.

import 'print-js'

@nsksaisaravana @crabbly thanks guys, for quick response. Well, I was using:

Ionic Framework: 3.9.2
Ionic App Scripts: 3.1.11
Angular Core: 5.2.11
Angular Compiler CLI: 5.2.11
Node: 8.11.1
OS Platform: macOS
Navigator Platform: MacIntel
User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36

Details:
I runned npm install print-js --save and import it correctly.

  1. code in xxx.html
    <button type="button" (click)="testPrint()"> print-jsondata </button>

  2. code in xxx.ts
    import printJS from 'print-js';
    export class XxxPage { testPrint() { printJS({printable: [{name:'alex', age:'11'}], properties: ['name', 'age'], type: 'json'}); } }

During running, I got the error:
Runtime Error:
__WEBPACK_IMPORTED_MODULE_7_print_js___default(...) is not a function

Hey @slyfalcon ,

The problem with your code is this: import printJS from 'print-js'

When you do that inside the same file where you are calling the function, you had replaced the global variable printJS with the module import, creating the above error.

Use import 'print-js' instead.
Or obviously, any other name, like for ex. import print form 'print-js'.

Thanks @crabbly , it works! Finally, it looks like this, (Note: please ignore the vscode warning)
import 'print-js'
...
printJS({ printable: [{ name: 'alex', age: '11' }], properties: ['name', 'age'], type: 'json' });
...

Just published a new version to npm which has proper support for module import.
The library will still expose a global variable printJS, but the recommend usage now should be to import the package at the same file where it will be used:

For ex.:

import printJS from 'print-js'

printJS('path-to.pdf')

Run npm update print-js to get the latest version.

Thanks @crabbly , it works! Finally, it looks like this, (Note: please ignore the vscode warning)
import 'print-js'
...
printJS({ printable: [{ name: 'alex', age: '11' }], properties: ['name', 'age'], type: 'json' });
...

@slyfalcon This is not a warning, this is an error and webpack fails to compile. Please suggest the solution.

Hi there you should try the last version.

Hi @kamal0912, like @slyfalcon suggested, make sure you are using the latest version.

About webpack, I have used the library with multiple projects without any compiling issues. Check your webpack configuration and your import.

Here is a Codesandbox example showing how to import the library, using either the local import or the global function.
https://codesandbox.io/s/x2yrj0kwww

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Orcinuss picture Orcinuss  Â·  8Comments

GuoSirius picture GuoSirius  Â·  8Comments

JahsonKim picture JahsonKim  Â·  5Comments

maisumakun picture maisumakun  Â·  5Comments

DouglasOGarrido picture DouglasOGarrido  Â·  3Comments