Using NodeJS plugins in Electron - node.js

I am new to Electron (Atom-shell), and I am trying to load a NodeJS plugin into the application I am building, but I don't know how. The documentation is not clear on that.
For instance, I am trying to use sqlite3 plugin in my app, I used npm install sqlite3, and it was successfully installed. But the application throws and error when I try to call it var sqlite = require('sqlite3'). Are there any further steps I am not aware of ?
Thanks.

For pure JS (i.e. not native) modules you need the following:
Have the module listed in your package.json dependencies
Let electron know where to find the module (e.g. export NODE_PATH=/PATH/TO/node_module)
The first requirement is obvious and the second has its roots in this issue.
For native node modules (such as sqlite3) which use C++ bindings, you need to build them against electron headers to work. According to electron docs, the easiest way to do that would be:
npm install --save-dev electron-rebuild
# Every time you run npm install, run this
./node_modules/.bin/electron-rebuild

To install the npm modules correctly you should go into the folder of your electron app and install the module via npm.
npm install --save sqlite3
The flag --save is important, because npm will install the module inside your app.
Afterwards the require should work.

Related

node printer.node is not a valid win32 application

I have developed a node API for my angular application, My node application uses node-printer package for printing pdf files generated by node, when i tried to run my application using nodemon i am getting an error
node printer.node is not a valid win32 application
The same application is working on the other machine without any error. both the machines are of X64 bit architecture.
also i have also tried to install node js 32 bit then too i am getting same error.
This worked for me, installing printer as such:
npm install printer --build-from-source
After so many attempts i have deleted node_modules folder from an application and fire an npm install command to add all modules again that resolves the problem.
Hi there let me go through what's happened on my site here in 2022.
So if you're going to use npm install printer for your Electron application, you're using a native module as deemed by electron.
Since Electron has a different application binary interface (ABI) from a given Node.js binary that you used to install your printer . Therefore, we need to rebuild the native module (in this case printer) for Electron.
For more detail check this out.
First, install electron-rebuild for your project. npm i electron-rebuild.
Second, install a native module using e.g. npm i printer
Third, after installed native module, execute ./node_modules/.bin/electron-rebuild to rebuild the native module.

get error when trying to use twilio's npm package in meteor

I am trying to use the twilio node.js library with my meteor application. I was able to install the package using meteor npm install --save twilio
and it installed correctly. However, when I use import twilio from 'twilio'; I get this error
Error: Cannot find module 'crypto'(…)require # modules-runtime.js?hash=637cb12…:119meteorInstall.node_modules.twilio.lib.webhooks.js # modules.js?hash=9468cd4…:38774fileEvaluate # modules-runtime.js?hash=637cb12…:191require # modules-runtime.js?
Any thoughts on how to fix this. Seems to be a meteor error.
Normally, when you hit the command meteor npm install it should create a folder name meteor-node-stubs (/node_modules/meteor-node-stubs) in which the dependencies such as crypto etc. is arranged.
I think there is a problem with this folder in your project. My suggestion is to check whether you have this folder.
If you do not have it, you can try to install it by meteor npm install --save meteor-node-stubs That should solve your problem if this is the case

I don't know why the core modules in node js are not being recognized within my project using IntelliJ Idea (2016)

I keep getting the error: Cannot find module 'serve-favicon.' This error occurs not just for serve-favicon but for all core modules which also includes 'body-parser', 'cookie-parser', etc. I am using IntelliJ IDEA (version: 2016). I already enabled the Node.js Core library, and I have been able to use core modules in other projects successfully. What is different about this project however, is that I pulled it from github. Do I need to install another plugin? If yes, which one? Do I need to add another package?
serve-favicon, body-parser, cookie-parser are not node.js core modules.
You will need to install these dependencies from npm registry. To install these dependencies, you will need to run
npm install <package-name> --save
If you have pulled the project from github, chances are there will be a package.json file with a list of dependencies. In that case, all you need to do is run npm install from the project folder.

Module async not found

I'm studying node.js
and I am making the exercises from a book
https://github.com/marcwan/LearningNodeJS/blob/master/Chapter05/05_series.js
I have a problem with an example in which you invoke the module async.js
when I go to run the example I get the error
"can not find module 'async'"
in the folder where you installed node
I checked that there is a module
I also downloaded this package
https://github.com/caolan/async
and launched the test file that works properly
the first question that you do, even if it seems correctly installed the module, there is a command to verify that a module is installed and that you can recall?
the second question is, why is this wrong example?
thanks
To install a package, use npm install package_name.
When that's done, you can easily require that package and use it in your application.
const package = require('package_name');
To install a package globally (so you don't have to install it in every project you create) add -g flag
npm install package_name -g
You should be using npm, not downloading packages from github manually: npm install async.
npm will install the module into a node_modules subdirectory of the directory that you run it in. That directory must be your examples folder, or an ancestor. See: https://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders

cannot run a node.js example

I am trying to run a pre developed example in node.js environment. But i keep on getting the following error:
Cannot find module '/build/default/validation'
Am I missing some module installation from npm here...? I have installed all the other modules like socket-io, websocket, websocket-server, websocket-clien, etc.
-Parag
Check the package.json file for all required modules. Run npm install and npm update to automatically install all required modules and update them.
Usually, npm modules are in a folder *node_modules*, so /build/default/... looks like some custom module that should be shipped with the example.

Resources