Open mbates opened 6 years ago
Some more info. I created a very simple node app, that just installs node-printer and sends some data to a printer using printDirect(). That worked fine.
I then added electron to the app, and created a simple main.js and renderer.js, then ran npm install printer --runtime=electron
the simple node app continued to work. But when npm start opens the electron window and the above error is displayed.
This is the node app, which works
// label-print.js
var net = require("net");
var printer = require("./node_modules/printer/lib");
function printNamed(printerName, rawCode){
printer.printDirect({
data: rawCode,
printer: printerName,
type: 'RAW',
success:function(){
console.log('printed');
},
error:function(err){
console.log(err);
}
});
}
function printNetwork(networkPrinterOptions, rawCode){
socketClient = net.connect(networkPrinterOptions, function () {
socketClient.write(rawCode);
});
}
printNamed(
'Zebra',
'^XA^MCY^XZ^XA^SZ2^MMT^MNY^LL210^PW615^LH0,0^CFR,15^FO170,80^FDNamed Printing^FS^XZ'
);
printNetwork(
{host: '192.168.0.210', port: 9100},
'^XA^MCY^XZ^XA^SZ2^MMT^MNY^LL210^PW615^LH0,0^CFR,15^FO170,80^FDNetwork Printing^FS^XZ'
);
This is the electron main.js, which works when line 3 is commented out. The renderer process outputs some version info for me as well: Node.js 8.9.3, Chromium 61.0.3163.100, and Electron 2.0.2.
// main.js
// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
var printer = require("./node_modules/printer/lib");
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow()
}
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
One more experiment, I set my package.json "main" from the eletron script to the node script. To the difference between the working script and the none working script is how I run it.
This works
node label-print.js
This gets me the error
npm start
This is the package.json
{
"name": "electron-print",
"version": "1.0.0",
"description": "Electron printing",
"main": "label-print.js",
"scripts": {
"start": "electron ."
},
"devDependencies": {
"electron": "^2.0.0",
"printer": "^0.2.2"
}
}
Hello, I have the same problem, I try to do .\node_modules.bin\electron-rebuild.cmd
Did you find any solution?
The solution is load the printer module using electron remote component
const { remote } = require('electron')
remote.require('printer').printDirect({})
Note: the printer module is only available in main process, if you need load a module from child process use remote.
Getting this error
webpack-internal:///78:12 Uncaught Error: Cannot find module "."
at webpackMissingModule (webpack-internal:///78:12)
at Object.eval (webpack-internal:///78:12)
at eval (webpack-internal:///78:327)
at Object.
@muyaedward You are using Electron or is a web page?
I am using electron vuejs
@muyaedward When you create a project in electron, you must be consider this: all electron apps have a main process and n+1 child process.
The main process contains all node modules (here lives the printer module) and the child process is more restricted (here live your Vue app). Electron offers an API that allow access to all modules from the child process: https://github.com/electron/electron/blob/master/docs/api/remote.md#passing-callbacks-to-the-main-process
If you need call the Printer module use the electron.remote.require
method:
const { remote } = require('electron')
remote.require('printer').printDirect({})
@muyaedward this works for me. https://github.com/Ronalses/printer-electron-epson-TM-T88V
Windows 7 x64 electron 2.0.2 node 8.9.3
I installed node_printer with
npm install printer --runtime=electron --target=2.0.2 --target_arch=x64
When I require printer/lib in my main process, the printer.js throws an exception as soon as it tries to require the node_printer.node file at
printer_helper = require(native_lib_path);
The message I get is Uncaught Exception: App threw an error during load Error: The system cannot find message text for message number 0x%1 in the message file for %2. \?\C:\Path\To\App\node_modules\printer\build\Release\node_printer.node at process.module.(anonymous function) [as dlopen] (ELECTRON_ASAR.js:172:20) at Object.Module._extensions..node (module.js:671:18) at Object.module.(anonymous function) [as .node] (ELECTRON_ASAR.js:172:20) at Module.load (module.js:561:32) at tryModuleLoad (module.js:504:12) at Function.Module._load (module.js:496:3) at Module.require (module.js:586:17) at require (internal/module.js:11:18) at Object. (C:\Path\To\App\node_modules\printer\lib\printer.js:10:2
2)
at Object. (C:\Path\To\App\node_modules\printer\lib\printer.js:326:
3)