1

I have a TypeScript web application with the following folder structure.

- assets
 |- a.png
 |- b.png
 |- c.png
 |- d.png
 |- ...
- app.ts

In app.ts, how do I programatically list all the files in assets folder?

I tried the below but it didn't work. And I also thought I may be going down the wrong path because fs is used to access the user's file system, which is not my intent.

const fs = require('fs');
const assets_folder = './assets/';
fs.readdirSync(assets_folder).forEach((file) => {
  console.log(file);
});
0

1 Answer 1

1

The issue is related to file location because when you use the typescript after compiling., it'll move to dist/build and it'll look in that directory.

Solution: Copy files/directory from src to dist/build programatically, it'll solve your issue.

Project src

src
  assets
    images
  app.ts

After complie

dist
  //<Missing assets>
  app.js
Sign up to request clarification or add additional context in comments.

3 Comments

wow thanks. so the same code that i provided above using fs will work once i move the files?
@chongzixin it should work.
having a separate issue Uncaught ReferenceError: require is not defined now. Will resolve that then validate this. thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.