Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot find module 'serialport' on build with cordova-electron #2715

Open
laux98 opened this issue Oct 10, 2023 · 0 comments
Open

Cannot find module 'serialport' on build with cordova-electron #2715

laux98 opened this issue Oct 10, 2023 · 0 comments

Comments

@laux98
Copy link

laux98 commented Oct 10, 2023

SerialPort Version

12.0.0

Node Version

18.16.1

Electron Version

25.2.0

Platform

Ubuntu 22.04

Architecture

x64

Hardware or chipset of serialport

No response

What steps will reproduce the bug?

Hello, my problem is when I include the library SerialPort in my cordova-electron projet, build crashe on windows 10 , window 11 and Ubuntu 22.04.

In the file "pathToMyProjetct/cordova/platforms/electron/platform_www/cdv-electron-main.js", I include the line
const { SerialPort } = require("serialport");
to use the library.

What happens?

Build don't launch and error is displayed.

A JavaScript error occurred in the main process
Uncaught Exception:
Error: Cannot find module 'serialport'
Require stack:
- /pathToMyProject/cordova/platforms/electron/build/linux-unpacked/resources/app.asar/cdv-electron-main.js
- 
    at Module._resolveFilename (internal/modules/cjs/loader.js:892:15)
    at Function.n._resolveFilename (electron/js2c/browser_init.js:245:1128)
    at Module._load (internal/modules/cjs/loader.js:737:27)
    at Function.f._load (electron/js2c/asar_bundle.js:5:12913)
    at Module.require (internal/modules/cjs/loader.js:964:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/pathToMyProject/cordova/platforms/electron/build/linux-unpacked/resources/app.asar/cdv-electron-main.js:27:24)
    at Module._compile (internal/modules/cjs/loader.js:1083:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1113:10)
    at Module.load (internal/modules/cjs/loader.js:940:32)

What should have happened?

Build launch and I can use serial port.

Additional information

I am trying to use the serial port library on cordova-electron.
When i use
cordova run electron --nobuild , this work perfectly and the application is launch and I can use serialport.
When i want to do the build, I use
cordova build androidand there are no errors.
But when I launch the build , this error is displayed :

A JavaScript error occurred in the main process
Uncaught Exception:
Error: Cannot find module 'serialport'
Require stack:
- /pathToMyProject/cordova/platforms/electron/build/linux-unpacked/resources/app.asar/cdv-electron-main.js
- 
    at Module._resolveFilename (internal/modules/cjs/loader.js:892:15)
    at Function.n._resolveFilename (electron/js2c/browser_init.js:245:1128)
    at Module._load (internal/modules/cjs/loader.js:737:27)
    at Function.f._load (electron/js2c/asar_bundle.js:5:12913)
    at Module.require (internal/modules/cjs/loader.js:964:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/pathToMyProject/cordova/platforms/electron/build/linux-unpacked/resources/app.asar/cdv-electron-main.js:27:24)
    at Module._compile (internal/modules/cjs/loader.js:1083:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1113:10)
    at Module.load (internal/modules/cjs/loader.js:940:32)

The line "cdv-electron-main.js:27:24" is

const { SerialPort } = require("serialport");

Steps to reproduce :

Create a projet with cordova-electron

npm i -g cordova
cordova create sampleApp
cd sampleApp
cordova platform add electron

Add serialport

npm i serialport

Update sampleApp/platforms/electron/platform_www

#Add this in top of file
const { SerialPort } = require("serialport");
#And after all the const declarations, add this
console.log("hello serialport : " , SerialPort);

Try it with

cordova run electron --nobuild

That work and the console.log is make.

Build the project with

cordova build electron

There are no errors

But when you try to open the executable (for exemple in window, it is
in /sampleApp/platforms/electron/build/win-unpacked/HelloCordova.exe, this error message is displayed

A JavaScript error occurred in the main process
Uncaught Exception:
Error: Cannot find module 'serialport'
Require stack:
- /pathToMyProject/cordova/platforms/electron/build/linux-unpacked/resources/app.asar/cdv-electron-main.js
- 
    at Module._resolveFilename (internal/modules/cjs/loader.js:892:15)
    at Function.n._resolveFilename (electron/js2c/browser_init.js:245:1128)
    at Module._load (internal/modules/cjs/loader.js:737:27)
    at Function.f._load (electron/js2c/asar_bundle.js:5:12913)
    at Module.require (internal/modules/cjs/loader.js:964:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/pathToMyProject/cordova/platforms/electron/build/linux-unpacked/resources/app.asar/cdv-electron-main.js:27:24)
    at Module._compile (internal/modules/cjs/loader.js:1083:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1113:10)
    at Module.load (internal/modules/cjs/loader.js:940:32)

I have the same issue in Windows 10, Windows 11 and Ubuntu22.04.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant