1

I have react-boilerplate application. I want to start using database so I installed:

npm install mysql   
npm install mysqljs/mysql

As shown in mysql webpage: https://www.npmjs.com/package/mysql

Now I get errors when I go to localhost in a browser:

 Can't resolve 'fs' in '....node_modules\mysql\lib\protocol\sequences'
 Can't resolve 'net' in '....node_modules\mysql\lib'
 Can't resolve 'tls' in '....node_modules\mysql\lib'

I am using redux-saga combination. I figured out that I get the error when I write:

const mysql = require('mysql');

in saga.js file

How can I fix these errors?

vytaute
  • 1,260
  • 4
  • 16
  • 36

1 Answers1

0

You can't use a MySQL client library within a browser app.

The errors you're seeing is the client library attempting to require() Node.js standard libraries for file system, network and encryption (TLS) access, which don't exist in the browser.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • So what do I do? Previously with codemash.io database everything worked. I was getting my data in saga.js file, putting it into a state and everything was fine. Cant I simply switch to mysql and do the same simple thing? – vytaute Feb 21 '20 at 08:02
  • No, you can't, since MySQL doesn't speak HTTP(S) or Websockets, which are (for this purpose anyway) the only protocols your browser can speak. You'll need a backend service that connects to MySQL. – AKX Feb 21 '20 at 08:08
  • So maybe there are database for front enders? Maybe MONGO db? – vytaute Feb 21 '20 at 08:09
  • Depends on what you're trying to do. What's the reason for switching away from Codemash, if it worked? – AKX Feb 21 '20 at 08:11
  • Good question. It was free and now they are making it paid :( – vytaute Feb 21 '20 at 08:15
  • Well, Google's Firebase has a free tier, if it helps. It's accessible from the frontend. – AKX Feb 21 '20 at 08:21