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/
Related
So I have the code
Directory/app/script.js:-
// MongoDb
// Connect to MongoDB through Mongoose
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost:27012/profiles')
// Requiring the Schemas
const SignIn = require('MongoDb/Models/profiles.js')
console.log(SignIn)
So what is wrong in the code. The console in the web says that require is not defined. Can you help me with how to go about it.
There are two reasons you might get this error. Since you said
The console in the web says
I expect it is the first reason in your code
You are running the code in a web browser
Node.js is a stand-alone environment for running JavaScript. It is not a browser extension.
To run Node.js code, save it in a file with a .js file extension, then run it with:
node yourFile.js
If you want Node.js code and browser code to interact then the typical way is to write a server in Node.js that hosts a web service. Express.js is a popular way to do this. You can then use Ajax to make HTTP requests to that web service.
For bi-directional communication (as opposed to the request/response of Ajax) look at Socket.io.
You are using ECMAScript modules
Node originally used CommonJS modules which use module.exports and require.
Recent versions also support ECMAScript modules which use export and import.
Either switch to using import instead of require or change your configuration to use CommonJS modules.
I have a node module that I am building, and I want it to be able to execute on the server (in nextjs server side rendering) and the client (call additional lifecycle methods in the UI). This same module also needs to work when used purely as a js library that can be included in a <script> tag on the page. This module depends on the uuid module, which has logic in it to check if it is running in a browser or server context, and use the proper random number generators/crypto libraries that are available in that context.
If I don't specify a target in my webpack config, the bundle works great in a client browser. It includes the webpack browser logic just fine. But it doesn't work in the server case - webpack removed the server capable logic in the uuid module.
If I target: 'node' in my webpack config - it executes just fine as a node module on the server and the client. It seemingly included all of the logic this time. But now it doesn't work if included just as a script tag on the page. I get ReferenceError: require is not defined from the file that depends on the uuid module.
It seems like the uuid module should handle these different environments just fine, but webpack is messing with that. How can I let that module resolve the proper implementation at runtime?
I unfortunately do not have a minimally reproducible example, or additional code to share at this time. I figured someone might have run into this with webpack (or even webpack and the uuid module) and know the solution.
I was trying to do this by building a single version of the package, but I don't think that is possible.
What is possible is building multiple versions, and then hosting the web bundle via unpkg or jsdelivr via an entry in package.json. Those entries can point to the target: 'web' version of the package, while the npm package can point to the target: 'node' version.
Is there front-end node modules or all are server-side unless packaged using browserify and similar tools?
Node.js modules are designed to be used with Node.js.
You might run Node.js as a server side environment. You might run it as a command line program. You might run it as an HTTP client in its own right.
Node.js does not run inside a browser. Node.js modules are not generally designed for use embedded in a webpage using <script> elements.
It is possible to write a hybrid JavaScript file that can function both as a Node.js module and as a script in a webpage … but there isn't often a good reason to do so. (I've done it once: To write a client for a particular webservice that I wanted to use in a browser and in a Node.js program).
Browserify can convert some Node.js modules so they can run in a browser.
I installed a npm package that had 'http' as a dependency, so I installed that as well. All that was downloaded by npm for 'http' was a package.json file which referenced a non-existent index.js file. Is the index.js indeed missing from package.json or am I doing something wrong?
I'm using systemJS as a library loader.
TL;DR: you can't run server-side modules inside a browser.
From what I understand, you're trying to use server-side JavaScript modules inside of a browser, which isn't going to work. Browser have (very) limited abilities to set up network connection, or read from local file systems.
The http dependency that you're refering to is part of the Node standard library. So for Node apps, running server-side, it's always available.
In your case, you assumed that because require('http') didn't work (in the browser), you needed to install a separate package for that (this package).
But even if that package was working properly (it isn't), it wouldn't have worked inside of a browser because it depends on other modules inside the Node standard library, that also aren't available in a browser.
I don't know if CouchDB has a REST API itself that you would be able to use from the browser, but if not, you're going to have to implement a server-side API that will act as go-between between the browser and CouchDB.
From the browser, to talk to CouchDB, try the PouchDB library (https://pouchdb.com/) and put the URL to your CouchDB in the constructor. It's intended for connecting to a local PouchDB (javascript implementation of CouchDB) but their APIs are identical. Or, try actually using a local PouchDB and then syncing between the two databases.
So I have been fiddling around with Netsuite and I want to know if it is possible to use Node.js or npm modules within a SuiteScript or Suitelet?
My goal is to use a few npm modules within Netsuite to interact with Amazon's MWS API.
No, it's a different Javascript engine. Rhino I believe.
You can use various JS libraries but Node modules are usually written with that engine in mind.