Node.js, can't open files. Error: ENOENT, stat './path/to/file'

后端 未结 2 670
花落未央
花落未央 2020-11-28 20:21

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 Stud

相关标签:
2条回答
  • 2020-11-28 21:06

    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')
    
    0 讨论(0)
  • 2020-11-28 21:08

    Here the code to use your app.js

    input specifies file name

    res.download(__dirname+'/'+input);
    
    0 讨论(0)
提交回复
热议问题