I have installed this database plugin for Node.js called 'db-mysql' and I've gone through all the steps mentioned on the site without getting errors, which must mean that the plugin is installed. But when I run my .js file (given below) in the Apache server I get this error.
Error: Cannot find module 'db-mysql'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/var/www/server.js:4:13)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
Here is the sample code I'm running.
var http = require("http");
var mysql = require("db-mysql");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
//Database Connectivity//
new mysql.Database({
hostname: 'localhost',
user: 'root',
password: '',
database: 'test'
}).on('error', function(error) {
console.log('ERROR: ' + error);
}).on('ready', function(server) {
console.log('Connected to ' + server.hostname + ' (' + server.version + ')');
}).connect();
///////////////////////
response.end();
}).listen(8889);
Did I miss anything during installation?
If you install module like in example
npm install db-mysql
Node will search it here
./project_root/node_modules/db-mysql
So make sure this path is correct in your case.
Other option is to install it globally by using -g flag
npm install db-mysql -g
Simply make a sym link that points to where your modules are physically installed
Name this link node_modules and put it inside the folder of your project
Related
I am using node.js to connect to Mysql database. I succeded in making the connection. However, several attempts to insert data have failed.
Can somoeone please look at the code of the js file executed in node ?
This is the js file to execute by node.js:
let mysql = require("mysql")
connection = mysql.createConnection({
host : 'localhost',
user : 'daniel',
password : '',
database : 'company',
});
connection.connect()
connection.query('INSERT INTO Employees (emp_no,emp_name,salary,reports_to) VALUES (1, "Manos", 1500, "Director")');
And this is the error I get:
node mysqlconnection3.js
/root/nodejs/mysqlconnection3.js:1
(function (exports, require, module, __filename, __dirname) { \// Read the docs at https://www.npmjs.com/package/mysql.
^
SyntaxError: Invalid or unexpected token
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
Nodejs version : 4.4.7
NPM version: 2.15.8
I am running the following code, which is literally copied from the following URL
https://kafka.js.org/docs/getting-started
const { Kafka } = require('kafkajs')
const kafka = new Kafka({
clientId: 'my-app',
brokers: ['kafka1:9092', 'kafka2:9092']
})
The above code is saved in "apps.js" file under a "simpleapp" folder. In addition to that I have installed the following
npm install kafkajs
When I run the app.js file I get the following error
/Users/nick/Documents/nodeprojects/simpleapp/apps.js:1
(function (exports, require, module, __filename, __dirname) { const { Kafka } = require('kafkajs')
^
SyntaxError: Unexpected token {
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
What is the issue?
Version 4.4.7 of NodeJS is very old, and isn't even supported anymore. KafkaJS follows the same support schedule as NodeJS itself, so the oldest currently supported version is 8.
Please advise i want to minify the .css files of my project with gulp so i have written the below task in my gulp.js file as shown below but still upon execution i am getting the error , please advise how to fix the below error
below is the code that I have written is
var gulp = require('gulp');
var minifyCss = require("gulp-minify-css");
gulp.task('minify-css', function () {
gulp.src('C:\\Softwares\\Spring4MVCAngularJSExample\\target\\Spring4MVCAngularJSExample\\static\\css\\*.css') // path to your file
.pipe(minifyCss())
.pipe(gulp.dest('C:\\Softwares\\Spring4MVCAngularJSExample\\target\\Spring4MVCAngularJSExample\\static\\cssoutput'));
});
below is the error i am getting upon execution
C:\Softwares\Spring4MVCAngularJSExample>gulp minify-css
module.js:471
throw err;
^
Error: Cannot find module 'gulp-minify-css'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:\Softwares\Spring4MVCAngularJSExample\gulpfile.js:10:13)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
It appears that it cannot locate the module. Have you run npm install gulp-minify-css --save-dev?
You may want to try re-installing your local packages / dependencies:
rm -rf node_modules/
npm install
When install a foo module, as developer, I want to write ~/.foo.json file (where ~/ is the user's home directory).
For this I did in package.json:
{
...
"scripts": {
"preinstall": "./installation/preinstall.js"
},
...
}
And in /installation/preinstall.js (that is executable), I have:
#!/usr/bin/env node
// Dependencies
var Fs = require("fs");
function getUserHome() {
return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
}
console.log("Creating configuration file ...")
Fs.writeFileSync(getUserHome() + "/" + ".foo.json", JSON.stringify(
require("./sample-config"), null, 4
));
When running sudo npm install ...#... -g I get the following output:
ionicabizau#laptop:~$ sudo npm install ...#...-alpha1 -g
npm http GET https://registry.npmjs.org/...
npm http 304 https://registry.npmjs.org/...
> ...#...-alpha1 preinstall /usr/lib/node_modules/...
> ./installation/preinstall.js
Creating configuration file ...
fs.js:432
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: EACCES, permission denied '/home/ionicabizau/.foo.json'
at Object.fs.openSync (fs.js:432:18)
at Object.fs.writeFileSync (fs.js:971:15)
at Object.<anonymous> (/usr/lib/node_modules/.../installation/preinstall.js:11:4)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
Why EACCESS error, even if I run it with sudo?
Running Ubuntu 14.04, if it's relevant.
According to documentation, If npm was invoked with root privileges, then it will change the uid to the user account or uid specified by the user config, which defaults to nobody. Set the unsafe-perm flag to run scripts with root privileges.
So, that's the problem here. The environment variables HOME or USERPROFILE remain the same, but the user is nobody.
However, --unsafe-perm prevents this:
sudo npm install foo -g --unsafe-perm
I want to achieve that node prints me a table from MySql on my Windows 7 (64bit) machine but fail. Sorry for the long post in advance, I tried to be complete, also I failed to find this error in the forum. The optimistic mysql.js file I typed in Notepad++ was
var mysql = require('node-mysql');
var connectParams = {'hostname' : 'localhost','user' : 'dev','password': 'dev','database' : 'upandrunning'};
var db = new mysql.Database(connectParams);
db.connect( function(err) {
if(err) return console.log('Failed to connect ' + err);
this.query()
.select(['id', 'user_login'])
.from('users')
.execute(
function(err, rows, columns){
if(err) return console.log('err on query ' + err);
console.log(rows);
})});
Node keeps returning
C:\dev\nodejs\0.10\tutorials\node-up-and-running>node tutorials\node-up-and-running\mysql.js
module.js:340
throw err;
^
Error: Cannot find module 'C:\dev\nodejs\0.10\tutorials\node-up-and-running\tutorials\node-up-and-running\mysql.js'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
Why??
Before the node command I told Squirrel to do
drop database if exists upandrunning;
create database upandrunning;
grant all privileges on upandrunning.* to 'dev'#'%' identified by 'dev';
use upandrunning;
create table users(
id int auto_increment primary key,
user_login varchar(25),
user_nicename varchar(75)
);
using my localhost root account (bad), but all seemed fine because squirrel object tab showed new upandrunning db.
Node.exe is on C:\dev\nodejs\0.10\ but from dos prompt:
C:\dev\nodejs\0.10\tutorials\node-up-and-running>node -v
gives
v0.10.26
My file mysql.js has been reduced to the mere
var mysql = require('node-mysql');
where the error must be. I ran the following commands from C:\dev\nodejs\0.10\tutorials\node-up-and-running>
npm install -g mysql
npm install -g node-mysql
npm install -g db-mysql
These were the ones i could find.
After each install the same error above.
Any suggestions please how to make node cooperate?
I admit I lack node trouble shooting skills, but studying module.js, should not be necessary.
Also, do you know of, and enjoy working in, a free and easy to work with code completion ide for node?
And is there some way to debug other than console.log() ?.
Lastly what are your ideas on layout conventions for reducing the closing brackets because they hurt the eyes.
The site suggested From NodeJS require a global module/package
but how do i get the node_path variable on dos prompt and how to set it:
C:\dev\nodejs\0.10\tutorials\node-up-and-running>set NODE_PATH
Environment variable NODE_PATH not defined
Ok set NODE_PATH=c:\dev\nodejs\0.10
C:\dev\nodejs\0.10\tutorials\node-up-and-running>node tutorials\node-up-and-running\mysql.js
'node' is not recognized as an internal or external command, operable program or batch file.
Closing and reopening dos makes this go away.
C:\dev\nodejs\0.10>set NODE_PATH=%NODE_PATH%;C:\dev\nodejs\0.10\node_modules
And it works.. But now it fails on
var mysql = require('mysql');
var connectParams = {'hostname' : 'localhost','user' : 'dev','password': 'dev','database' : 'upandrunning'};
var db = new mysql.Database(connectParams);
C:\dev\nodejs\0.10\mysql.js:3
var db = new mysql.Database(connectParams);
^
TypeError: undefined is not a function
at Object.<anonymous> (C:\dev\nodejs\0.10\mysql.js:3:10)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
and if i remove the new it says
C:\dev\nodejs\0.10\mysql.js:3
var db = mysql.Database(connectParams);
^
TypeError: Object # has no method 'Database'
at Object. (C:\dev\nodejs\0.10\mysql.js:3:17)
Using the REPL:
var mysql = require('mysql');
undefined
var connectParams = {'hostname' : 'localhost','user' : 'dev','password': 'dev'
,'database' : 'upandrunning'};
undefined
var db = new mysql.Database(connectParams);
TypeError: undefined is not a function
at repl:1:10
at REPLServer.self.eval (repl.js:110:21)
at repl.js:249:20
at REPLServer.self.eval (repl.js:122:7)
at Interface. (repl.js:239:12)
at Interface.EventEmitter.emit (events.js:95:17)
at Interface._onLine (readline.js:202:10)
at Interface._line (readline.js:531:8)
at Interface._ttyWrite (readline.js:760:14)
at ReadStream.onkeypress (readline.js:99:10)
Aren't you supposed to create the connection like this.. ?
connection = mysql.createConnection({
host : 'localhost',
user : 'node',
password : 'UnPassword',
database: 'nodesql'
});