I'm new to Reactjs, Nodejs and MongoDB. I'm currently trying to change Mediums snowflake tool to store users scores in a database. I have installed yarn, mongodb and mongodb-core through npm. It is a single page web application which is what I think is causing me trouble. I add
var MongoClient = require('mongodb');
To SnowflakeApp.js and encounter the following error:
Module not found: Error: Can't resolve 'dns' in
'/home/mlAHO174/snowflake/node_modules/mongodb-core/lib'
I've tried googling this error and have discovered it could be a range of things. I'm not sure if it is because React is front end and I'm trying to alter back end or because mongoDB is installed incorrectly. I'm new to this so would be grateful for help!
DNS is a core module of Node.JS. Telling people they need to install DNS via NPM will end up with them having a completely different module that does something else.
https://nodejs.org/api/dns.html vs https://www.npmjs.com/package/dns
This error most likely means you are trying to do something from the client-side that needs to be done on the server-side. If MongoDB module can't find the DNS component, it's running on the client-side.
MongoDB has to run on the server. In order to access data from React dynamically you'll need to set up an API using something like Express or Apollo.
Update:
A great way to do this is with Azure Functions (TypeScript) or AWS (Lambda) functions
For anyone who encounters this Error while importing the clientPromise (like in the with-mongodb template):
Make sure you're in the /pages/ directory!
It won't work in other directories like /components.
(and you should take a break or get some coffee...)
The problem is that you are trying to connect to the database from the front end. If this were possible that would open up a whole world of security issues. You need to set up your database connections on the backend and then have the front end make requests to the backend to handle the database.
I solved this by installing and using 'bson' instead of 'mongodb' for the client part of the code. 'bson' has a tiny bit of what 'mongodb' has and it might have what you are looking for. 'bson' is built for the browser.
In my case I needed the "ObjectId" in the browser and pulling it in from 'bson' did the trick as I didn't want to reference 'mongodb' because of the error described in the OP.
The other answers are also correct depending on why you're getting this error.
I think - mongo package is meant to be run on servers only, not in the browser.
It does not work in Next.js pages file components too, but does work in getStaticProps, getServerSideProps, getStaticPaths etc - because they run on the server, not the client.
Alternative - use Firebase Realtime database, you can access it in client-side code too. Example - a website (say a React app) that is hosted on GitHub pages or some other static server, but doesn't have a web app server (aka backend).
welcome to stack overflow.
You need to understand and learn few basics of web-applications. There's frontend, backend and a layer between them and a layer between backend and database. Frontend includes react.js, angular.js or anything else that is on browser. Backend is used to take request from frontend, providing API's to frontend and ask for data from other API's or database. Database includes sql, no-sql.
The error you are facing if of a NPM module mongodb-core.js. Either it's not installed properly, or installed using wrong version of module which is not comparable with your node version, or wrong version of NPM, or module using another NPM module which is not installed.
The issue in your case is mongodb-core uses a module dns which is not been installed. Try to install dns with npm i dns. or remove and install mongodb-core again.
Related
I've made a react project that uses nodejs, graphql, apollo, express, and mongo DB.
I'm struggling to get it deployed through Heroku.
first, when I deployed it, it failed and said I needed to add a build pack. I've gone through and gotten an idea of what buildpacks are. However, I'm more vague on if I need multiple buildpacks for my application. So I've added both a nodejs buildpack and a create-react-app buildpack. This is the furthest I've reached as it deploys with a 404 error.
One thing to note is that that the nodejs buildpack requires a package.json file in the root folder, though mine were both in my client and server folders. Not sure if I structured it wrong? So I went ahead and npm init in the root folder to create a package.json not knowing if that's an adequate solution.
I've checked the Heroku logs and getting a few errors it seems:
herokulogs
I've also read somewhere about maybe needing a profile? which is not something I entirely understand yet.
As you can tell, I'm new to this and still learning a lot. I'm hoping someone can point me in the right direction as I'd like to see this thing through. Please let me know if you'd like to have access to my codebase.
Thanks
It seems, that npm start throws that error. How did you defined it in your package.json and can you execute it on your local machine?
Have you read the documentation for the Node.js Buildpack?
I'm assuming that you are using one heroku server for both front-end and back-end.
If so, they must be separate.
I'm using NPM module ip (https://www.npmjs.com/package/ip) in an Angular app to do some parsing/comparation of IP addresses. Everything was working fine, but when I try to build the application for production using ng build --prod --aot, the compiler complains with the following error:
ERROR in ./node_modules/ip/lib/ip.js Module not found:
Error: Can't resolve 'os' in '/home/fel/Documentos/proyectos/IPGes/node_modules/ip/lib
It seems that the error comes because it tries to use the module os, which is only available in a node.js environment, not in the browser.
Does anybody know if there's a workaround that enables to use the ip npm module in an Angular compiled app?
Thanks in advance,
The ip library is a NodeJs specific library since it has a dependency on os. In some cases, you can have some luck using browserfy to make your node packages work in the browser. However, I don't think it'll work with this one.
I would suggest moving this logic to your server. If you're using node, just add the package there and surfaced an API endpoint to post an IP Address. Thay way, you can use the library as you wish.
The only reason why this works locally while running in Dev is that Angular is running a node server at that point. Once you bundle everything up, it's just a bunch of bundled HTML and JS.
Just in case anyone has the same trouble, I dropped the ip library (which is a great tool, but for Node projects) and I used ipaddr.js (https://www.npmjs.com/package/ipaddr.js), which has all the methods I've needed in the application.
Cheers,
Question
I want to deploy my application on the server in production but I am struggling to do so.
When I use npm run build to produce an "Optimized" version of the app and launch it with serve, it doesn't seem to use the proxy.
Going back to the dev server with npm start, I get this error:
TypeError: Kotlin.defineModule is not a function
I cannot get out of this. Has someone got a similar configuration? How did you make it work in production? Do you use something to proxy the requests to your backend?
App configuration
Frontend:
Web app using create-react-kotlin-app in kotlin, react. It's on localhost:3000 (dev-server) and has a proxy to localhost:3001 (the backend).
Backend
Backend, which is a simple express router for auth and data managment from the database.
Thanks in advance
I'll go back and answer my question,
One is a bug on their side:
https://youtrack.jetbrains.com/issue/CRKA-66
I'm using a config that maybe is not ideal:
I copy the build folder that contains the optimized folder into the backend and I serve the main view "index.html" as an entry point using sendFile().
I then use the url and args to route the user into various react components pages, maybe in the future it's best to switch to: https://github.com/JetBrains/kotlin-wrappers/tree/master/kotlin-react-router-dom
according with the repo documentation, if you are getting Kotlin.defineModule is not a function you would need to run:
rm -rf node_modules/.cache
Default node.js module gets created with express, adds all the dependencies and forces me to remove all these files afterwards.
Isn't there any other way to create simple node.js project without express while keeping all remaining seetings (node_modules marked accordingly etc.)? Was trying to google around but with no luck.
Please vote for the related request on YouTrack: https://youtrack.jetbrains.com/issue/WEB-12988
What I've done
I've developed a custom-made Kurento Media Server module but I'm having issues when I try to use it in my node.js application.
This is the system's description:
Based on the one to one tutorial, 2 clients establish connection with each other. My module, which is a OpenCV module, takes each frame and converts it to grayscale. Client A's WebRTCEndpoint is connected to my module and this is connected to the B clients WebRTCEndpoint. The purpose is to apply the grayscale filter to A's video stream, while B's one should maintain intact.
After programming everything, I've installed it in the Kurento Media Server just like the developer team explains it in the official documentation (it seems to be ok because when I execute kurento-media-server -v command I see it alongside other ones). The last step I've done is generating the Javascript library with cmake .. -DGENERATE_JS_CLIENT_PROJECT=TRUE.
The problem is that I don't know how to add it in my application. I have it locally, so I suppose I don't have to add the module dependency in my package.json file, right?
If your server app is a node.js application, you'll need to import the module in your package.json. You can find an example of a node.js tutorial using an external module here and here. You can see their package.json, and they have the modules imported. Otherwise, your app won't have the correct types imported.
EDIT
You'll still need to register the module in your server.js code. From the same project used in the other links from this answer, you can see an example here.