126
votes

I have developed a node.js program using the express framework on my computer, where it runs fine with no complaints.

However, when I run the program on my SUSE Studio appliance, where it is intended to live, I receive an error at any file interaction.

Error: ENOENT, stat './path/to/file'

I have checked that the file permissions are correct, which they are. My computer and my appliance are running different versions of node, if this matters.

Any thoughts?

2
Since the path is relative, perhaps you are running it from a different directory? How are you generating the path and what is the directory structure? And how are you running node?loganfsmyth
Thanks, yes I was running the script from a different directory and foolishly assumed that the file path would be relative of the script itself. I knew it was something simple like this :)user712410
It should be fixable. Can you add some code showing how you are making the path and what you expect it to resolve to? If you want it to be relative to a particular file, normally you would do __dirname + 'path/to/file';loganfsmyth
Yeah, that's what I've done now. I was running the script by executing "node ~/path/to/script.js" and expecting relative references to files in my script to resolve to "~/path/to" Thanks!user712410

2 Answers

188
votes

Paths specified with a . are relative to the current working directory, not relative to the script file. So the file might be found if you run node app.js but not if you run node folder/app.js. The only exception to this is require('./file') and that is only possible because require exists per-module and thus knows what module it is being called from.

To make a path relative to the script, you must use the __dirname variable.

var path = require('path');

path.join(__dirname, 'path/to/file')

or potentially

path.join(__dirname, 'path', 'to', 'file')
5
votes

Here the code to use your app.js

input specifies file name

res.download(__dirname+'/'+input);