I need to upload file and I am using node js in server(version : [nodemon] v1.0.1). The following code worked before 4 months after that I did n't check it and yesterday I run it again. But it is not working and I got "TypeError: Object #<Object> has no method 'existsSync'"
error in console. Following is my code
var express = require('express');
var path = require('path');
var app = module.exports = express();
var calenderid;
var EventEmitter = require('events').EventEmitter, https = require('https'), http = require('http'), querystring = require('querystring'), url = require('url');
var path2;
app.configure(function() {
app.use(expresspress());
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.cookieParser());
app.use(express.session({
secret : 'foobar'
}));
app.use(express.bodyParser({
uploadDir : __dirname + '/uploads'
}));
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
var http = require('http');
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
port : '3306',
database : 'db',
user : 'root',
password : '',
});
connection.connect();
var fs = require('fs');
app.post('/file-upload/:id', function(req, res) {
var tble = "image";
var id = req.params.id;
console.log("req.files.files" + JSON.stringify(req.files),
req.files.files.length);
var tmp_path = req.files.files[0].path;
console.log("hiii temp path" + tmp_path, typeof tmp_path);
// var firstIdx =0
var lastidx = tmp_path.lastIndexOf("/");
var path = tmp_path.substring(0, lastidx);
console.log("target path sub" + lastidx, tmp_path);
// var target_path = path+"\"+req.files.files[0].name;
var dirExists = fs.existsSync(__dirname + "/uploads/" + id);
console.log(dirExists);
if (!dirExists) {
fs.mkdirSync(__dirname + "/uploads/" + id);
target_path = path + "/" + id + "/" + req.files.files[0].name;
} else {
target_path = path + "/" + id + "/" + req.files.files[0].name;
}
console.log("hiii target_path" + target_path);
fs.rename(tmp_path, target_path, function(err) {
if (err)
throw err;
// delete the temporary file, so that the explicitly set temporary
// upload dir does not get filled with unwanted files
fs.unlink(tmp_path, function() {
if (err)
throw err;
// res.send('File uploaded to: ' + target_path + ' - ' +
// req.files.files[0].size + ' bytes'+req.files.files.length);
// queryurl = 'SELECT * FROM ' +tble + ' WHERE ' + id;
queryurl = 'SELECT * FROM ' + tble + ' WHERE image=' + '"'
+ req.files.files[0].name + '"' + ' AND branch=' + '"' + id
+ '"';
connection.query(queryurl, function(err, result, fields) {
console.log(JSON.stringify(result) + "result.image");
if (result.length == 0) {
console.log("in if !result");
connection.query('INSERT INTO ' + tble + '(' + 'branch'
+ ',' + 'image' + ')' + 'VALUES' + '(' + '"' + id
+ '"' + ',' + '"' + req.files.files[0].name + '"'
+ ")", function(err, result, fields) {
if (err)
throw err;
else {
res.send('File uploaded to: ' + target_path + ' - '
+ req.files.files[0].size + ' bytes'
+ req.files.files.length);
}
});
} else {
console.log("in else !result");
res.send("duplicate");
}
});
});
});
});
Following is the error that I got from console.
TypeError: Object #<Object> has no method 'existsSync'
at /var/www/jts-web/WebContent/app.js:95:20
at callbacks (/var/www/web/WebContent/node_modules/express/lib/router/index.js:165:11)
at param (/var/www/web/WebContent/node_modules/express/lib/router/index.js:139:11)
at param (/var/www/web/WebContent/node_modules/express/lib/router/index.js:136:11)
at pass (/var/www/web/WebContent/node_modules/express/lib/router/index.js:146:5)
at Router._dispatch (/var/www/web/WebContent/node_modules/express/lib/router/index.js:173:5)
at Object.router [as handle] (/var/www/web/WebContent/node_modules/express/lib/router/index.js:33:10)
at next (/var/www/web/WebContent/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at Object.methodOverride [as handle] (/var/www/web/WebContent/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:37:5)
at next (/var/www/web/WebContent/node_modules/express/node_modules/connect/lib/proto.js:190:15)
In one site somebody told it will overcome after upgrading node js. But this code was working fine. It is not good if I need to upgrade for each change.
I need to upload file and I am using node js in server(version : [nodemon] v1.0.1). The following code worked before 4 months after that I did n't check it and yesterday I run it again. But it is not working and I got "TypeError: Object #<Object> has no method 'existsSync'"
error in console. Following is my code
var express = require('express');
var path = require('path');
var app = module.exports = express();
var calenderid;
var EventEmitter = require('events').EventEmitter, https = require('https'), http = require('http'), querystring = require('querystring'), url = require('url');
var path2;
app.configure(function() {
app.use(express.compress());
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.cookieParser());
app.use(express.session({
secret : 'foobar'
}));
app.use(express.bodyParser({
uploadDir : __dirname + '/uploads'
}));
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
var http = require('http');
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
port : '3306',
database : 'db',
user : 'root',
password : '',
});
connection.connect();
var fs = require('fs');
app.post('/file-upload/:id', function(req, res) {
var tble = "image";
var id = req.params.id;
console.log("req.files.files" + JSON.stringify(req.files),
req.files.files.length);
var tmp_path = req.files.files[0].path;
console.log("hiii temp path" + tmp_path, typeof tmp_path);
// var firstIdx =0
var lastidx = tmp_path.lastIndexOf("/");
var path = tmp_path.substring(0, lastidx);
console.log("target path sub" + lastidx, tmp_path);
// var target_path = path+"\"+req.files.files[0].name;
var dirExists = fs.existsSync(__dirname + "/uploads/" + id);
console.log(dirExists);
if (!dirExists) {
fs.mkdirSync(__dirname + "/uploads/" + id);
target_path = path + "/" + id + "/" + req.files.files[0].name;
} else {
target_path = path + "/" + id + "/" + req.files.files[0].name;
}
console.log("hiii target_path" + target_path);
fs.rename(tmp_path, target_path, function(err) {
if (err)
throw err;
// delete the temporary file, so that the explicitly set temporary
// upload dir does not get filled with unwanted files
fs.unlink(tmp_path, function() {
if (err)
throw err;
// res.send('File uploaded to: ' + target_path + ' - ' +
// req.files.files[0].size + ' bytes'+req.files.files.length);
// queryurl = 'SELECT * FROM ' +tble + ' WHERE ' + id;
queryurl = 'SELECT * FROM ' + tble + ' WHERE image=' + '"'
+ req.files.files[0].name + '"' + ' AND branch=' + '"' + id
+ '"';
connection.query(queryurl, function(err, result, fields) {
console.log(JSON.stringify(result) + "result.image");
if (result.length == 0) {
console.log("in if !result");
connection.query('INSERT INTO ' + tble + '(' + 'branch'
+ ',' + 'image' + ')' + 'VALUES' + '(' + '"' + id
+ '"' + ',' + '"' + req.files.files[0].name + '"'
+ ")", function(err, result, fields) {
if (err)
throw err;
else {
res.send('File uploaded to: ' + target_path + ' - '
+ req.files.files[0].size + ' bytes'
+ req.files.files.length);
}
});
} else {
console.log("in else !result");
res.send("duplicate");
}
});
});
});
});
Following is the error that I got from console.
TypeError: Object #<Object> has no method 'existsSync'
at /var/www/jts-web/WebContent/app.js:95:20
at callbacks (/var/www/web/WebContent/node_modules/express/lib/router/index.js:165:11)
at param (/var/www/web/WebContent/node_modules/express/lib/router/index.js:139:11)
at param (/var/www/web/WebContent/node_modules/express/lib/router/index.js:136:11)
at pass (/var/www/web/WebContent/node_modules/express/lib/router/index.js:146:5)
at Router._dispatch (/var/www/web/WebContent/node_modules/express/lib/router/index.js:173:5)
at Object.router [as handle] (/var/www/web/WebContent/node_modules/express/lib/router/index.js:33:10)
at next (/var/www/web/WebContent/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at Object.methodOverride [as handle] (/var/www/web/WebContent/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:37:5)
at next (/var/www/web/WebContent/node_modules/express/node_modules/connect/lib/proto.js:190:15)
In one site somebody told it will overcome after upgrading node js. But this code was working fine. It is not good if I need to upgrade for each change.
Share Improve this question edited Feb 26, 2014 at 11:14 Parvathy asked Feb 25, 2014 at 4:59 ParvathyParvathy 2,4554 gold badges26 silver badges39 bronze badges 4 |3 Answers
Reset to default 12I solved this issue by adding the following code
fs.existsSync = require('path').existsSync;
above
var dirExists = fs.existsSync(__dirname + "/uploads/" + id);
I got solution from this site
As a side note, as I encountered somewhat *similar* issue but on different scenario.
I tried to run grunt for bootstrap. Here is the error I got:
/home/user/gbootstrap/node_modules/grunt/lib/grunt/file.js:374
return fs.existsSync(filepath);
^
TypeError: Object #<Object> has no method 'existsSync'
at Object.exists (/home/user/gbootstrap/node_modules/grunt/lib/grunt/file.js:374:13)
at Task.init (/home/user/gbootstrap/node_modules/grunt/lib/grunt/task.js:423:31)
at Object.initTasks (/home/user/gbootstrap/node_modules/grunt/lib/grunt/help.js:90:14)
at /home/user/gbootstrap/node_modules/grunt/lib/grunt/help.js:49:55
at Array.forEach (native)
at Object.display (/home/user/gbootstrap/node_modules/grunt/lib/grunt/help.js:49:17)
at Object.tasks (/home/user/gbootstrap/node_modules/grunt/lib/grunt.js:101:10)
at Object.cli (/home/user/gbootstrap/node_modules/grunt/lib/grunt/cli.js:38:9)
at Object.<anonymous> (/home/user/gnode-v0.10.26-linux-x86/lib/node_modules/grunt-cli/bin/grunt:45:20)
at Module._compile (module.js:446:26)
I found the root cause was due to i am using the default debian nodejs package (version 0.6.19) from repository.
Solution:
1. remove the nodejs:
sudo apt-get remove nodejs
2. download the binary from http://nodejs.org/download/
3. use the correct node and npm from the bin folder. Then it runs...
credit to this post: https://github.com/Crisu83/yii-app/issues/13
Make sure you load the core fs
module at the top of this file like var fs = require("fs");
. Other that that, existsSync
should be there.
Side note: You can't use synchronous IO calls in a network server such as this. It completely destroys node's concurrency model at a basic and fundamental level. Use the async versions. You're already using some of them, just use async version exclusively and you'll be noding correctly.
fs
. it should bevar fs = require('fs').
– Christian Fritz Commented Feb 25, 2014 at 5:02sync
methods in server code. – vkurchatkin Commented Feb 25, 2014 at 5:11