Although Node is known as a server platform, interest in using it to create Windows OS desktop applications is growing rapidly. Bindings exist for GUI toolkits such as GTK +, QT, and Cocoa. However, one of the advantages of Node for web development is the ability to use the same language, both on the server and on the client. It works well with a web browser shell to develop cross-platform JS Windows desktop applications and games using WebGL with HTML, CSS, and JS.
A brief history of code
JavaScript has always been a programming language or, rather, a script on the Internet. It was first introduced in 1995 and was not called JS Windows then. During his early development, he bore the name Mocha, which was a pun in Java, the most popular programming language for web servers of the time. It was then changed to JavaScript in the Netscape Navigator web browser. Finally, it was later renamed JavaScript with a very limited scope.
Thanks to the improvements made to the ECMAScript specification, JS has become a modern browser programming language. Due to its accessibility and ubiquity, it has become one of the three pillars of the Internet along with HTML and CSS. However, the current level of use of IT technology requires launching outside the browser.
So, in 2009, on the initiative of Ryan Dahl, Node was born - a free, open-source JavaScript runtime that allows you to run JS outside the browser in the form of JS Windows. The written JS-code works in any OS in which Node is installed, which makes the cross-platform aspect of the Internet accessible to everyone.
Principle of operation
Node.js provides APIs that are not available in the browser environment. They have fewer restrictions. For example, the file system API is used to access and manage the OS. It provides the developer with the basic CRUD operations: create, read, update, and delete. For security reasons, this cannot be done in browsers. In essence, JavaScript is becoming a more powerful and dangerous application.
There are times when the user needs to include external code in his own program. For example, if you use the removed tag when using jQuery, it imports the functionality of the application. The same goes for regular scripts that users write on their own. You can simply import any JS file with the removed tag. However, this can become cumbersome and will have to manually manage hundreds of dependencies.
In this case, Node.js comes to the rescue. How? Before installing Node JS on Windows or another OS, the user will not need any device upgrades, as the software provides an easy way to manage dependencies. It comes with a package manager (NPM). Through it, external data can be imported into user projects as dependencies.
Scopes of packages:
- Servers.
- Database management.
- Web applications.
- Desktop and mobile applications.
- Robotics.
Characteristics:
- Fast code execution.
- High scalability.
- Multifunctionality and asynchrony.
- Lack of buffering.
Comparison: Node.js and JavaScript
Browsers have a built-in interpreter for JS. Node is both an environment for it, it includes a set of libraries for using JS as a general-purpose programming language with a focus on asynchrony and non-blocking operations. In fact, the code runs the same interpreter as Google Chrome (V8), but provides a different set of libraries and a different runtime. It also includes a package management system (NPM) and several language extensions that are not found in browsers, such as modules.
Thus, the JS interpreter in Google Chrome and the similar JS application program for Windows in Node are essentially the same. The difference is that in the browser, the ultimate goal is to change the contents of the web page - text, graphics, style sheets, and others, and in Js - the launch of general purpose code that can perform any action (from activating the web server to manipulating files).
Desktop Utilities
Any existing web application is suitable for becoming one. You can create it on the basis of code that will benefit from integration into the user's system, starting with notifications at startup and ending with interaction with files and other necessary programs. Some simply prefer to have certain applications permanently on their machine, available regardless of whether they have a connection or not.
It is easy to create powerful desktop utilities using web technologies, for example, with NW.js. Such programs have existed for a long time. In Shanghai in 2011, Roger Wang of Intel's Open Source Technology Center wrote node-webkit, a concept validation module that allows the user to create a browser window and Node.
After some progress and transition from WebKit and Chromium, the open source software on which Google Chrome is based, Chen Zhao joined the project. Together they created an application runtime on Node and Chromium. The node-webkit project became quite popular and was later renamed to NW.js.
By downloading it, the user actually runs Chromium plus Node.js, as well as the application code itself. This not only means that desktop software can be created using HTML, CSS, and JavaScript, but it will also have access to all Node APIs, such as reading and writing to disk.
Both projects are based on Chromium and Node, so you can write desktop applications using it at the system level and web technologies for the GUI level.
Electron has some advantages:
- More popular among developers.
- Has the best corporate support. Github and Microsoft are behind Electron, and NW.js is sponsored by Intel.
- Offers an automatic update mechanism out of the box.
- The process of assembling it is a little easier, because you do not need to combine the entire Chromium.
Advantages of NW.js:
- Legacy system support: Windows XP and older versions of Mac OS. Despite the fact that Microsoft has refused support for XP, but many users still use it, so they really need such a service.
- An application master record can be either an HTML page or a Node.js.
- It is more browser oriented.
- The approach to it is much simpler and more understandable. This gives users many options.
- Separate and mixed contexts.
- It has a simple but powerful mechanism in relation to the JavaScript context, while Electron has two separate main and rendering contexts that are completely separate.
- Chrome Extensions and Chrome API Support. Moreover, you can run Chrome applications and extensions using NW.js. "Electron" cannot and will not do this.
- Supports out-of-box PDF files using the native Chrome PDF plugin.
- Has a source protection tool. This allows you to compile JavaScript into V8 bytecode.
- Uses Latest Chromium
- You can use software without Dev Tools.
- Supports Chromium command line switches. For more information, refer to the NW.js documentation. These switches can be used to tune performance.
- The best security model for desktop applications. This one in Chrome (and web browsers in general) is a bit limited due to the nature of the user experience for websites. NW.js provides another model that circumvents all security restrictions, such as, for example, the sandbox.
Getting started with the program
You can download two versions of Node.js. At the initial stage, it does not matter which one to choose. The long-term support version is considered the most stable. It is updated only when necessary to correct errors and basic maintenance. This version is preferable in a production environment and much more stable. It is often supplied with new features.
Thanks to a faster update cycle, the software has the most advanced APIs and technologies for developers. After going through the installation process, you can verify it by opening a command prompt.
When the program is activated, you can start the JavaScript interpreter. Node currently allows you to write JS in the console in DevTools, on the command line. In order to exit the interpreter, press “Ctrl + C” twice and enter exit.
You can enter all the JavaScript you need in the interpreter. First make sure that the user is in the directory with the file, write: cd / path / to / the / file and wait until the desired directory is opened. Then they type “node” to run the file. For example, “node filename.js” is written to activate “filename.js”. You can omit the .js file extension by typing “node filename”. Node implies that “filename” is a JS file.
NPM Application Basics
Node.js allows JavaScript to run outside the browser. To help manage external dependencies, the installation comes with NPM. Besides storing project metadata, the package.json file also tracks these dependencies. The official Node documentation is the main reference material for everything related to software.
The code can be divided into imported and exported modules. NPM provides an easier way to manage dependencies. Let's say you need to use Lodash in your code. Initialize a new project by running npm init in the project directory. Upon request, fill out the necessary information about package.json. All metadata is stored here.
Download Lodash
Assuming the user is in the correct directory, do this by running npm install lodash. The dependency is loaded into a folder named node_modules. It tends to accumulate a lot of files as more dependencies are introduced into the project. After installing Lodash, they create a JS file in the current directory that Lo Dash uses. Lodash is imported into it by calling the require function.
As mentioned earlier, Node.js provides an API. This is a built-in module that comes with every installation. You can import this code by also calling the require function. Running the JS file will result in writing Hello World to text named dev to.txt.
This fs.writeFile method is just one of many fs functions offered by the module. All available API methods and properties are described in the documentation.
Export Modules
If you need to import your own code from different files, JS first export the JavaScript file code. Then, using the require function, load it into another. In fact, this is not as confusing as it seems.
Say there are four JS files in the same directory as index.js, add.js, username.js and dog.js. The task of add.js is to provide a function that returns the sum of two input data. You can export it by assigning the add parameter to the module.exports property.
The work of username.js is to export the string Some Dood. Or whatever. This means that you can assign the value of any type to module.exports. This means that they export strings, numbers, booleans, characters, functions, classes, arrays, and objects.
You can call these three index.js files using the require function. Absolute or relative paths to them must be provided in order to explicitly indicate to Node.js that they are importing from files, not from the built-in modules or from the node_modules of the project folder, and you can omit the .js extension. The “required” function returns the actual value that was assigned to the corresponding module.exports file.
Install and run the application
A very simple first step into the JS world is performed using the command line and writing the first bit of Node code.
Windows JS script launch algorithm:
- Download the latest version of the program, for example, V6.2.2.
- Install Node npm install by following the instructions.
- After activation is complete, find the icon in the Windows search option and open the program.
- Now you can continue, do some calculations and try to print messages on the console.
- Actions will show that the node has been successfully installed in the system and is working properly.
- Press Ctrl + C twice to exit the node and get the directory path again.
- Configure the Windows command line environment for the application.
- Download and run the installation from the JS Windows open developer site.
- Run cmd.exe.
- Press Windows + R on the keyboard.
- Type cmd and press enter.
- Check if the node is installed successfully by typing node -v, it should answer “v #. #. # ", Where" # "means a number
- Restart the computer if the -v node does not respond correctly.
- They try to automatically install all the dependencies using the npm install command.
- They start the server by executing the “node application” in the server root folder.
If the previous step failed, manually install the dependency by running the following commands:
- "npm install";
- "npm install express -g";
- "npm install url -g";
- "npm install fresh -g";
- "npm install cookie -g";
- "npm install crc -g";
- "npm install send -g";
- "npm install connect -g";
- "npm install commander -g".
Configuring Node JS Windows 10
Developing using js for Windows used to be rather cumbersome, but now it has become much easier. The network has the latest tools and applications with the ability to use the 64-bit version.
Installation Algorithm:
- Install Git using the default settings.
- The installation location selects the Projects folder in the home directory, if it is not, then create it for the project first.
- Right-click on the folder, select Git bash here and check the git version, writing: git-version.
- Create a .bash_profile that will execute when the bash window opens. This is not a cmd.exe window, so check the parameters. Click on the icon in the upper left corner. You can insert text using the middle mouse button, as on popular Linux terminals.
- Install Node.js on Windows 10 using the LTS version. It is not recommended to activate several versions at the same time, since the node manager (NVM) does not officially support Windows, but, nevertheless, there are alternatives, such as nvm-windows or nodist.
- Node ships with npm, the package manager that is available after installation.
- They open the bash shell and check the versions with: npm --version. If npm is 2.x, you should upgrade it to version 3, which solves many problems, for example, handles peer dependencies.
- Find Powershell on the Start menu, run it as administrator, and install npm, the package manager on Windows 10.
- Activate Visual Studio and Python. Node packages usually depend on packages with their own code, so you need to install Visual Studio. Node-gyp is a wrapper for Python GYP. It generates project files for Gcc, Xcode and Visual Studio.
- Install Python by downloading the x64 branch. You can go with the default settings, as well as select the "Add to path" option. This will add the Python binary to the global path, which means that you will eventually have to log out and log back in.
- Install Visual Studio.
Typically, Node.js projects are highly dependent on environment variables. In the package.json scripting section, you can use locally installed node modules. They recommend that you do not activate packages globally with npm –g.
To summarize: with Node, building desktop applications using HTML, CSS, and JavaScript looks promising. Utilities such as Spotify, Light Table, and Brackets use open-source browser renderers. Using node-webkit and atom-shell, developers can easily import web applications to the desktop and use Node and npm modules in the interface, taking into account the location of JS Windows.