Cannot find module 'mysql' - javascript

I went through the different proposed answers related to this problem while installing node.js mysql.
I tried about everything and I still get the same error message: "Cannot find module 'mysql'" either locally or on a webserver.
Checks I've performed:
Packages and dependencies are installed in myproject/node_modules
==> check
Run my database.js test script going into the folder and via the command line typing "node database.js" => this works
Tried to load in my HTML document the index.js file from node_modules/mysql
before loading the file database.js => ReferenceError: exports is not defined. Also tried to put database.js in the main folder, same error.
I haven't trying copying my project folder in the c:/some-dir/nodejs because it is supposed to run on a webserver.
Any clue what's the issue here?

Related

Error: ENOENT: no such file or directory when running a CLI app

I was coding a small CLI app and it works perfectly fine when running it directly using the "node" command, I added it to my PATH using "npm -g" and I also added the bin property on my package.json.
The command is recognized by my terminal, but it throws the following error:
Error: ENOENT: no such file or directory, open 'classes.json'
classes.json is a file that I'm reading and writing to and it's on the same directory as the index.js file.
The command works when running it on the directory of my node project.
How do I solve this?
The fs.readFileSync looks for a file in the working directory of your shell, the one you are currently. If you are on a Unix-like system (Linux/Mac) or using Bash for Windows you can type pwd on your terminal to see your current directory.
Using an absolute path is not ideal because you don't know where that CLI will be placed on someone else's computer.
Although for JSON files this is not the approach I'd recommend, the way to fix your code independently where the files are could be something like this:
const path = require('path')
const classJsonFile = path.join(__dirname,"classes.json")
const classes = JSON.parse(fs.readFileSync(classJsonFile, "utf-8"))
For loading JSON files I'd recommend using the require function instead of fs then parsing the JSON. (assuming you are coding it in CJS not MJS)
const classes = require("classes.json");
Much simpler and cleaner.

Module not found: Can't resolve '../styles/global.css' in an next.js project

I am following the instruction on :
https://nextjs.org/learn/basics/assets-metadata-css/global-styles
And getting error message when adding import '../styles/global.css'
to _app.js
./pages/_app.js
Module not found: Can't resolve '../styles/global.css' in 'C:\Users\Owner\Documents\tradingCFD\pages'
Your global.css seems to be in public/styles., not just styles/. The styles directory should be moved up a level to be in the app root.
I was having this issue. The tutorial tells you to restart the server, do it.
Restart the Development Server
Important: You need to restart the development server when you add pages/_app.js. Press Ctrl + c to stop
the server and run:
npm run dev
If you put _app and global in the correct folders, it should run OK.
Not exactly the same problem as the OP but in my case, using styled-components createGlobalStyle, I incorrectly referenced the file as ../styles/global and it build fine locally even though the filename was Global.js. The build on Vercel wasn't so lenient.
TLDR:
double check capitalization on file refs 😅

NextJS cannot read jsx-loader

I'm trying to set up new Next.js application. When i install all packages (using npx create-next-app or installing with myself like npm i -s next react react-dom) and when i start app, i always see this error:
Module parse failed: Unexpected token (6:2)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
I read that what they wrote before and i tried to install jsx-loader and tried to set up it in webpack.config.js but it doesn't work at all. I already tried to set up project 5 times and i always get this error. Can you help me?
Thanks :)

Heroku doesn't find my index.js, however my index.js is effectively in my directory

Edit : this answer works for me.
While trying to git push to Heroku as master, my console returns me that Heroku failed to find the client/src/index.js file, but in fact it is effectively in my directory.
My build works fine in local but fail when I try to push it in a Heroku production's environment.
So far I have make some manips as :
commituntracked files
git add -A
delete .git folder of these directories.
my .gitignore are clean, no index.js specified in'
But it still doesn't work. My console returns me :
Could not find a required file.
Name: index.js
Searched in: /tmp/build_a371b974c2c8e8fdf83c4e7f613805b2/client/src
So what can I do, if anyone have an hint,
Thanks

setting up app.js using git bash error

I am a beginner for this one and I want to learn from the start. I want to set up app.js in my sp-node-mysql directory to use mysql through javascript. But I can't get through.
Here is the line of code: var mysql = require("mysql");
and the error: bash: syntax error near unexpected token '('
I think there is nothing wrong with the line of code. But it is still showing this error message.
To get started do the following:
In your projects root directory type the following into the command line:
"npm init"
"npm install mysql --save"
This will create a package.json file and a node_modules folder in your projects root directory. Within the node_modules folder you will have the mysql module installed. Nothing else needs to be done with this.
Then in the project root directory create a file called app.js.
Within app.js write the following code.
var mysql = require('mysql')
console.log('No errors during require')
process.exit()
Then execute your node program by typing "node app" or "node app.js" on the command line within your projects root folder.
Let me know if that works for you.

Categories

Resources