Initializing node.js environment through C++ application - javascript

Just want to know is it possible to initialize/start node.js from C++ application. Root of this question is that I am having a C++ console application, which launches an javascript application which does require('os').Now It is failing as I suppose node.js is missing or not initialized as I am getting Uncaught ReferenceError: require is not defined
Edit 1:
I tried browserify and it resolved the require() issue. Now problem is that I am not able to use any of the methods exposed by the required module.
I am getting "undefined is not a function" error.

Since require is not defined it seems like your JavaScript engine is not supporting the CommonJS specification. You cannot run node modules without CommonJS support.
Generally it's hard to run node modules anywhere but in node, because of the node internals. There exists software to transform node modules and scripts to other platforms like webpack and browserify. Maybe those will create JavaScript that you can use in your project.

Related

Uncaught ReferenceError: module is not defined at guacamole-common.min.js

We are using guacamole-common-js in one of our projects and using guacamole-common.min.js in the script results in a module not found error:
<script type="text/javascript" src="guacamole-common.min.js"></script>
I am using guacamole-common.js: 1.4.0-a
At first, I thought this might be related to guacamole-common.js version but I tried downgrading its version but still facing the same error.
The use of module.exports indicates that the file is a CommonJS module. This is the default module format of Node.js and is not supported by browsers.
Either:
You have a module designed for Node.js and not web browsers. There might be a browser targetted version available.
You have a module designed for use with a module bundling tool (such as Webpack or Parcel) and should use one of those to build your browser-side JS application.
Try carefully reading the documentation from wherever you downloaded the JS file from.

libp2p for React Native: How does libp2p get around needing NodeJS' EventEmitter when used in the browser?

So I'm trying to get run a libp2p node using React Native.
I realise that it isn't currently supported (Dec 2017 -- may have changed since) but I decided to try anyway since it already works in both the browser and NodeJS.
After requiring libp2p I get the error:
bundling failed: UnableToResolveError: Unable to resolve module `events` from `<full path>/node_modules/libp2p/src/index.js`: Module does not exist in the module map
This is React Native complaining that it cannot find events; which makes sense because it is part of the NodeJS environment and is not available in the browser (or in the React Native environment).
How is this handled in the browser? Or rather; how is libp2p intended to be used in the browser if they aren't bundling it at all?
The README also mentioned a specific bundled version of the library for use in the browser, however I have given up looking for it.
Given the lack of documentation and bundling of libp2p I can only assume that you're intended to bundle it yourself.
Creating the bundle works fine -- which answers the question about use in the browser -- however libp2p in RN still does not work due to a lack of a compatible webcrypto implementation.

Using module "child_process" without Webpack

I'm using Webpack to bundle dependencies, one of which is the emailing service postmark. This service depends upon something called child_process that apparently ships with node.
The problem is, when I attempt to run webpack to bundle my app, it complains:
Module not found: Error: Cannot resolve module 'child_process' in ...
Most of the answers online say that, in response to this error, I should add the line:
node: {
child_process: 'empty'
}
to my webpack config. But this makes no sense, because then webpack just doesn't try to look for child_process, and consequently, when I run my app, I get the following error:
Uncaught TypeError: exec is not a function
which is an error from postmark (the service that relies upon child_process) complaining that the exec function within the child_process module doesn't exist.
Thus, I'm wondering how I can include the child_process module in my build without webpack complaining?
Front-end developer of Postmark here.
It looks like you're trying to bundle postmark.js into your client-side JavaScript but https://github.com/wildbit/postmark.js is a Node.js library. This means it will not run in the browser and requires a running Node.js server.
The main reason for not supporting browser environment is security. As you can see in this example:
var postmark = require("postmark");
var client = new postmark.Client("<server key>");
it requires you to insert the auth token. Bundling this with your client-side JS would expose it and allow everyone access your postmark account API.
Hope this clarifies it.

process.binding is not supported, browserify

In a project where I am using Gulp and Browserify, anytime I try to require dust library (dustjs) in my script, Browserify throws an error: "process.binding is not supported".
Note: Browserify throws the same error whether dust is Installed from npm or called in and added via 'browserify-shim' in package.json.
Thanks for your help.
Browserify only shims certain common Node-specific properties. As said in the process documentation:
This module mostly exists to provide the nextTick functionality and nothing more. We keep this module lean because it will often be included by default by tools like browserify when it detects a module has used the process global.
It also defines a few other properties to prevent undefined errors from being thrown. binding and chdir intentionally throw errors, however.

How to "require(module)" in mongo shell

I am writing a mongo shell script for data management. I want to write it using modular code that makes use of function libraries as modules. In some cases, my own modules. In other cases, carefully selected node.js modules (that I know will work in the Mongo shell environment, e.g. uuid).
Unfortunately, Mongo lacks real module management. load() is not the same thing. I'm looking for a back-fill, as it were.
Does anybody know of a library that can provide CommonJS module loading functionality, that is generic enough to run in the Mongo shell, or that has been ported to run in the Mongo shell?
Yes, I know, I could just do it in a purely node.js environment. But if there is such a thing as a real module loader that will work in the mongo shell, that would be my first choice.
Well, there are some tips to get it working.
The first, if your CommonJS module requires no module is a simple as:
var module = {};
load('/lib/migration/forms.js');
print(typeof module.exports);
The second, if your module requires others is to build a single module with browserify and require it like in the above example.
No. The mongo shell is its own javascript environment running the V8 engine. You can't load in Node.js modules into the mongo shell anymore than you can into the browser. A lot of Node functions just won't be part of the mongo shell environment. You can either use the Node.js driver in Node.js so you can use your Node modules, or you can try to get the necessary bits into a js file that you can run to set up the appropriate environment when you run the shell, e.g.
mongo --shell mymongohost:port/myDB myjsfunctions.js
While the legacy mongo shell does not support node modules via the require statement, the new mongosh shell does. You will need to install it via the 5.0 MongoDB repo but it is compatible with any database from 4.0 and above.
Reference here: https://www.mongodb.com/docs/mongodb-shell/write-scripts/require-external-modules/

Categories

Resources