How typescript requier Nodejs module? - node.js

i read many similar question
this is the workaround i am trying now
for example to mysql module in Nodejs
1.
cmd enter:
npm install typescript
npm install mysql
typescript write:
import * as ms from "mysql";
// i see some people write " import ms = require("mysql"); ", but i get the error
then i check console window in chrome devtool
i see error " Uncaught ReferenceError: exports is not defined "
and this error seems to stop script from keep running.

TypeScript will, by default, transpile to JavasScript files using the CommonJS module format which browsers do not support.
You can configure it to use ECMAScript modules (which are supported by browsers).
However, the mysql module depends on APIs that are available in Node.js but not in web browsers so you cannot use it in a web browser.
You could write a web service (e.g. using Express hosted by Node.js) which accesses the database and then interact with it from the browser using forms, Ajax or Web Sockets.

Related

How to get nodejs on client-side on cordova?

I am making a app with cordova. I want the coinmarketcap stats in it. I am using nodejs and it is working fine when I execute it in my command line. However I want to use it in my cordova application. This is the link to the npm package https://www.npmjs.com/package/node-coinmarketcap
When I try to run it in a browser I get the error message require not defined. I believe that error comes because nodesj is a server-side and browser is a client side ? Am I correct ?
My code
var CoinMarketCap = require("node-coinmarketcap");
var coinmarketcap = new CoinMarketCap();
coinmarketcap.multi(coins => {
console.log(coins.getTop(10)); // Prints information about top 10 cryptocurrencies
});
I expect that I can run my code in my browser or cordova application.
You are right, Node.js is a JavaScript run-time environment that executes JavaScript code outside of a browser. You have to find some client-side JS code or execute your node code on your server and send result to your Cordova app.

fs.existsSync is not a function error when requiring grpc through create-react-app

We are trying to get gRPC to work with React (actually we were trying to get it to work with React-Native but gave up on that for now).
Using plain node.js things are pretty straight forward if you follow this example.
We started by using create-react-app but when we started the app, we got the following error:
existsSync is not a function
That was casued by this bit in pre-binding.js belonging to the node-pre-gyp package located in grpc:
var existsSync = require('fs').existsSync || require('path').existsSync;
My understanding is that something goes on with Webpack (or some other process run by create-react-app) that goes and returns and empty object instead of require('fs').
Any ideas of how to get this to work without having to give up on the wonders of create-react-app?
To test it out you can just follow these 2 easy steps:
create-react-app test-app
add import grpc from 'grpc'; in the App.js file
Basically, from what I understand now, you are not really supposed to use gRPC + Protobuf directly from any frontend but rather it is more common to use the grpc node package on node.js server-side code and then communicate with the browser-side code using Express.
The server-side code on node.js then communicates using grpc with the microservices.
We are testing out the use of Firebase Functions to communicate securely with the frontend and Firebase Functions communicate with the Go microservices using grpc.

Can we use node core module or other node module in React Js using bundling tool Webpack?

I try to use node core module fs in reactjs.
const fs = require('fs');
but it is throwing error: "can't find module fs". can we use node core module or other module in front end side(react js) using webpack. if yes then how can we achieve this and if we can't, then is there any way to use node module in front end side ?
Using a node module in the front end can make sense, but in the case of fs it doesn't. The file system of the client computer is not exposed to the code running on a website.
This is enforced for evident security reasons. You cannot circumvent this barrier magically using a node module on the client side.
The short answer here is : no, you can't.

Compile HttpServer from Dart to JS for NodeJS

Is there some way to create http server on Dart that will be compiled to JS for Node JS?
If I try to use default server
HttpServer.bind(new InternetAddress("127.0.0.1"), 8081);
and then compile this code via dart2js I'll get error when node started
Unsupported operation: InternetAddress
You can't use dart:io when you want to build to JS because dart:io won't be available where the build output runs.
dart:io forwards to operating system calls and dart2js doesn't have any ability to translate such calls to JS (actually Node.js because JS itself doesn't provide any way at all)
You might need something like https://github.com/luizmineo/node-webkit.dart to use a custom implementation that translates to quivalent Node.js calls.

Troubleshooting socket.io error message

I inherited a project which utilizes node.js and socket.io, both of which I am not too familiar with.
I installed node.js and npm via homebrew and socket.io via npm.
The current setup has two different "sites": The frontend, that the user interacts with, and the backend that handles all the communications with a DB. The application uses several different displays, and the previous developer deemed it best to use the current setup.
I can successfully start the node server by invoking node node-server.js. In the head of the node-server.js, socket.io is being called like:
var sys = require('sys')
var exec = require('child_process').exec;
var io = require('socket.io').listen(8090);
[...]
In my front end, I call socket.io like this:
<script src="http://192.168.1.111:8090/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://192.168.1.111:8090');
[...]
However, when I debug the page, I get the following error messages:
SyntaxError: Unexpected identifier 'to' (anonymous function) socket.io.js:1
ReferenceError: Can't find variable: io sample.html
In the terminal window, where I started the node server, I get the output:
debug - served static content /socket.io.js
I should add that the two sites reside in separate folders - I access the frontend through a local Apache server, the piece that interacts with the DB sits someplace else on the same machine.
How would I correctly call the socket.io in my client?
Is there a way to enable better debugging?
Update
So the to in the error message, is because whenever the browser tries to load the socket.js file, it gets the reply of Welcome to socket.io

Resources