mysql: Question: Net.createConnection is not a function
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.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 16 (5 by maintainers)
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
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…
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 nocreateConnection
function.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.