Node.js name too long, scandir - node.js

I have a folder structure which starts at my project
Note that user.hash and user are MD5 hash
root/data/${user.hash}/
Now, what i need to do is read the files in side that directory using:
var companies = fs.readdirSync(`../data/${user}/`);
I also tried
var BASE_FOLDER = path.resolve(__dirname, "..");
var companies = fs.readdirSync(`${BASE_FOLDER}/data/${user}/`);
And in both cases i get the following error:
UnhandledPromiseRejectionWarning: Error: ENAMETOOLONG: name too long, scandir '../data/callback => {
AND HERE MY CODE FOLLOWS
So far what i understood is that the file path string is too long ? How can we workaround an error like this if we are limited to that certain path... ?

It looks like user is a function for some reason (depends on where it comes from / where you initialize it) and what you see in the error message ../data/callback => { ... is the stringified version of that function.
I would double check that user is really just a string identifier for the user. Based on your first example, shouldn't you use user.hash?

Related

How to read the contents of a file onto the console using Nodejs

I am going back to learning js after many years off and all i want to do is read the contents of a file onto the console using Nodejs. I found the sample code. Nice and simple. I have spent over an hour trying to figure out why it will not find the file. This is sample right off the documentation and i made it exactly like the example to debug it. The absolute only difference is the name joe is replaced with my user folder.
const fs = require('fs')
fs.readFile('/Users/gendi/test.txt', 'utf8' , (err, data) => {
if (err) {
console.error(err)
return
}
console.log(data)
})
It runs fine except it will not find test.text. no matter what. I receive the following error and no matter how i format the file path. Nothing.
C:\Users\gendi>node readfile.js
[Error: ENOENT: no such file or directory, open 'C:\Users\gendi\test.txt'] {
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'C:\\Users\\gendi\\pcsSnipe\\test.txt'
}
You can also only pass in the file path as 'test.txt' and the exact same results come up. on the first part of the error msg the path looks formatted correctly but on the last line of the error msg it is not? Its been years.. so i know i am missing something really simple. I assure that file is there!! Thank you in advance and forgive my ineptness.
The fs module requires an exact path to the file you'd like to read. A simple fix to this would be to add __dirname which will return the directory path along with the path of your file.
// Define modules
const fs = require('fs');
const path = require('path');
// Read the file
fs.readFile(path.join(__dirname, '/Users/gendi/test.txt'), 'utf8' , (err, data) => {
if (err) // If FS returned an error
return console.error(err); // Log the error and return
console.log(data); // If the reading was successful, log the data
});
It works if you remove the file extention, '.txt' . Idk why it make a differnce. maybe the "." is throwing it off but it doesn't matter in this respect. Thank you

Cleanest way to fix Windows 'filename too long' CI tests failure in this code?

I'm trying to solve this Windows filename issue
Basically our CI job fails, with the 'filename too long' error for Windows.
warning: Could not stat path 'node_modules/data-validator/tests/data/data-examples/ds000247/sub-emptyroom/ses-18910512/meg/sub-emptyroom_ses-18910512_task-noise_run-01_meg.ds/sub-emptyroom_ses-18910512_task-noise_run-01_meg.acq': Filename too long
I've read the docs for Node's path module, which seems like a possible solution. I also read about a Windows prefix (\\?\) to bypass the MAX_PATH...but have no idea how to implement these in a clean way.
This part of the codebase with the tests that are failing. The hardcoded path (testDatasetPath) is likely part of the problem.
function getDirectories(srcpath) {
return fs.readdirSync(srcpath).filter(function(file) {
return (
file !== '.git' && fs.statSync(path.join(srcpath, file)).isDirectory()
)
})
}
var missing_session_files = //array of strings here
const dataDirectory = 'data-validator/tests/data/'
function createDatasetFileList(path) {
const testDatasetPath = `${dataDirectory}${path}`
if (!isNode) {
return createFileList(testDatasetPath)
} else {
return testDatasetPath
}
}
createFileList function
function createFileList(dir) {
const str = dir.substr(dir.lastIndexOf('/') + 1) + '$'
const rootpath = dir.replace(new RegExp(str), '')
const paths = getFilepaths(dir, [], rootpath)
return paths.map(path => {
return createFile(path, path.replace(rootpath, ''))
})
}
tl;dr A GitLab CI Job fails on Windows because the node module filenames become too long. How can I make this nodejs code OS agnostic?
This is a known error in the Windows Environment, however, there is a fix..
If you're using an NTFS based filesystem, you should be able to enable long paths in
Local Computer Policy > Computer Configuration > Administrative Templates > System > Filesystem > NTFS
This is also specified in the document you just linked, and theoretically, should work. However, the path shouldn't really be longer than 32 bits,
This will come with some performance hits, but should get the job done.
Also, you should preferably switch to a better package or search for alternatives. If this is your own package, maybe restructure it?
Finally, if none of these work, move your project folder directly to your data drive, (C:\) this will cut down on the other nested and parent folders.
This is inherently bad, and you may run into issues during deployment, if you choose to do it.

module.exports variable producing undefined result

So one of the features of the bot I am working on is that I can be on discord 'discreetly', meaning that I can have the idle status but if a friend knows what command to call, they can actually check if I am there. So in my index file I am using module.exports to store the variable that will contain the info that I set. In the other file, I have an array of values that depending on the value from the variable, the bot will respond with one of the phrases from the array. The problem is that when using the variable, I get an undefined response. Any ideas what I am doing wrong? Important to note
I have checked to make sure by putting an actual number and have gotten the correct response so it is an issue with the exporting. I also have the correct file path. I have also assigned variable info a number and gotten the same result. Edit: tried using the filepath as part of the variable in the array like so and got the same error
//This got me a new result so progress.
const filepath = filepath;
console.log(filepath); //This gets me {}
message.reply(activity[info]); //undefined
//new attempt that failed
//paraphrasing the filepath assignment
const filepath = filepath;
activity[filepath.info]
//first attempt
//from index
var info = message.content;
module.exports = info;
//from the other file
var activity = ["Ready to play","Chilling","Doing work","afk","can talk"];
console.log(activity[info]);
message.reply(activity[info]);
how to get a variable from a file to another file in node.js
So this is the solution to my problem
//index
var info = message.content;
module.exports.info = message.content;
//other file
const filepath = filepath;
var activity = [array of different values];
message.reply(activity[index.info]);
Thank you slothiful for you time in trying to help me. I really appreciate it

Unable to use variables in fs functions when using brfs

I use browserify in order to be able to use require. To use fs functions with browserify i need to transform it with brfs but as far as I understood this results in only being able to input static strings as parameters inside my fs function. I want to be able to use variables for this.
I want to search for xml files in a specific directory and read them. Either by searching via text field or showing all of their data at once. In order to do this I need fs and browserify in order to require it.
const FS = require('fs')
function lookForRoom() {
let files = getFileNames()
findSearchedRoom(files)
}
function getFileNames() {
return FS.readdirSync('../data/')
}
function findSearchedRoom(files) {
const SEARCH_FIELD_ID = 'room'
let searchText = document.getElementById(SEARCH_FIELD_ID).value
files.forEach((file) => {
const SEARCHTEXT_FOUND = file.includes(searchText.toLowerCase())
if (SEARCHTEXT_FOUND) loadXML(file)
})
}
function loadXML(file) {
const XML2JS = require('xml2js')
let parser = new XML2JS.Parser()
let data = FS.readFile('../data/' + file)
console.dir(data);
}
module.exports = { lookForRoom: lookForRoom }
I want to be able to read contents out of a directory containing xml files.
Current status is that I can only do so when I provide a constant string to the fs function
The brfs README contains this gotcha:
Since brfs evaluates your source code statically, you can't use dynamic expressions that need to be evaluated at run time.
So, basically, you can't use brfs in the way you were hoping.
I want to be able to read contents out of a directory containing xml files
If by "a directory" you mean "any random directory, the name of which is determined by some form input", then that's not going to work. Browsers don't have direct access to directory contents, either locally or on a server.
You're not saying where that directory exists. If it's local (on the machine the browser is running on): I don't think there are standardized API's to do that, at all.
If it's on the server, then you need to implement an HTTP server that will accept a directory-/filename from some clientside code, and retrieve the file contents that way.

Vert.x how to call a file in module

I have a module like this structure:
module_root/
mod.json
filestore/myfile
com/my/www/my.groovy(Compiled to my.class)
I want to call filestore/myfile in my.groovy. I tried many methods to access the file, but I failed.
First, I try to use relative path. Both ../../../filestore/myfile and filestore/myfile failed. No such file or directory.
new FileInputStream('../../../filestore/myfile').withStream {
// ...
}
Then I try to use absolute path, I don't know how to get the module's absolute path. I try to get the my.groovy path use this code:
scriptFile = getClass().protectionDomain.codeSource.location.path
But when I exec vertx runzip mymodule.zip, I get an exception:
java.lang.NullPointerException: Cannot get property 'location' on null object
How to do this?
for me this works..please let me know if works for you:
this is js, I don't know groovy but I suppose than must be similar
var CURRENT-DIRECTORY = new java.io.File("").getAbsolutePath()
then you can use string concatenation
CURRENT-DIRECTORY+"/filestore/myfile/mysuperFile.groovy" <--notice the /
remember than the response is a buffer, maybe you must be convert this to string
return response.toString()
good luck

Resources