Electron - Issue Resolving Electron Modules In Renderer Process - requirejs

I am having difficulty resolving the electron modules in my web application using Electron v0.32.3 using require. My understanding (although its not very clear in the docs) is that the modules are supposed to be automatically available to require of the application being run in the webview (examples include 'ipc' and 'remote'). I can see that they are there at runtime, but I am not sure how to access them:
I feel like there is some piece to this that I am missing. Other information: my web application is a Durandal 2x SPA that uses require to load modules already. Is there any other kind of setup that is required in the render process requirejs config to access these modules?

It turns out I just didn't understand all of the different processes going on. So with an application that is using a webview inside of a browser-window, there are actually three processes to be concerned about:
Main process - has access to node
Renderer process (browser window) - has access to node by default
Web view process - does not have access to node by default
I was seeing the node modules available to 2) and trying to use them in 3). The webview has the 'nodeintegration' attribute that can be used to enable this:
http://electron.atom.io/docs/v0.34.0/api/web-view-tag/#nodeintegration
However, using a preload script allows for exposing only the necessary node functionality with using nodeintegration:
http://electron.atom.io/docs/v0.34.0/api/web-view-tag/#preload
I went with that solution, setting up communication between the renderer process and the webview process.

Related

How to package server side JavaScript for Node

I have been using webpack to build a server side app using express. This code is harder to debug since I don't have the immediate comfort of a web browser and if I use something like VS Code to debug, it won't accept breakpoints inside request handlers when using source maps. Besides it takes no time to compile if I just stick to Node compatible JS and skip all transpiling and whatnot. Further, if I use treeshaking, I can reduce the size, but what is the point of that when it is running on the server (no client will ever download it).
My point is that I don't see why one would want to create a bundle of server side code if the server don't have any issues with memory or other limiting factors. It's easier to read, takes no time to compile and is easier to debug.
So a question. Is it ok to have the server app as an npm package and deploy that? Is that what is common or what do people do?
The project my team works on is a TypeScript monorepo that includes a web application and server-side daemon processes. We use webpack for producing build artefacts for all targets.
I've read some posts saying there's no point using webpack server-side because it was designed for web (obv), and I can understand that point of view.
However, even if our code was JavaScript and didn't require any transpiling, we'd still use webpack for the server-side. I want a single, minimal set of files and no node modules to deploy to the servers.
Size itself doesn't matter that much for server side (though every bit helps), but running 'yarn install' on servers is out of the question IMO.

React NodeGUI - Question about technology. Choosing Desktop Platform

I estimate possibility to write Report Desktop Application using React NodeGUI. There is some questions what I need to find answer before starting to develop by NodeGUI. Could you help me to figure out.
What way is to load program extension dynamicaly in React NodeGUI. For example: If I want to change business logic and visualization of my program without compile code. Is it possible?
What way is to load program extension dynamicaly in React NodeGUI. For example: If I want to change business logic and visualization of program adding some program module code of other developers. How way is it possible to do?
What way is to work with XML files in React NodeGui?
Is there possibility to run other program code written by C/C++ or other languages? What restriction of running non-native JS modules?
What way is to connect to SQL or NoSQL DB (PostgreSQL, Oracle, Mongo and etc)?
What are reasons yet to write or not write Desktop Applications by React NodeGUI?
React NodeGui is just another npm module that you would install in a nodejs program. It is a pure javascript library. It depends on NodeGui which is another regular npm module. NodeGui contains C++ code which acts as a nodejs addon. But for all purposes think of nodegui and react nodegui as regular npm modules. An app built with these libraries is a regular nodejs app.
What way is to load program extension dynamicaly in React NodeGUI. For example: If I want to change business logic and visualization of my program without compile code. Is it possible?
All business logic is written in javascript so any change in those would follow the same flow of how you would in a nodejs app.
What way is to load program extension dynamicaly in React NodeGUI. For example: If I want to change business logic and visualization of program adding some program module code of other developers. How way is it possible to do?
Yes you can use all npm modules available in npm that works with nodejs.
What way is to work with XML files in React NodeGui?
You can find a lot of npm modules you can use to parse and work with xml files in the npm registry.
Is there possibility to run other program code written by C/C++ or other languages? What restriction of running non-native JS modules?
Yes it is possible to run non js code by building them as NodeJs addons.
What way is to connect to SQL or NoSQL DB (PostgreSQL, Oracle, Mongo and etc)?
You have a lot of modules available in the npm registry for these which work with NodeJs.
What are reasons yet to write or not write Desktop Applications by React NodeGUI?
Reasons to use:
- NodeGui apps are native apps so they get all the benefits of native apps. They are snappy, they are accessible and run with low memory and cpu usage.
Reasons not to use:
Its under development so is only recommended for hobby projects as of Dec 2019.
PS: I am the author of the library

Angular + Electron app: query web API or DataBase according to deskop or web mode

I would like to build an app using Angular + Electron. My app should be able to run both on desktop and browser platforms. I'm considering to use angular-electron starter kit (but I'm open to other possibilities).
What concerns my is the way I read and write data. The data must be stored in a MySQL database. Ideally I would like to:
make the app call an api when NOT running on Electron (browser mode)
make the app query directly the mysql database when running on Electron (desktop mode)
I know I could check for window && window.process && window.process.type to let the app understand wheter running on Electron or not, however I'm a bit concerned about how to handle this. Also because I probably need to import node packages like mysqljs but ONLY in the desktop mode.
You can simply pass in different environment files in Angular during build this allow you to control the environment variables so that you can tell that it is a web app build or an electron app build.
https://angular.io/guide/build
EXTRA
But if you are importing binary packages this is where it gets tricky. I dont think there is a clean way for you to do conditional imports. I did not manage to find a way to do it cleanly and sort of maintained another repository for all my services that needs to import binary files.
To import the binary files you will also need to edit some webpack settings to tell angular to not compile/include the binary files during the build process so that you can use you librarys like mysqljs that require binary files. There is also some settings on the electron end to make binary files compatible for different platforms ie Windows, Mac , Linux. Basically it is really a pain to do it.
Link to how to edit webpack settings for angular 7+
https://github.com/manfredsteyer/ngx-build-plus
I will totally suggest you not to do it unless you really have a very good reason that you need to use these libraries.
EDIT 10/1/19
Okay I was referring to the MySqlJs but it seems like it does not have native modules modules. Native/binary modules basically means javascript code that relies on c++ compiled binaries(Or any native language like rust...).
For my case I was using the grpc modules which has a native dependency. Had to switch to grpc-web in the end.
I will add some footnotes here if you ever need them
https://electronjs.org/docs/tutorial/using-native-node-modules
Node.js / npm - anyway to tell if a package is pure JS or not?

node js - How do I create build for commercial usage?

I am working on node js application and it is now ready to use. I want to make exe of this application so that it can be used for commercial usage.
Up to now I have used enclose module using which I have compiled the code of application but I have found some issues in that (app got crash on idle condition). App is running good without enclose or compiled code.
I have searched on google and found some alternate modules like JXcore, Node webkit and Electron etc. but JX core giving error same as in SO question.
In node web-kit, it's functionality is not looking suitable as we need its executable and some dll's along with our code, which makes our package bulky.
I have also tried jxcore. The main problem with the exe's and with modules that we use is their ability to work with native modules, in my case the Kinect.node module. This module cannot be compiled. We need a workaround to package only this along with our .exe file. Enclose provides this workaround in its inbuilt functionality.
Also looking a response from EncloseJS, which is actually run by just one person who gives further instructions upon purchase. A purchase is needed for commercial usage.
In case of Electron, It is supporting only Electron-based application source code. So If I choose this then I have to modify my application code.
So can any one suggest me what can I do to make exe file from node js code there?
Thank you!
I had the same issue before, the node js application close when running in background. now i am using process manager2 (pm2), it is working fine and if the application is crash due to any other reason it is automatically started again.
I have gotten my answer:
First, reason was DiskDB database, it was not compatible with the node webkit so that is why I was getting error of native modules.
Now I am using sqlite3 module for local database. It is better than DiskDB.
Second, One reason was free version of enclose, Paid version of Enclose JS module ignores the timeout issue which I was getting.
This way I have resolved my question.

TypeScript + NW.js: need namespaces for browser, need commonjs for nodejs - How do I get both?

I'm writing an application using NW.js and TypeScript. I would like to use one class per file.
To access node.js modules, I need to use require(). However, the document context within a require()d module is no longer the browser's document context.
It seems to me that I need to stick to namespaces to be able to access the browser DOM from my application.
How can I combine those two module styles in one TypeScript application? When trying to use namespace together with require, my types suddenly lose visibility across files (because the compiler goes into external module mode.)
It seems to me that I need to stick to namespaces to be able to access the browser DOM from my application
No. You can use a module loader like webpack to bundle the portions that the browser needs into a single bundle.js while still keeping all your code base in commonjs mode.
More
A quickstart is available : https://basarat.gitbooks.io/typescript/content/docs/quick/browser.html
More More
I am actually creating an OSS editor that demonstrates this : http://alm.tools/. The whole project (backend + server) has a single tsconfig.json file.

Resources