user16953756
user16953756

Reputation:

NodeJS - no such file or directory, scandir '/static/reports/'

I tried following this stackoverflow post to try and see my folder on my static webpage but no luck:https://stackoverflow.com/a/31274417

Here is my code:

const express = require('express');
const app = express();
const db = require('./persistence');
var fs = require('fs');
var files = fs.readdirSync('./static/reports');


app.use(require('body-parser').json());
app.use(express.static(__dirname + '/static'));



db.init().then(() => {
    app.listen(3000, () => console.log('Listening on port 3000'));
}).catch((err) => {
    console.error(err);
    process.exit(1);
});

const gracefulShutdown = () => {
    db.teardown()
        .catch(() => {})
        .then(() => process.exit());
};

process.on('SIGINT', gracefulShutdown);
process.on('SIGTERM', gracefulShutdown);
process.on('SIGUSR2', gracefulShutdown); // Sent by nodemon

Here is a picture of what it looks like from vscode: enter image description here

Here is a picture of the error from docker: enter image description here

Here is the error from text:

internal/fs/utils.js:269

    throw err;

    ^


Error: ENOENT: no such file or directory, scandir './static/reports'

    at Object.readdirSync (fs.js:955:3)

    at Object.<anonymous> (/app/src/index.js:5:16)

    at Module._compile (internal/modules/cjs/loader.js:999:30)

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)

    at Module.load (internal/modules/cjs/loader.js:863:32)

    at Function.Module._load (internal/modules/cjs/loader.js:708:14)

    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)

    at internal/main/run_main_module.js:17:47 {

  errno: -2,

  syscall: 'scandir',

  code: 'ENOENT',

  path: './static/reports'

}

Upvotes: 0

Views: 1870

Answers (1)

shreyasm-dev
shreyasm-dev

Reputation: 2834

Starting a file path with a / denotes that it's at the root of the filesystem. Don't use a /; instead, just use static/reports. You can also use ./static/reports if the code will be run from the src/ directory every time.

var fs = require('fs');
var files = fs.readdirSync('static/reports/');

Upvotes: 1

Related Questions