1

I wanted to create my own website and so started learning JS. I wanted to create a button called greet, that onclick shows how many people have greeted me (aka how many times it was clicked). This is easy to do with Java, but I'm having problems to establish a connection with Node.js.

Here is the code I'm using to connect to my db:

var mysql = requirejs(['../node_modules/mysql'], function (mysql) {

});
var connection = mysql.createConnection({
    host     : 'localhost',
    user     : '*********',
    password : '*********',
    database : 'logindb'
});

connection.connect();

I am not very clear about how RequireJS should help me, I just kept getting the error from my browser that require.js was not installed so I installed it. I am using WebStorm as IDE and Firefox 79 (latest version) as browser.

So far, I've tried using a define function, which also wasn't recognised. I also tried to understand something from the RequireJs Documentation, but it wasn't helpful.

The way i reference the require.js file in my home.html is: <script data-main="../js/logicForBackbone.js" src="../js/require.js"></script>

The error i get is:

Uncaught TypeError: mysql.createConnection is not a function
    <anonymous> Backbone
logicForBackbone.js:20:24
    <anonymous> Backbone
Uncaught TypeError: mysql.createConnection is not a function
    <anonymous> Backbone
logicForBackbone.js:20:24
GEThttp://localhost:63342/mywebsite/node_modules/mysql.js
[HTTP/1.1 404 Not Found 8ms]

The resource from “http://localhost:63342/mywebsite/node_modules/mysql.js” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff).
backBone.html
Loading failed for the <script> with source “http://localhost:63342/mywebsite/node_modules/mysql.js”. backBone.html:1:1
Uncaught Error: Script error for "../node_modules/mysql"
https://requirejs.org/docs/errors.html#scripterror
    makeError http://localhost:63342/mywebsite/js/require.js:5
    onScriptError http://localhost:63342/mywebsite/js/require.js:5
    load http://localhost:63342/mywebsite/js/require.js:5
    load http://localhost:63342/mywebsite/js/require.js:5
    load http://localhost:63342/mywebsite/js/require.js:5
    fetch http://localhost:63342/mywebsite/js/require.js:5
    check http://localhost:63342/mywebsite/js/require.js:5
    enable http://localhost:63342/mywebsite/js/require.js:5
    enable http://localhost:63342/mywebsite/js/require.js:5
    enable http://localhost:63342/mywebsite/js/require.js:5
    bind http://localhost:63342/mywebsite/js/require.js:5
    each http://localhost:63342/mywebsite/js/require.js:5
    enable http://localhost:63342/mywebsite/js/require.js:5
    init http://localhost:63342/mywebsite/js/require.js:5
    s http://localhost:63342/mywebsite/js/require.js:5
    setTimeout handler*req.nextTick< http://localhost:63342/mywebsite/js/require.js:5
    s http://localhost:63342/mywebsite/js/require.js:5
    requirejs http://localhost:63342/mywebsite/js/require.js:5
    <anonymous> Backbone
require.js:5:1795
    makeError http://localhost:63342/mywebsite/js/require.js:5
    onScriptError http://localhost:63342/mywebsite/js/require.js:5
    (Async: EventListener.handleEvent)
    load http://localhost:63342/mywebsite/js/require.js:5
    load http://localhost:63342/mywebsite/js/require.js:5
    load http://localhost:63342/mywebsite/js/require.js:5
    fetch http://localhost:63342/mywebsite/js/require.js:5
    check http://localhost:63342/mywebsite/js/require.js:5
    enable http://localhost:63342/mywebsite/js/require.js:5
    enable http://localhost:63342/mywebsite/js/require.js:5
    enable http://localhost:63342/mywebsite/js/require.js:5
    bind http://localhost:63342/mywebsite/js/require.js:5
    each http://localhost:63342/mywebsite/js/require.js:5
    enable http://localhost:63342/mywebsite/js/require.js:5
    init http://localhost:63342/mywebsite/js/require.js:5
    s http://localhost:63342/mywebsite/js/require.js:5
    (Async: setTimeout handler)
    nextTick http://localhost:63342/mywebsite/js/require.js:5
    s http://localhost:63342/mywebsite/js/require.js:5
    requirejs http://localhost:63342/mywebsite/js/require.js:5
    <anonymous> Backbone

the error lines are part of the code above

Thanks in advance

2
  • Did you do npm install mysql? Commented Aug 16, 2020 at 11:30
  • yes, I did and a folder called node_modules is created and inside you can find the mysql folder Commented Aug 16, 2020 at 11:51

1 Answer 1

3

It looks to me like you're trying to load '../node_modules/mysql' from Javascript you are running in your browser. You Can't Do That™.

When you use nodejs, you run Javascript both on your server and in your browser. But the two flavors of Javascript are different from each other, and it's important to keep them straight. (When you develop web apps with Java, not Javascript, it's easier to keep them straight because they're two different languages.)

To load npm modules like mysql in server Javascript (nodejs Javascript) you do this:

mysql = require('mysql')

nodejs knows to look in node_modules for modules like that. Then, you use express or some other web-server framework to build a web server to deliver information to your user's browser. (express fills a similar function to spring or jsf in the Java server world.)

Sign up to request clarification or add additional context in comments.

4 Comments

hey there, thanks for your quick answer! Your approach was the first one i tried, but i got this error: ``` Uncaught Error: Module name "mysql" has not been loaded yet for context: _. Use require([]) requirejs.org/docs/errors.html#notloaded makeError localhost:63342/mywebsite/js/require.js:5 s localhost:63342/mywebsite/js/require.js:5 requirejs localhost:63342/mywebsite/js/require.js:5 ``` Even if I use Express, it won't even load because of this error
Again, those error messages show you're trying to load the mysql module in your browser's Javascript code. How do I know that? Your error messages mention a localhost:63342 URL. mysql belongs in your server Javascript code. Remove any mention of MySQL from Javascript mentioned in <script...> tags. And, find a better tutorial; the one you have is getting you wrapped around the axle.
I tried to find a better tutorial, so I headed up to the official github site: github.com/mysqljs/mysql#install Almost all tutorials recommend such an approach, so I created a sepparate js file to test it out: now I get an Uncaught ReferenceError: require is undefined. Also, what do you mean by "server javascript code" ?
Don't use requirejs(). It's not suitable for your purpose. Work through this tutorial; it will help you understand what Javascript runs on the server, and what runs in the browser. developer.mozilla.org/en-US/docs/Learn/Server-side/…. Then look at this one that shows examples of using MySql. dev.to/achowba/build-a-simple-app-using-node-js-and-mysql-19me Soon you'll see how all this fits together.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.