Is more a question than an error, I already know in a browser environment the net module is supposed to not exists, but I'm writing a electron app with angular2 and i've tested this script:
<script>
var connection = mysql.createConnection({
host : 'localhost',
user : 'user',
password : 'password',
database : 'test'
});
console.log( connection.connect() )
</script>
and is working good, since electron is node based and can interact with filesystem and net.
But the problem come when I try to put in a angular2 component this:
import { Component } from '@angular/core';
import * as mysql from 'mysql';
export class AppComponent {
constructor() {
var connection = mysql.createConnection({
host : 'localhost',
user : 'user',
password : 'password',
database : 'test'
});
console.log( connection.connect() )
}
}
it says: __Net.createConnection is not a function__..so my question is.. why? what's happening?
Any hint is appreciated. Many thanks in advance.
Hi @codekraft-studio unfortunately I am not familiar with angular2 components, so have no hints to give specific to that :( Generically, the reason you get that error is because require('net') is returning probably an empty object {} (which is clearly not correct--it should return https://nodejs.org/dist/latest-v6.x/docs/api/net.html) and so there is no createConnection function.
@dougwilson ok, thanks, I will post the issue on typescript page since I belive the issue is related with that or with SystemJs loading the scripts for angular2, maybe they will know the answer.
I'll leave this issue open to comments and once I found the answer I'll copy and paste it here for future readers and close it, is alright?
Hello @codekraft-studio.
If your component are rendered on client-side the mysql node module not work, i'm tried to use with vue.js, the module Net does't work on the client side, you ned to process the mysql query on the server-side.
The alternative are
@joridos i'm using electron that is a node environment, looks like a browser but is not and is capable of use node core modules, the problem is with typescript compiler I guess
@codekraft-studio I see, can be possible.
This isn't an "Angularjs" thing, its a nodejs problem. I've facing the same thing using React.
There should be some kind of fix or idea of why this would happen when connecting client side...
I'm happy to make whatever changes are necessary to this module if someone can tell me what to change :+1:
@dougwilson a good place to start might be with "var Net = require('net');" in "Connection.js" on line 3. Here's a screenshot of the missing method trying to be referenced: https://www.screencast.com/t/bbmlevoo
Ok, what method should it be using instead? Happy to change it :+1:
@dougwilson it should be that method, just looks like "Net" is NOT being imported correctly or something. I don't see it in my modules directory after installing the mysql package.
The net module is built into Node.js directly https://nodejs.org/dist/latest-v9.x/docs/api/net.html and so won't be in your installed modules, which are just the non built-in ones.
@dougwilson ok, idk then.
@dougwilson I have the same error in 2019
with angular 8
and Nodejs v10.16.0
Did you find a solution
I'm happy to make whatever changes are necessary to this module if someone can tell me what to change 馃憤
@joridos i'm using electron that is a node environment, looks like a browser but is not and is capable of use node core modules, the problem is with typescript compiler I guess
Did you ever find a solution to this issue. I am having the same problem and it's driving me nuts! The .net lib is obviously not being seen, but I can't figure out how to fix it, inside of electron.
Most helpful comment
This isn't an "Angularjs" thing, its a nodejs problem. I've facing the same thing using React.
There should be some kind of fix or idea of why this would happen when connecting client side...