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.
Related
I searched whole stackoverflow to fix it, but cant. So... I need to use NPM module in browser (index.html).
Getting error "require is not defined" if using
<script src="./script.js"></script>
in html file, and using const { WebcastPushConnection } = require('tiktok-livestream-chat-connector'); in script.js.
Tried to use browserify, and when i use outputed js script getting error "require.resolve is not a function"
Can anyone help?
tiktok-livestream-chat-connector is described, by its website, as:
A Node.js module to receive and decode livestream events like comments and gifts in realtime from TikTok LIVE by connecting to TikTok's internal WebCast push service.
At no point in its documentation does it mention compatibility with web browsers.
Browserify (and similar tools) can bundle up CommonJS modules into a single file so that they can run in environments which don't support CommonJS modules (e.g. browsers). They can't polyfill all the Node.js features that browsers don't support (like the ability to make raw socket network connections).
If a module needs to run on Node.js then you can't just move it to the browser.
You could write a program for Node.js that runs as a web service and operates as a bridge to whatever the module does. The the browser could connect to your web service with WebSockets or Ajax requests. The project links to an example of one.
First I dowloaded nodejs from link.
Then I installed browserify npm install -g browserify
Then I installed fs npm install fs
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
const fs = require('fs');
fs.writeFile("/tmp/test", "Hey there!", function(err) {
if(err) {return console.log(err);}
console.log("The file was saved!");
});
</script>
</body>
</html>
I am getting the following error:
Uncaught ReferenceError: require is not defined
at index.html:12:12
Why require is still not defined? What could I do to get the code to be executable?
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.
I just want it to be local storage, so I do not want to share the datas between the users, I just want to interact with there own local storage datas.
If you want to use the localStorage API provided by browsers, then do so.
localStorage doesn't need Node.js. It doesn't need the fs module that is built into Node.js. It doesn't need the require method that is part of the CommonJS module specification.
You can't muck around freely on the user's file system from code running in the browser. That would be a huge security problem.
You can't use node APIs in browser environment; more specifically browser won't allow script to directly interact with file APIs like Node.
Either run your code in node as quentin said, or use browser file api
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.
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.
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/