2

Is there any function to include all JavaScript files from a folder using JavaScript. Since I'm have plenty if .js file to include.

2

2 Answers 2

0

This is a really brief way to require all files (node.js-style):

var fs = require('fs');

// assuming all things are files
fs.readdirSync('path/to/dir').forEach(function(elementName) {
    require('path/to/dir' + elementName);
});

Generally though people will make an index file in the root of the folder that individually requires each file and exports the appropriate fields.

Sign up to request clarification or add additional context in comments.

2 Comments

he can do it with any lang. that have access to the file-system, that wasn't the question though... by the question and tags seems like he is looking for a way to access it from the client
I wasn't totally sure, even though he mentioned angular. Thought I'd throw this out there in case it was helpful.
-1

There isn't, as JS cannot read the file system. You could:

  • Create an array of filenames
  • Create a php (or other server-side) script that returns the file list and fetch the list with ajax

Once you have the list of files, you can load them with JS:

var js=document.createElement("script");
js.src=url;
document.body.appendChild(js);

1 Comment

It's an angularJs question, your answer is irrelevant

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.