How to use a .node file? - javascript

I was trying to install node_mouse and when I looked in my node modules folder and instead of a normal .js file extension, I found a .node file extension. How could I run node_mouse? I looked this up and I think it might be an addon written in C++, but I'm not exactly sure(Node addons)

Yep these .node files are Node Addons (binary modules) and you should be able to just use require() on them. Be aware that it will look for .json and .js files first.
From the documentation:
The filename extension of the compiled Addon binary is .node (as
opposed to .dll or .so). The require() function is written to look for
files with the .node file extension and initialize those as
dynamically-linked libraries.
When calling require(), the .node extension can usually be omitted and
Node.js will still find and initialize the Addon. One caveat, however,
is that Node.js will first attempt to locate and load modules or
JavaScript files that happen to share the same base name. For
instance, if there is a file addon.js in the same directory as the
binary addon.node, then require('addon') will give precedence to the
addon.js file and load it instead.
You should also be aware that these are binary modules, so loading them is a lot like just running a standard executable file (Think .exe file if you are just familiar with Windows). Like native executables they are a lot more dependent on the particulars of your system and also potentially a security risk. While a standard .js module will be portable (with a few caveats) a .node binary module will be fundamentally built for a particular machine architecture and OS and often even a particular version of Node. If you are having trouble loading a binary module you should make sure you are running the right version for your system, and confirm with the provider that your system is actually supported.
Sometimes specific functionality or performance needs requires it but with Node.js you shouldn't be loading binary modules unless you really have to.
PS: This is an old question and answer but I happened to come across something that reminded me of this, so for the record: Don't use node_mouse which looks like a garbage package, instead global-mouse-events looks far more promising.

Yes, the normal "require" usage is appropriate for .node files. The point of these files is to create portable binaries (using node-gyp, from C++) that can be referenced like normal node requires. See the hello.js section of the node addon docs:
const addon = require('./build/Release/addon');
console.log(addon.hello());
After looking into this NPM lib, it is loaded by node correctly on my Windows, Mac, and Linux VM's with several different node versions, but the binary throws an array of errors. On windows, it has a specific version of windows as a build target (likely NT, because windows 10 throws an error):
Error: %1 is not a valid Win32 application.
On OS X, this is dyld failing to open a shared library referenced by the binary. (See man dlopen):
Error:dlopen(/.../node_mouse/node_mouse.node, 1): no suitable image found.
On Linux, we get an ELF header error, which tells us that the binary can't be run on this OS.
Error: /app/available_modules/1484064894000/node_mouse/node_mouse.node: invalid ELF header
The author seems to do a lot of Windows NT work, so if you really need this working, find a fresh copy of Windows NT with all the dev add ons.
Lastly, consider the security risk of running third-party closed source binaries in your code base (especially ones that control mouse movement).

Related

Can I use node js with a static webiste?

I am making a small website for a buddy but he only has a Webserver from hostinger, for his website I want to use a Node.js Package. Is it still possible to use this hosting service if I just point the URL to the index.html file?
Or does the javascript not work anymore if I use Node.js?
tl'dr: Most likely not.
If the plan your friend has does not offer support Node.js you will not be able to use such package, but it will be hard to say for sure without knowing exactly the plan your friend has.
Webserver usually means static content (HTML, CSS and JS) hosting, and serving to the browser as-is. In this case no actual processing is done on the server - which is what node.js is for.
You could use other browser npm packages (or isomorphic ones, that support Node.js and browser environments), but in the case you described you won't be able to use node specific packages.
According to Hostinger's website, you can only use node.js if you are on VPS plan.
EDIT: It is worth mentioning that you could use a node package if you use it locally to generate or preprocess the assets before putting it in a webserver. But in this case the package will be executing in your machine instead of the server, during build time (not run time).
It depends on the package and what you want to do with it.
Node.js can be used to run server-side JavaScript. If you need to run server-side JavaScript then you need a hosting service that supports it. A service that supports only static files will not suffice.
The term “a Node.js package” might refer to a package available via NPM. Some such packages require Node.js (in which case see the previous paragraph). Others will run client-side in the browser. Typically, if you are using such a package in client-side code you will use a bundler (such as Webpack or Parcel) to convert your program (which imports said package) for use in browsers.
Some websites are generated programatically at build time and the resulting static files uploaded to a static hosting site. Node.js can be used to do that generation. It is, for example, the usual means by which sites using Gatsby.js are built.

How to deploy a JavaScript automation app?

I have built a web automation programme with selemium & javascript. Now i want make it usable for everyone so that anyone can use it without any dependecies or coding environment i mean any non technical people can use it easily.
how can i do it?
One option is to use: https://www.npmjs.com/package/pkg
This command line interface enables you to package your Node.js
project into an executable that can be run even on devices without
Node.js installed.
The most useful part will be dependencies:
During packaging process pkg parses your sources, detects calls to
require, traverses the dependencies of your project and includes them
into executable
However, i expect this will not manage the webdriver executable. You'll most likely need to ship chromedriver/geckodriver/etc with your resultant exe.
Looking forward, i expect you'll need to manage your distributable for the rollout of new browser versions. Different users will be on different versions at different times and your relevant drivers will need to be updated and re-shipped.

How to minify SignalR

I have the following in my Index.cshtml:
#Scripts.Render("/signalr/hubs")
In my BundleConfig.cs, I have the following:
.Include("~/Scripts/jquery.signalR-{version}.min.js")
With EnableOptimizations on, I get a nicely bundled vendor? package. But in my Sources, I see:
Why is this raw unminified JS getting loaded? How do I bundle/minify it?
SignalR's proxy scripts are dynamically generated at runtime at /signalr/hubs by default. They're typically small, on the order of a couple of kilobytes or smaller, so minifying them will not yield any performance benefits (perhaps zero benefit at all if they already fit into an entire Ethernet frame).
Additionally, the hubs themselves cannot have their internal symbols/identifiers minified because it exposes a "public API" that your code consumes - see how dynamic (or interfaced) "client method" calls inside your Hub class are transferred over the pipe, so those names must be preserved for the system to work.
Finally, IIS is usually configured to HTTP gzip-compress certain dynamically-generated content anyway, this includes the SignalR proxy scripts - further minification can be counterproductive (as the entropy of minified scripts can be higher than uncompressed scripts).
But if you believe you can safely compress them, or if you want to bundle them, and you're certain you don't need the benefit of dynamically-generated proxies to handle rapidly changing developer requirements, then you can generate them offline:
https://learn.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client
How to create a physical file for the SignalR generated proxy
As an alternative to the dynamically generated proxy, you can create a physical file that has the proxy code and reference that file. You might want to do that for control over caching or bundling behavior, or to get IntelliSense when you are coding calls to server methods.
Install the Microsoft.AspNet.SignalR.Utils NuGet package.
Open a command prompt and browse to the tools folder that contains the SignalR.exe file. The tools folder is at the following location: packages\Microsoft.AspNet.SignalR.Utils.2.1.0\tools
signalr ghp /path:[path to the .dll that contains your Hub class] - This command creates a file named server.js in the same folder as signalr.exe.
Put the server.js file in an appropriate folder in your project, rename it as appropriate for your application, and add a reference to it in place of the "signalr/hubs" reference.

Browserify produces a *huge* output file

With the aid of node-google module I wrote a simple node module to enable a 'text web search' feature to my web app, presenting results in one of my views.
Since after a quite small number of queries from the same IP Google returns a 503 error, I decide to use the module on the client, so the limit is per client, not per server.
I did use browserify to convert the node module to a script to be sourced in a client page.
The script just requires 'google.js', and it's just 20 lines of javascript long:
'use strict';
var google = require('google');
var Google = Object.create({});
var Google.search = function(text, callback) {
...
});
// end of the script
The command I use is simply:
$ browserify google-search-module.js -o app/scripts/google-search.js
The problem is that the output browserify produces is far bigger than I did expect: a 1.2 kB module becomes a 2.4 MB script! Probably it's including all 'google' dependencies, too, but..,
The question is: is this normal? Is my search page expected to load a 2.4 MB file just to search some text on Google?
I'm quite sure I'm missing something, but can't understand what... :-(
That is expected behaviour. Browserify loads all modules imported with require() recursively, and outputs a single file. There are ways around this, but they are unlikely to work in your paticular case.
Normally, with Browserify, you might work with one huge bundle in development, but then build a much smaller production version. If you were using jQuery, for example, you could install the package locally into your node_modules folder. Then, for production, you could set the --exclude flag to have Browserify ignore anything in your node_modules folder, instead relying on a CDN to deliver jQuery to the client.
I say this is unlikely to work in your case because node-google really is a Node module. There is no guarantee that it will work in the browser (it may or may not). You really should determine if it is working before you start planning your next line of attack.
If it is working, you have two possible remedies:
Minify your bundle and make sure it is served gzipped. The resulting file size will likely be fewer than 100kB, if you can live with that.
Find some other module for doing a Google search, or implement one of your own. This is probably the best solution unless you must use node-google for some reason.
Of course, if it isn't working in the browser anyway, only the second solution is available.

Compiling nodejs code for safer distribution

Is there any software that I can use to compile nodejs program?
The reason I want to compile nodejs code is to make it safely distributable.
For example for a desktop application,etc.
And also I want to know whether nodejs will execute faster if compiled as it is asynchronous already?
Javascript is not a compiled language and Node.js is Javascript. It will be executed and interpreted at runtime. You can process your javascript with tool like grunt.js for example lint-test and uglify it, but be careful so that do not break the npm system since it is based on certain conventions.
To package your javascript for distribution in the node.js context build an npm module.
https://www.npmjs.org/doc/developers.html
For distributing javascript to the desktop client: Remember it's Javascript an it need to be executed in the Javascript VM. So to have some UI you need to run it in the browser or you need to have some webkit compiled dll to run your code...
something like this...
http://www.tidesdk.org/
You can also use: http://github.com/rogerwang/node-webkit (Thanks to #edi9999)
There is no way to do that with v8, it has only JIT option. It possible to make a "snapshot" with v8, but it's not exactly the same as compilation and node.js doesn't have support for this feature (also it might produce slower code). Also all your code will be available with toString() of functions.
You might be interested in JXcore project. It is a fork of node and as far as I know has some solution to code protection. Also one of the project goals is to develop javascript-to-LLVM compiler. Of course it can't have full support for ES specification (eval, new Function etc).
There's no way to 'compile' a nodejs program, as the javascript is interpreted at run time.
However, if you want to protect your code, you could maybe use something like Uglify JS to make the javascript less readable. However, this won't hinder people to change around your code.
The closest you might get to acheiving your goal is to create a self-executing Javascript bytecode wrapper.
A project that does this is pkg
It somehow creates a self-contained binary executable from Javascript, including module dependencies and asset files and produces a self-contained executable.
Installation and use is easy:
$ npm install -g pkg
$ pkg index.js -o my-program
$ ./my-program
It seems the resulting binary contains nodejs bytecode. It appears that you can cross-compile.

Categories