Why am I encountering a foreach undefined error in this .js file running in node.. and how can I fix it?











up vote
0
down vote

favorite












I'm running the following myfile.js on node.

Executing it like so.. node myfile.js



However, it is resulting in a forEach undefined error... I think my syntax is correct too! Am I missing something?



The error I am getting is ... TypeError: Cannot read property 'forEach' of undefined



var fs = require('fs');
var path = require('path');
fs.readdir("/.lib", function(err,files){

files.forEach(function(fileName) {
var file = path.join(__dirname, "lib", fileName);
var stats = fs.statSync(file);

if (stats.isFile() && fileName !== ".DS_Store"){

fs.readFile(file,"UTF-8", function(err,contents){
console.log(contents);
});

};
});

});









share|improve this question


















  • 2




    You probably want ./lib as path, or at least ./.lib The error you're getting means files isn't an array. Probably because readdir failed.
    – Chris G
    Nov 19 at 20:40








  • 2




    Check that err parameter
    – darklightcode
    Nov 19 at 20:40










  • Check files, it's undefined
    – Ariel Alvarado
    Nov 19 at 20:41










  • Probably need to put an if(files) before your file handling block, the callback is firing before you get a result.
    – chairmanmow
    Nov 19 at 20:42










  • Thanks all.. So lib is a subfolder, it was an error on my part.. Instead of /.lib, like Chris G mentioned it is ./lib and that solved it!! I was pretty sure my syntax was correct too lol. Thanks much Chris G
    – Ramona
    Nov 19 at 20:45















up vote
0
down vote

favorite












I'm running the following myfile.js on node.

Executing it like so.. node myfile.js



However, it is resulting in a forEach undefined error... I think my syntax is correct too! Am I missing something?



The error I am getting is ... TypeError: Cannot read property 'forEach' of undefined



var fs = require('fs');
var path = require('path');
fs.readdir("/.lib", function(err,files){

files.forEach(function(fileName) {
var file = path.join(__dirname, "lib", fileName);
var stats = fs.statSync(file);

if (stats.isFile() && fileName !== ".DS_Store"){

fs.readFile(file,"UTF-8", function(err,contents){
console.log(contents);
});

};
});

});









share|improve this question


















  • 2




    You probably want ./lib as path, or at least ./.lib The error you're getting means files isn't an array. Probably because readdir failed.
    – Chris G
    Nov 19 at 20:40








  • 2




    Check that err parameter
    – darklightcode
    Nov 19 at 20:40










  • Check files, it's undefined
    – Ariel Alvarado
    Nov 19 at 20:41










  • Probably need to put an if(files) before your file handling block, the callback is firing before you get a result.
    – chairmanmow
    Nov 19 at 20:42










  • Thanks all.. So lib is a subfolder, it was an error on my part.. Instead of /.lib, like Chris G mentioned it is ./lib and that solved it!! I was pretty sure my syntax was correct too lol. Thanks much Chris G
    – Ramona
    Nov 19 at 20:45













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm running the following myfile.js on node.

Executing it like so.. node myfile.js



However, it is resulting in a forEach undefined error... I think my syntax is correct too! Am I missing something?



The error I am getting is ... TypeError: Cannot read property 'forEach' of undefined



var fs = require('fs');
var path = require('path');
fs.readdir("/.lib", function(err,files){

files.forEach(function(fileName) {
var file = path.join(__dirname, "lib", fileName);
var stats = fs.statSync(file);

if (stats.isFile() && fileName !== ".DS_Store"){

fs.readFile(file,"UTF-8", function(err,contents){
console.log(contents);
});

};
});

});









share|improve this question













I'm running the following myfile.js on node.

Executing it like so.. node myfile.js



However, it is resulting in a forEach undefined error... I think my syntax is correct too! Am I missing something?



The error I am getting is ... TypeError: Cannot read property 'forEach' of undefined



var fs = require('fs');
var path = require('path');
fs.readdir("/.lib", function(err,files){

files.forEach(function(fileName) {
var file = path.join(__dirname, "lib", fileName);
var stats = fs.statSync(file);

if (stats.isFile() && fileName !== ".DS_Store"){

fs.readFile(file,"UTF-8", function(err,contents){
console.log(contents);
});

};
});

});






javascript node.js






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 20:39









Ramona

648




648








  • 2




    You probably want ./lib as path, or at least ./.lib The error you're getting means files isn't an array. Probably because readdir failed.
    – Chris G
    Nov 19 at 20:40








  • 2




    Check that err parameter
    – darklightcode
    Nov 19 at 20:40










  • Check files, it's undefined
    – Ariel Alvarado
    Nov 19 at 20:41










  • Probably need to put an if(files) before your file handling block, the callback is firing before you get a result.
    – chairmanmow
    Nov 19 at 20:42










  • Thanks all.. So lib is a subfolder, it was an error on my part.. Instead of /.lib, like Chris G mentioned it is ./lib and that solved it!! I was pretty sure my syntax was correct too lol. Thanks much Chris G
    – Ramona
    Nov 19 at 20:45














  • 2




    You probably want ./lib as path, or at least ./.lib The error you're getting means files isn't an array. Probably because readdir failed.
    – Chris G
    Nov 19 at 20:40








  • 2




    Check that err parameter
    – darklightcode
    Nov 19 at 20:40










  • Check files, it's undefined
    – Ariel Alvarado
    Nov 19 at 20:41










  • Probably need to put an if(files) before your file handling block, the callback is firing before you get a result.
    – chairmanmow
    Nov 19 at 20:42










  • Thanks all.. So lib is a subfolder, it was an error on my part.. Instead of /.lib, like Chris G mentioned it is ./lib and that solved it!! I was pretty sure my syntax was correct too lol. Thanks much Chris G
    – Ramona
    Nov 19 at 20:45








2




2




You probably want ./lib as path, or at least ./.lib The error you're getting means files isn't an array. Probably because readdir failed.
– Chris G
Nov 19 at 20:40






You probably want ./lib as path, or at least ./.lib The error you're getting means files isn't an array. Probably because readdir failed.
– Chris G
Nov 19 at 20:40






2




2




Check that err parameter
– darklightcode
Nov 19 at 20:40




Check that err parameter
– darklightcode
Nov 19 at 20:40












Check files, it's undefined
– Ariel Alvarado
Nov 19 at 20:41




Check files, it's undefined
– Ariel Alvarado
Nov 19 at 20:41












Probably need to put an if(files) before your file handling block, the callback is firing before you get a result.
– chairmanmow
Nov 19 at 20:42




Probably need to put an if(files) before your file handling block, the callback is firing before you get a result.
– chairmanmow
Nov 19 at 20:42












Thanks all.. So lib is a subfolder, it was an error on my part.. Instead of /.lib, like Chris G mentioned it is ./lib and that solved it!! I was pretty sure my syntax was correct too lol. Thanks much Chris G
– Ramona
Nov 19 at 20:45




Thanks all.. So lib is a subfolder, it was an error on my part.. Instead of /.lib, like Chris G mentioned it is ./lib and that solved it!! I was pretty sure my syntax was correct too lol. Thanks much Chris G
– Ramona
Nov 19 at 20:45












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Check my example below to see how you can improve your code.
First I checked that the folder is present, then i had to make sure errors are thrown and then an if to see if there are any files.






var fs = require('fs');
var path = require('path');
var searchPath = './lib';

if (fs.existsSync(searchPath)) { // `existsSync` is recommended to be used. `exists` is deprecated

fs.readdir(searchPath, function(err, files) {

if (err) {
// fix error
throw err;
}

if (!files.length) {
console.log('Empty folder');
} else {

files.forEach(function(fileName) {
var file = path.join(__dirname, "lib", fileName);
var stats = fs.statSync(file);

var excludeFiles = ['.DS_Store']

if (stats.isFile() && excludeFiles.indexOf(fileName) === -1) {

fs.readFile(file, "UTF-8", function(err, contents) {
console.log(contents);
});

};
});

}

});

} else {
console.log('Folder doesn't exist')
}








share|improve this answer





















  • Your comment doesn't really make any sense. The reason exists is deprecated is to discourage the antipattern of checking the existence of a file or directory before reading from it. The correct pattern is to attempt and then handle the error if it happens to not exist.
    – Patrick Roberts
    Nov 19 at 21:35











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382298%2fwhy-am-i-encountering-a-foreach-undefined-error-in-this-js-file-running-in-node%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Check my example below to see how you can improve your code.
First I checked that the folder is present, then i had to make sure errors are thrown and then an if to see if there are any files.






var fs = require('fs');
var path = require('path');
var searchPath = './lib';

if (fs.existsSync(searchPath)) { // `existsSync` is recommended to be used. `exists` is deprecated

fs.readdir(searchPath, function(err, files) {

if (err) {
// fix error
throw err;
}

if (!files.length) {
console.log('Empty folder');
} else {

files.forEach(function(fileName) {
var file = path.join(__dirname, "lib", fileName);
var stats = fs.statSync(file);

var excludeFiles = ['.DS_Store']

if (stats.isFile() && excludeFiles.indexOf(fileName) === -1) {

fs.readFile(file, "UTF-8", function(err, contents) {
console.log(contents);
});

};
});

}

});

} else {
console.log('Folder doesn't exist')
}








share|improve this answer





















  • Your comment doesn't really make any sense. The reason exists is deprecated is to discourage the antipattern of checking the existence of a file or directory before reading from it. The correct pattern is to attempt and then handle the error if it happens to not exist.
    – Patrick Roberts
    Nov 19 at 21:35















up vote
0
down vote













Check my example below to see how you can improve your code.
First I checked that the folder is present, then i had to make sure errors are thrown and then an if to see if there are any files.






var fs = require('fs');
var path = require('path');
var searchPath = './lib';

if (fs.existsSync(searchPath)) { // `existsSync` is recommended to be used. `exists` is deprecated

fs.readdir(searchPath, function(err, files) {

if (err) {
// fix error
throw err;
}

if (!files.length) {
console.log('Empty folder');
} else {

files.forEach(function(fileName) {
var file = path.join(__dirname, "lib", fileName);
var stats = fs.statSync(file);

var excludeFiles = ['.DS_Store']

if (stats.isFile() && excludeFiles.indexOf(fileName) === -1) {

fs.readFile(file, "UTF-8", function(err, contents) {
console.log(contents);
});

};
});

}

});

} else {
console.log('Folder doesn't exist')
}








share|improve this answer





















  • Your comment doesn't really make any sense. The reason exists is deprecated is to discourage the antipattern of checking the existence of a file or directory before reading from it. The correct pattern is to attempt and then handle the error if it happens to not exist.
    – Patrick Roberts
    Nov 19 at 21:35













up vote
0
down vote










up vote
0
down vote









Check my example below to see how you can improve your code.
First I checked that the folder is present, then i had to make sure errors are thrown and then an if to see if there are any files.






var fs = require('fs');
var path = require('path');
var searchPath = './lib';

if (fs.existsSync(searchPath)) { // `existsSync` is recommended to be used. `exists` is deprecated

fs.readdir(searchPath, function(err, files) {

if (err) {
// fix error
throw err;
}

if (!files.length) {
console.log('Empty folder');
} else {

files.forEach(function(fileName) {
var file = path.join(__dirname, "lib", fileName);
var stats = fs.statSync(file);

var excludeFiles = ['.DS_Store']

if (stats.isFile() && excludeFiles.indexOf(fileName) === -1) {

fs.readFile(file, "UTF-8", function(err, contents) {
console.log(contents);
});

};
});

}

});

} else {
console.log('Folder doesn't exist')
}








share|improve this answer












Check my example below to see how you can improve your code.
First I checked that the folder is present, then i had to make sure errors are thrown and then an if to see if there are any files.






var fs = require('fs');
var path = require('path');
var searchPath = './lib';

if (fs.existsSync(searchPath)) { // `existsSync` is recommended to be used. `exists` is deprecated

fs.readdir(searchPath, function(err, files) {

if (err) {
// fix error
throw err;
}

if (!files.length) {
console.log('Empty folder');
} else {

files.forEach(function(fileName) {
var file = path.join(__dirname, "lib", fileName);
var stats = fs.statSync(file);

var excludeFiles = ['.DS_Store']

if (stats.isFile() && excludeFiles.indexOf(fileName) === -1) {

fs.readFile(file, "UTF-8", function(err, contents) {
console.log(contents);
});

};
});

}

});

} else {
console.log('Folder doesn't exist')
}








var fs = require('fs');
var path = require('path');
var searchPath = './lib';

if (fs.existsSync(searchPath)) { // `existsSync` is recommended to be used. `exists` is deprecated

fs.readdir(searchPath, function(err, files) {

if (err) {
// fix error
throw err;
}

if (!files.length) {
console.log('Empty folder');
} else {

files.forEach(function(fileName) {
var file = path.join(__dirname, "lib", fileName);
var stats = fs.statSync(file);

var excludeFiles = ['.DS_Store']

if (stats.isFile() && excludeFiles.indexOf(fileName) === -1) {

fs.readFile(file, "UTF-8", function(err, contents) {
console.log(contents);
});

};
});

}

});

} else {
console.log('Folder doesn't exist')
}





var fs = require('fs');
var path = require('path');
var searchPath = './lib';

if (fs.existsSync(searchPath)) { // `existsSync` is recommended to be used. `exists` is deprecated

fs.readdir(searchPath, function(err, files) {

if (err) {
// fix error
throw err;
}

if (!files.length) {
console.log('Empty folder');
} else {

files.forEach(function(fileName) {
var file = path.join(__dirname, "lib", fileName);
var stats = fs.statSync(file);

var excludeFiles = ['.DS_Store']

if (stats.isFile() && excludeFiles.indexOf(fileName) === -1) {

fs.readFile(file, "UTF-8", function(err, contents) {
console.log(contents);
});

};
});

}

});

} else {
console.log('Folder doesn't exist')
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 20:51









darklightcode

1,15279




1,15279












  • Your comment doesn't really make any sense. The reason exists is deprecated is to discourage the antipattern of checking the existence of a file or directory before reading from it. The correct pattern is to attempt and then handle the error if it happens to not exist.
    – Patrick Roberts
    Nov 19 at 21:35


















  • Your comment doesn't really make any sense. The reason exists is deprecated is to discourage the antipattern of checking the existence of a file or directory before reading from it. The correct pattern is to attempt and then handle the error if it happens to not exist.
    – Patrick Roberts
    Nov 19 at 21:35
















Your comment doesn't really make any sense. The reason exists is deprecated is to discourage the antipattern of checking the existence of a file or directory before reading from it. The correct pattern is to attempt and then handle the error if it happens to not exist.
– Patrick Roberts
Nov 19 at 21:35




Your comment doesn't really make any sense. The reason exists is deprecated is to discourage the antipattern of checking the existence of a file or directory before reading from it. The correct pattern is to attempt and then handle the error if it happens to not exist.
– Patrick Roberts
Nov 19 at 21:35


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382298%2fwhy-am-i-encountering-a-foreach-undefined-error-in-this-js-file-running-in-node%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Paul Cézanne

UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

Angular material date-picker (MatDatepicker) auto completes the date on focus out