Nick Manning
Nick Manning

Reputation: 2989

What is the es6 equivalent of node_modules?

I'm used to require but I'm playing with es6 modules. I decided to change my directory structure, which meant that I had to go and change all of the import statements (import thing from "../thing" became import thing from "../../thing" etc).

I had to go through and change a lot of this. With node_modules, this is never an issue.

Do you always have to specify a path in es6 modules or is there some sort of system/order-of-operation for finding modules?

Upvotes: 1

Views: 94

Answers (1)

isysd
isysd

Reputation: 401

There is no direct equivalent to node_modules in ES6, meaning no modules can be imported by name, from a pre-defined directory. No such directory exists, and so modules must be imported by path.

The practice for ES6 modules is to put them in a flat(ish) directory structure, like the one you created with all of your ../thing changes.

BTW, bundlers virtualize this process, which is why you may not be used to seeing import '../thing'

If you would like to support both ES6 and Node.js style modules, as well as flat and node_modules directory structure, check out this example and it's introductory article.

Upvotes: 1

Related Questions