Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I require a directory in ES6?

Tags:

ecmascript-6

I know I can require a file in ES6 like this:

require('./config/auth');

When I try to do this

require('./config/');

I get: Module not found: Error: Cannot resolve directory './config'. Why does this happen? How can I require a directory?

like image 566
Jason Swett Avatar asked May 02 '15 11:05

Jason Swett


People also ask

What is require context?

require. context is a special feature supported by webpack's compiler that allows you to get all matching modules starting from some base directory.


1 Answers

First of all, your requires are in NodeJS/io.js syntax, module in ES6 syntax looks like this:

import "./config/auth";

Or if you want to load something from it:

import authenticate from "./config/auth";

You can't load whole directories at once, but in Node/io.js you can create a module and then load that.

Note that as a workaround you can load a single file that in turn loads multiple files and returns their results. There is also work in progress on an asynchronous loader but that changes so often it's hard to keep track so I wouldn't rely on it just yet.

like image 163
Benjamin Gruenbaum Avatar answered Sep 26 '22 18:09

Benjamin Gruenbaum