Node js: TypeError: Object # <Object> does not have a method 'existsSync'

I need to upload a file, and I'm using node js on the server (version: [nodemon] v1.0.1). The following code worked up to 4 months after that, I did not test it, and yesterday I run it again. But it does not work, and I got an error "TypeError: Object #<Object> has no method 'existsSync'"in the console. Below 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");

                }
            });

        });
    });

});

The following is the error I received from the 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)

On one site, someone said he would overcome after upgrading node js. But this code worked fine. This is not good if I need to update for every change.

+3
source share
3 answers

I solved this problem by adding the following code

fs.existsSync = require('path').existsSync;

higher

var dirExists = fs.existsSync(__dirname + "/uploads/" + id); 

I got a solution from this site

+12
source

, * *, .

grunt . , :

 /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)

, , debian nodejs ( 0.6.19) .

:
1. nodejs:

sudo apt-get remove nodejs  

2. http://nodejs.org/download/
3. node npm bin. ...

: https://github.com/Crisu83/yii-app/issues/13

+5

, fs , var fs = require("fs");. , existsSync.

: - , . node concurrency . . , , .

+2
source

All Articles