IP addresses utility library for Angular - javascript

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,

Related

Allow node module relying on uuid() to execute in client and server

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.

Module not found: Error: Can't resolve 'dns' when using MongoDB

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.

The npm http package only provides a package.json, no javascript files

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.

Installing JavaScript AWS SDK for NativeScript

I'm trying to use AWS-SNS in NativeScript. AWS SDK is available for JavaScript.Can anybody know how to install AWS SDK for NativeScript.
I tried by installing aws-sdk for javascript on Node.js. Reference link is "https://www.npmjs.com/package/aws-sdk".
But because of below code
require("aws-sdk")
In Nativescript an error saying Failed to find module: "crypto" is coming.With a bit of search in internet, I found that crypto module support is not added in Nativescript.
Now I'm wondering are there any other ways.
Thanks In Advance.
As NativeScript does not (yet) "polyfill" a crypto module this could be a hard thing.
Searching the code for references to crypto, you'll find that there's not that many uses and the uses are not that advanced. What this means is that you should be able to substitute the Node module crypto with crypto-js which works in a NativeScript environment.
You might need to fork the AWS SDK codebase and substitute the calls to the Node crypto module with calls to the corresponding methods in crypto-js. Unfortunately they do not share the same API.
If you're running Webpack (or anything like it) you should be able to create a "transformer module" which will transform Node crypto calls to the corresponding crypto-js calls and then make sure your transformer module is called instead of the Node crypto module via Webpack alias'.
This is an old question, but I recently had to figure this out so for future AWS-{N} integrators:
The easiest solution is to use the regular aws-sdk along with nativescript-nodeify. Instructions for aws and cognito are at the bottom of this page. It takes care of making a bunch of dependencies (i.e crypto, fs) NativeScript-compatible

Installing custom-made Kurento module in node.js

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.

Categories

Resources