Printing in Node.js with Electron (ElectronForge + node-printer + webpack)

If you are facing some problems with node-printer in an electron application — in development time or even during install — this post may offer you help.

So, printing in node.js, huh?
Actually using node-printer in node.js alone shall be an easy task, the challenge starts when you try the same approach in an electron app (or maybe I’m just an unlucky guy).

About the library

At the moment I write this to you, the only existent library for printing in node, as far as I know, is node-printer. The bad news is that it is an unmaintained lib, but the good news is that we have a great community and a lot of forks 😊.

The most maintained fork, again, as far as I know and at this moment, is @thiagoelg/node-printer. So now on when referring to the term “node-printer” I’ll be talking about this fork.

The first level

Not yet talking about our main purpose of the post, but already talking about caveats of node-printer with electron, we have to be careful with the compatibility between the versions of the 3 characters in game: node-printer, node.js and electron. You have to use a node.js version that works either with node-printer version and with the electron version that you will use.

Since node-printer is an older lib than electron, you probably should start matching its compatibility with node.js — you can search in the commits of the lib — and then find a fitting electron version (most of latest versions should work).

Matching the versions you should be able to get node-printer installed. To get it running you’ll need one more step — only required if not using Electron Forge, since it handles it automatically— about native modules in electron (the electron-rebuild in postinstall script thing).

The second level, and the boss already

The real point of this post comes when we want to use our dear node-printer with Electron Forge template with webpack (in a react app for example). You’ll see that even doing everything we saw above you’ll still get a “can’t find module node-printer” (or something like that).

It happens the webpack build does not handle rightly the node_printer.node file inside the lib package. And to solve that we have to do two different configs, so it can be fixed in dev mode as well in production mode.

To get it working in dev mode first you’ll have to make webpack ignore the whole lib package, and that’s simple, just add that to your main.webpack.js:

...
externals: {
  '@thiagoelg/node-printer': 'commonjs @thiagoelg/node-printer',
},
...

As far as my research took me, the problems occours when webpack transpiles some internal paths of the lib to correspond to the new build folder called “.webpack”, making the package external will prevent webpack from doing that and the package will be able to recover its paths from node_modules normally.

Now, to solve in production mode it’s a bit more specific, the problem is that our now external packages will not be added to the “.webpack” folder after build, by that not being added to the asar file in the end of the process of executable generation.

So we manually add those files, firstly installing copy-webpack-plugin and than adding the below code to main.webpack.js:

const CopyPlugin = require('copy-webpack-plugin');
module.exports = {
...
  plugins: [
new CopyPlugin({
      patterns: [
        {
          from: 'node_modules/@thiagoelg/node-printer/lib/',
          to: 'node_modules/@thiagoelg/node-printer/lib/',
        },
        {
           from: 'node_modules/@thiagoelg/node-printer/build/',
           to: 'node_modules/@thiagoelg/node-printer/build/',
         },
         {
            from: 'node_modules/@thiagoelg/node-             printer/package.json',
            to: 'node_modules/@thiagoelg/node-printer/package.json',
},
      ],
    }),
  ],
...
}

In a sequence of tryings and fails I discovered that only “lib/”, “build/” and “package.json” are needed to be copied to the final build, which makes sense, I guess.

Now you — I hope — can run your make script and then execute the application and see no errors.

That’s all, thank you for giving me some minutes of your time.
PS: thats my second post, I beg your pardon for any dumby thing (in the third one I’ll stop giving that excuse)


Printing in Node.js with Electron (ElectronForge + node-printer + webpack) was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Lucas Hang

If you are facing some problems with node-printer in an electron application — in development time or even during install — this post may offer you help.

So, printing in node.js, huh?
Actually using node-printer in node.js alone shall be an easy task, the challenge starts when you try the same approach in an electron app (or maybe I’m just an unlucky guy).

About the library

At the moment I write this to you, the only existent library for printing in node, as far as I know, is node-printer. The bad news is that it is an unmaintained lib, but the good news is that we have a great community and a lot of forks 😊.

The most maintained fork, again, as far as I know and at this moment, is @thiagoelg/node-printer. So now on when referring to the term “node-printer” I’ll be talking about this fork.

The first level

Not yet talking about our main purpose of the post, but already talking about caveats of node-printer with electron, we have to be careful with the compatibility between the versions of the 3 characters in game: node-printer, node.js and electron. You have to use a node.js version that works either with node-printer version and with the electron version that you will use.

Since node-printer is an older lib than electron, you probably should start matching its compatibility with node.js — you can search in the commits of the lib — and then find a fitting electron version (most of latest versions should work).

Matching the versions you should be able to get node-printer installed. To get it running you’ll need one more step — only required if not using Electron Forge, since it handles it automatically— about native modules in electron (the electron-rebuild in postinstall script thing).

The second level, and the boss already

The real point of this post comes when we want to use our dear node-printer with Electron Forge template with webpack (in a react app for example). You’ll see that even doing everything we saw above you’ll still get a “can’t find module node-printer” (or something like that).

It happens the webpack build does not handle rightly the node_printer.node file inside the lib package. And to solve that we have to do two different configs, so it can be fixed in dev mode as well in production mode.

To get it working in dev mode first you’ll have to make webpack ignore the whole lib package, and that’s simple, just add that to your main.webpack.js:

...
externals: {
  '@thiagoelg/node-printer': 'commonjs @thiagoelg/node-printer',
},
...

As far as my research took me, the problems occours when webpack transpiles some internal paths of the lib to correspond to the new build folder called “.webpack”, making the package external will prevent webpack from doing that and the package will be able to recover its paths from node_modules normally.

Now, to solve in production mode it’s a bit more specific, the problem is that our now external packages will not be added to the “.webpack” folder after build, by that not being added to the asar file in the end of the process of executable generation.

So we manually add those files, firstly installing copy-webpack-plugin and than adding the below code to main.webpack.js:

const CopyPlugin = require('copy-webpack-plugin');
module.exports = {
...
  plugins: [
new CopyPlugin({
      patterns: [
        {
          from: 'node_modules/@thiagoelg/node-printer/lib/',
          to: 'node_modules/@thiagoelg/node-printer/lib/',
        },
        {
           from: 'node_modules/@thiagoelg/node-printer/build/',
           to: 'node_modules/@thiagoelg/node-printer/build/',
         },
         {
            from: 'node_modules/@thiagoelg/node-             printer/package.json',
            to: 'node_modules/@thiagoelg/node-printer/package.json',
},
      ],
    }),
  ],
...
}

In a sequence of tryings and fails I discovered that only “lib/”, “build/” and “package.json” are needed to be copied to the final build, which makes sense, I guess.

Now you — I hope — can run your make script and then execute the application and see no errors.

That’s all, thank you for giving me some minutes of your time.
PS: thats my second post, I beg your pardon for any dumby thing (in the third one I’ll stop giving that excuse)


Printing in Node.js with Electron (ElectronForge + node-printer + webpack) was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Lucas Hang


Print Share Comment Cite Upload Translate Updates
APA

Lucas Hang | Sciencx (2022-06-04T01:35:03+00:00) Printing in Node.js with Electron (ElectronForge + node-printer + webpack). Retrieved from https://www.scien.cx/2022/06/04/printing-in-node-js-with-electron-electronforge-node-printer-webpack/

MLA
" » Printing in Node.js with Electron (ElectronForge + node-printer + webpack)." Lucas Hang | Sciencx - Saturday June 4, 2022, https://www.scien.cx/2022/06/04/printing-in-node-js-with-electron-electronforge-node-printer-webpack/
HARVARD
Lucas Hang | Sciencx Saturday June 4, 2022 » Printing in Node.js with Electron (ElectronForge + node-printer + webpack)., viewed ,<https://www.scien.cx/2022/06/04/printing-in-node-js-with-electron-electronforge-node-printer-webpack/>
VANCOUVER
Lucas Hang | Sciencx - » Printing in Node.js with Electron (ElectronForge + node-printer + webpack). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/06/04/printing-in-node-js-with-electron-electronforge-node-printer-webpack/
CHICAGO
" » Printing in Node.js with Electron (ElectronForge + node-printer + webpack)." Lucas Hang | Sciencx - Accessed . https://www.scien.cx/2022/06/04/printing-in-node-js-with-electron-electronforge-node-printer-webpack/
IEEE
" » Printing in Node.js with Electron (ElectronForge + node-printer + webpack)." Lucas Hang | Sciencx [Online]. Available: https://www.scien.cx/2022/06/04/printing-in-node-js-with-electron-electronforge-node-printer-webpack/. [Accessed: ]
rf:citation
» Printing in Node.js with Electron (ElectronForge + node-printer + webpack) | Lucas Hang | Sciencx | https://www.scien.cx/2022/06/04/printing-in-node-js-with-electron-electronforge-node-printer-webpack/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.