NodeJS fs.open failing on existing file (not a path issue) - javascript

I've been dealing with this for a long time, so any help is much appreciated. So, I'm downloading a file and saving it using PhantomJS and CasperJS. Let me point out that they aren't the issue. The file is downloaded without a problem.
The problem is that NodeJS won't recognize or open the file after it is downloaded. I can't fs.stat, fs.open, etc.. Nothing works.
I'll share the code in a second, but here's the log:
Here: bdTcK6hSdownload.csv
[ '2puzZMeLdownload.csv',
'2s5ICbKNdownload.csv',
'bdTcK6hSdownload.csv',
'izIfagwCdownload.csv' ]
fs.js:230
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
Error: ENOENT, no such file or directory './caspertemp/bdTcK6hSdownload.csv'
at Object.openSync (fs.js:230:18)
at Object.processCSV (/Users/Home/dev/node_modules/inviter/index.js:64:29)
at /Users/Home/dev/node_modules/inviter/index.js:36:33
at ChildProcess.exithandler (child_process.js:281:7)
at ChildProcess.emit (events.js:70:17)
at maybeExit (child_process.js:361:16)
at Process.onexit (child_process.js:397:5)
As you can see, I'm printing out the created file name, then printing the contents of the directory and then trying to open the file. As you can see, bdTcK6hSdownload.csv exists in the directory but fails on open.
The simple code snippet is here:
console.log('Here: ' + filename);
filenames = fs.readdirSync('./caspertemp/');
console.log(filenames);
var fd = fs.openSync('./caspertemp/' + filename, 'r');
console.log(fd);
There's a bunch more going on before and after this but none of it matters since this basic function fails. Please help! This has been nagging for weeks.

My guess is that it's a discrepancy in current working directory. Are you starting the casperJS and node.js processes from the same directory? Do either of them change working directory at runtime? Try something like this, where node's __dirname will give you the directory path of the currently executing .js file
var path = require("path");
var filename = "bdTcK6hSdownload.csv";
var csvPath = path.resolve(path.join(__dirname, "caspertemp", filename));
console.log(csvPath);

Another possibility is timing.
I've had situations in the past where a file entry appears in the folder straight away but the file is unusable as the thing writing it has either not finished or not yet released it's handle. Under those conditions it isn't unusual to need to either defer processing or to do some other checks to see if things are ready.
On those occasions it may be that a restart 'fixes' things but only because as a side effect it either releases the handle or delays processing, so when your code trys again it's all fine.

This occurs only when you are giving wrong path. I have come across the same problem.
Initially I was running the file directly node xyz.js and required static files are in same repository. But When I do npm start, code was throwing above error :
fs.js 495
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
**Error: ENOENT, no such file or directory**
Then I moved static resources, renamed path and It worked. Hope this may help you.

File extension could be entered twice hence not found. Navigate to the file directory with your command prompt and get the list of files in that directory
command:
dir
or
ls
Get the actual file name there and use it. In my case I had saved a text document as "readMe.txt" and on command prompt it appeared as "readMe.txt.txt". I did not have to append the file extension when saving and this created the problem.

Related

Can't download a pdf file in npx cypress run --headed mode?

Hope to get some help with what is my first question to the community...
I'm using the Tricentis Sample App to practice some testautomation with Cypress (javascript) and I'm having issues with the final step. (Downloading the mock invoice and checking the presence of it in the downloads folder)
//This function displays the "download pdf" button
function selectPriceOption(cy){
cy.get('#selectplatinum').check({force:true})
}
//this function is supposed to download the pdf
function downloadQuote(cy){
cy.wait(2000)
cy.get('#downloadquote').click()
cy.wait(20000)
}
//This function checks if file exists in the download folder
function checkFileExists(cy){
cy.readFile('C:/xxx/Documents/cypress/downloads/Tricentis_Insurance_Quote.pdf')
}
module.exports = {
enterAutoPage,
enterVehicleData,
enterInsuranceData,
enterProductData,
selectPriceOption,
checkFileExists,
downloadQuote
//sendQuote
}
When I'm running the script on npx cypress open mode, it downloads the pdf generated without a hitch. But as soon as I use npx cypress run --headed it doesn't manage to download the pdf file and as a consequence the test case throws a fail... Btw, I know the cy.wait command is not needed, it was just my last attempt to see if it needed additional waiting time. Still didn't work!
Now, here's what bugs me... If I run the script in open mode first, the download is successful and if I then go for the "run --headed" mode afterwards, it finds the file! I'm running trashAssetsBeforeRuns: false in the config file. So the check file function is working as intended. But when the folder is empty and I only use --headed mode then the download is NOT successful and the test case fails due the file is missing when the script reaches the last test step. I hope this explanation makes sense... #newbietechnicaltester
PS: Leaving the possibility open to something funky going on with the Tricentis site that is causing the code to not work as intended

fs.writeFileSync Can't set DOCTYPE here

This Meteor code uses fs.writeFileSync to save a file to disc with the html extension, it saves the file OK but I get the following server error
While processing files with templating-compiler (for target web.browser):
screen_shots/page.html:1: Can't set DOCTYPE here. (Meteor sets for
you)
And the following browser console message
Uncaught SyntaxError: Unexpected token < in JSON at position 1
Any idea how to fix this error? Thanks
import { Meteor } from 'meteor/meteor';
const puppeteer = require('puppeteer'); //maybe change const to let
const fs = require('fs')
//.... SOME CODE HERE...
let searchLink = await page.$('input[alt="Search"]')
await searchLink.click()
await page.waitForNavigation()
const html = await page.content()
fs.writeFileSync("/screen_shots/page.html", html, 'utf8') //removing utf8 did not eleminate the error
The location of /screen_shots/page.html is in your source tree, which causes Meteor to start rebuilding, and abort what it's doing. You definitely don't want this. Meteor watches all source files that are explicitly imported, but also anything in client and server folders.
When running meteor or meteor run in this mode, all files excepted of those in the folder /imports are loaded automatically. There are also a few other rules to it, all can be found on this page: https://guide.meteor.com/structure.html#load-order
There is more information here: https://stackoverflow.com/a/64116026/517914
The .png files you also wrote in the same folder will also be loaded automatically, but won't cause errors.
You probably don't write to the file system
Now that you understand what's going on, you may want to avoid writing to the file system. There are a few reasons for this
When using docker, the local file system is read only by default
You can attach a volume, but you need to attach it to each server in your scaling group
Volumes get full
S3 (or similar) is more convenient, and you can apply policies and permissions to the files

Problem running same script in different platforms(Termux and Windows)

I'm trying to run my server script on nodejs through Termux on my phone and normally on windows. On windows it all runs perfectly without erros, but on termux there's an error "Cannot find module gameserver.js", even though that is the main file(I'm running "sudo node gameserver.js" inside the folder its locatedd), and no other file tries to do a require on it.
The error points to js files from node itself(loader.js, run_main.js and run_main_module.js). I've given termux root access, and I run node.js using sudo, so I've no clue what could be happening. I've no .json file since I'm just trying to run a js file through node on my phone. Both windows and my phone are using the same node.js version
Did you try to give an absolute path to your file as a parameter? It could happen if working directory has changed (because of sudo?).

Run Meteor from within a folder

I've run meteor build to create my bundle, uploaded to the server, it runs fine, however the .css and .js paths are wrong, as it's using the root url. I need to run this from within a /project folder. Again it's running, but 404 on the files as they're not prefixed with /project.
eg. http://domain.com/65d054cb90ff094804072528d222178ddbf625e22.js?meteor_js_resource=true 404 (Not Found)
needs to be http://domain.com/project/65d054cb90ff094804072528d222178ddbf625e22.js?meteor_js_resource=true
I've tried using ROOT_URL=http://domain.com/project node main.js, that gives an unknown path error, i've also tried using Meteor.absoluteUrl('project', {}); in conjuntion with rooturl but again, no avail.
Any of you fine people have any ideas? :) Thanks!
PS. It's running on an apache server with ProxyPass, if that's of relevance.
You could instruct your apache to redirect those calls with a ProxyPassMatch, such as:
<LocationMatch ^/(.*)meteor_js_resource=true$>
ProxyPassMatch http://localhost/project/$1meteor_js_resource=true
</LocationMatch>

Heroku(Cedar) + Node + Express + Jade Client-side javascript files in subdirectory work locally with foreman+curl but not when pushed to Heroku

I am very new to node and heroku and I suspect this is some kind of simple permission issue etc, but I can't seem to track it down.
I have several pure javascript files in a sub-directory one level beneath my root directory where my web.js file is sitting. I have a line in my web.js file to specify the directory
app.use('/heatcanvas',express.static(__dirname+'/heatcanvas'));
If I run my app locally with Heroku Foreman I get the expected js response when I run the following curl command
curl localhost:5000/heatcanvas/heatcanvas.js
However when I push to Heroku and hit the corresponsing live url in the browser
www.example.com/heatcanvas/heatcanvas.js
I receive the following:
Cannot GET /heatcanvas/heatcanvas.js
If I check Firebug and/or the Heroku logs I see I am actually getting 404 errors for those files even though the pathing should match what is being done locally. It is also worth mentioning that third party javascript is coming over just fine, it is only when the src attribute of the script tag points to my site that there is an issue. What do I need to do to get my scripts to be available?
I recommend you to use process.cwd() value to get specific directory
process.env.PWD = process.cwd()
at the very beginning of your web.js
let you access files easily.
You can do
app.use('/heatcanvas',express.static(process.env.PWD+'/heatcanvas'));
instead of using
__dirname
Warning: Make sure to execute web.js at the root directory of web.js (Heroku web.js are executed that way)

Categories