concept module in category electron

This is an excerpt from Manning's book Electron in Action.
You’re welcome to use a build tool such as webpack or Browserify if you like, but it’s often not necessary in Electron applications because all of Node’s global properties (for example, require, module, and exports) are available in the browser content. You can use Node’s module system on what you’d traditionally think of as the client side without needing to add a build process to your application.
Listing 4.4. Importing Node’s fs module: ./app/main.js
const { app, BrowserWindow, dialog } = require('electron'); const fs = require('fs'); #1 app.on('ready',() => { ... }); #2 const getFileFromUser = () => { const files = dialog.showOpenDialog(mainWindow, { properties: ['openFile'] }); if (!files) { return; } const file = files[0]; #3 const content = fs.readFileSync(file).toString(); #4 console.log(content); };
Our main process is in charge of interfacing with the native operating system APIs. It’s in charge of spawning renderer processes, defining application menus, displaying Open and Save dialog boxes, registering global shortcuts, requesting power information from the OS, and more. Electron enforces this by making many of the modules needed to perform these tasks available only in the main process, as shown in figure 4.10.
Figure 4.10. Electron provides different modules to the main and renderer processes. These modules represent the code capabilities of Electron. This list is likely to grow and may be incomplete by the time you read this. I encourage you to visit the documentation to see the latest and greatest features.
![]()
Electron provides only a subset of its modules to each process and doesn’t keep us from accessing Node APIs that are separate from Electron’s modules. We can access a database or the filesystem from the renderer process if we want, but there are compelling reasons to keep this kind of functionality in the main process. We could potentially have many renderer processes, but we will always have only one main process. Reading from and writing to the filesystem from one of our renderer processes could become problematic; we could end up in a situation where one or more processes try to write to the same file at the same time or read from a file while another renderer process is overwriting it.
Electron does not include an ability to read files. Instead, we use Node’s fs module to read from and write to the filesystem.

This is an excerpt from Manning's book Cross-Platform Desktop Applications: Using Node, Electron, and NW.js.
NW.js provides access to the Node.js API in the app, as well as uses modules that are installed with npm. This means that you can install npm modules for use with your desktop apps, and you can even access them and Node.js core modules from the same code that’s interacting with the front end of the desktop app.
Structuring code into reusable libraries is an important part of any programming language’s ecosystem, and a good package system goes a long way toward helping developers be productive. In Node.js, groups of functions are organized into modules, and these modules can be easily created and reused in other places.
My personal preference is to not only track the package.json file of your app, but also lock down the version of the dependencies in that package.json file. To explain: When a Node.js module is added to the package.json file via npm install, you’ll notice that the dependency listing in the package.json will show a caret character (^) before the version number of the module. The ^ indicates to npm that when installing that dependency, it can install a more up-to-date version, so long as it’s compatible with the major version specified in the package.json file. As an example, say you install a version of CoffeeScript for use in your app:
npm’s shrinkwrap command will lock down the version of dependencies that are installed with the module. Running npm shrinkwrap in the same working directory as the package.json file will produce a file called npm-shrinkwrap.json, a JSON file that has configuration information to specify exactly what version of the module should be installed, which looks like this:
{ "name": "pkgjson", "version": "1.0.0", "dependencies": { "underscore": { "version": "1.8.3", "from": "underscore@", "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.8.3.tgz" } } }This file helps npm know exactly what versions of the software should be installed for the module.