Question: Net.createConnection is not a function

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:16 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
jonatanrdsantoscommented, Jul 18, 2019

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

  • Create a rest api to get data from mysql, you can use express to serve it and get with axios or other HTTP request nodejs module.
  • In the server-side process the query and send the result as json.
4reactions
snolangpscommented, Feb 15, 2018

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…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error in MySQL library for Node.js - Stack Overflow
The error you're getting about Net.createConnection not being a function means it's coming up as an empty object and the error is related...
Read more >
Net - node - Read the Docs
This function is asynchronous. When the 'connect' event is emitted the socket is established. If there is a problem connecting, the 'connect' event...
Read more >
Net | Node.js v18 API - NodeJS Dev
The node:net module provides an asynchronous network API for creating stream-based TCP or IPC servers ( net.createServer() ) and clients ( net.createConnection ......
Read more >
Uncaught TypeError: net.createConnection is not a function
I am connecting my server (which uses C++) to client (which uses React Js) through APACHE THRIFT. However, I got the error "Uncaught ......
Read more >
Node.js - Net Module - Tutorialspoint
Sr.No. Method & Description. 1. net.createServer([options][, connectionListener]) ... net.createConnection(options[, connectionListener]). A factory method ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found