How to Import Modules from All Files in a Directory, Using a Wildcard

Is it possible to import modules from all files in a directory, using a wildcard?

I don't think this is possible, but afaik the resolution of module names is up to module loaders so there might a loader implementation that does support this.

Until then, you could use an intermediate "module file" at lib/things/index.js that just contains

export * from 'ThingA';
export * from 'ThingB';
export * from 'ThingC';

and it would allow you to do

import {ThingA, ThingB, ThingC} from 'lib/things';

How do I import all files in a folder as an module and export all as an object?

const fs = require('fs');
const path = require('path');

const basename = path.basename(__filename);
const functions = {}

fs
.readdirSync("./")
.filter(file => (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js'))
.map((file) => {functions[file.slice(0, -3)] = require(path.join(__dirname, file})))

module.exports = functions;

Would something like this help?

Is it possible to import modules from all files in a directory, using a wildcard?

I don't think this is possible, but afaik the resolution of module names is up to module loaders so there might a loader implementation that does support this.

Until then, you could use an intermediate "module file" at lib/things/index.js that just contains

export * from 'ThingA';
export * from 'ThingB';
export * from 'ThingC';

and it would allow you to do

import {ThingA, ThingB, ThingC} from 'lib/things';

python wildcard import

It matters because the pages/__init__.py contains the symbols which from pages import * will import

How to import all modules i have into one ts file then export it?

Yes, You can do it. But there can be atmost one default export in a file. So, you can have it or not, its optional.

So, for example :

import Login from "./Login/login_page";
import MyAccount from "./AccountSettings/myAccount_page";
import Settings from "./AccountSettings/settings_page";
import DataProvider from "../utils/data-provider/data-provider";

export default DataProvider;

export { MyAccount,Settings, DataProvider} ;

or

import Login from "./Login/login_page";
import MyAccount from "./AccountSettings/myAccount_page";
import Settings from "./AccountSettings/settings_page";
import DataProvider from "../utils/data-provider/data-provider";


export {DataProvider, MyAccount,Settings, DataProvider} ;

How to recursively export all from folders in node js project?

You can map exports in your package.json :

{
"name": "@your-namespace/your-package",
...
"exports": {
".": "./index.js",
"./common": "./common/index.js"
}
}

Then you can refer to the exports by name :

import { YourClass } from '@your-namespace/your-package';
import { AnotherClass } from '@your-namespace/your-package/common';

Alternatively, if your submodules only need to be accessible from within your package, you could map imports instead of exports which would not need your package name prepended but must start with a # character. You don't need to explicitly specify each submodule either, you can map everything from within a folder with wildcard substitution :

{
"name": "your-package",
...
"imports": {
"#common": "./common/index.js",
"#common/services": "./common/services/index.js",
"#common/services/*": "./common/services/*.js",
"#shortcut": "./deeply/nested/path/to/module.js"
}
}

And use them :

import { YourClass } from '#common';
import * from '#common/services';
import { AnotherClass } from '#common/services/ServiceA';
import something from '#shortcut';

In the above example, #common would be a reference to ./common/index.js and #common/services/ServiceA would point to ./common/services/ServiceA.js.

Typescript wildcard import all module names into current namespace?

import * from "./MyModule";

is not part of the ES Module specification. You cannot simply inject members into the scope without naming them. Identifier conflicts could arise whenever a module changed.

As @haim770 points out in his comment, this would be very much like the much maligned (and very rightly so) with statement.

It is simply not allowed.



Related Topics



Leave a reply



Submit