setting up app.js using git bash error - javascript

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.

Related

How to fix npm run serve command bringing Relative Module was not found error

I have a blockchain project whose frontend is implemented in Vuejs. I have installed all the dependencies with "npm install" command. I am trying to set up the development server to serve the front end by running the "npm run serve" command in the /appvue root folder but get a compilation error as follows:
This relative module was not found:
../../build/contracts/SupplyChain.json in ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/App.vue?vue&type=script&lang=js&
I am new to VueJs but it looks like the Supplychain.json file cannot be found yet its clearly in the project structure attached.
https://github.com/kafukoM/project_3_Dapp_Supply_Chain
you need to replace line with import in your App.vue file line № 209
from this
import supplyChainArtifact from '../../../build/contracts/SupplyChain.json'
to
import supplyChainArtifact from '../../app/src/SupplyChain.json'

How to run node in VSCode properly?

$ node test.js
internal/modules/cjs/loader.js:883
throw err;
^
I tried all the ways (adding/checking PATH route for node js, restarting, using another files etc) and couldn't find why my node doesn't run properly. When I'm typing node -v it returns me version, so it is installed, I go to VSCode and open my any folder. Then in JS file when I'm typing for instance node app.js it throws me error that I wrote above.
Any idea what should I try else?
Seems like node modules are not properly installed.
Try these steps and let me know:
delete node_modules
delete package-lock.json file
npm install
npm start
should be good to go.

"Could not find task 'npm'" error in VS Code

Whenever I try to run a JavaScript or Python file in VS Code this error message appears:
I have tried reinstalling node but it still wont work. When I click on configure I am shown these options. When I click on any of them I am directed to this json file.
I am pretty new to coding so sorry if I missed anything. Let me know if I did, I would be happy to provide it.
PS: I see that my interpreter at the bottom left is set to python, but clicking on it I am not getting any options that make sense to me. And also, this same thing happens when I try to run a python file.
if you want to run npm commands you should go to a directory that your nodejs is installed... if you want to run npm commands in other directories you should go to the directory that you want and then run cmd and type in the commant
set path=C:\Program Files\nodejs\
then you can run all of the npm commands in your directory
In my case I didn't open the right directory (I was inside the project parent directory). Maybe it would help someone else who get into
The list of commands you see in there are what is called VS Code tasks, see details here - https://code.visualstudio.com/docs/editor/tasks
In your particular case they have been created to run npm commands from within VS Code without opening the terminal. These require a package.json file at the root of your project directory with corresponding script commands.
An example package.json file might look something like this:
{
"scripts": {
"install": "npm install",
"test": "node test.js",
"start": "node index.js"
}
}
WARNING: your project should have a package.json file with scripts that match what has been specified in your tasks.json file.
Please provide a working copy of you application somewhere online, e.g. CodeSandbox, if you require more help.

Cannot find module 'mysql'

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?

nodejs bootstrap project works local only

Ive been trying out different things, and looking up various ways to solve it, but I'm in need of some extra hints to make this work.
I have a working NodeJS Bootstrap project with Gulp. My website functions locally.
So in my project I have the fiollowing files (among other folders) :
gulpfile.js
index.html
package.json
When I locally run my project via 'gulp dev' everything seems perfect. On Heroku after deploying it gives me the error: npm ERR! missing script: start
This makes sense, because indeed my package.json doesn't have a start script. How can this work on local?
any leads?
thanks
node.js file
name it index.js
first before you start move all the current files into a new folder called public in the parent folder above public create the file index.js
once you are in the parent folder above public run this in the command line for the directory where you created the index.js file
npm i express --save
this will install a mini framework for node.js and save it to the project.json config
for the index.js file this is the content
/// START
var express = require('express');
var path = require('path')
var app = express();
var port = process.env.PORT || 80;
// this allows the files to be visible without having to type in the
// index.html for the url simply do / and you'll get the url
app.use(express.static(path.join(__dirname, 'public'),{
extensions: ['html']}
));
app.listen(port);
///END
you need to make sure that the index.js (node file) is in 1 folder above the rest of the project. The project should be in a folder called public in this case
before you upload this to the heroku get it working on your local machine
you need to run node index.js if you get any errors we can work through them but this is a really generic static serving node.js server.
last thing you need to do is go into the project.json file and add the following you can add it right above "title" in this case so it's easy to find later
"scripts": {
"start": "node index.js"
}
If the code is client-side, you should serve up the bundled HTML/CSS/JS in a static server. Doesn't make sense to run node just to serve up your static assets.
Ive never used heroku but for my node application that is hosted in AWS i had to add a start script to my package.json
"scripts": {
"start": "set NODE_ENV=production nodemon app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
You can use similar node start commands if youre not using nodemon.

Categories

Resources