4 ms·
The problem I find with the current ES modules is a lot of statements like this import {thing} from '../../../thing/in/dir.js' and then if I move a file I hav
by plopz 9y ago
The problem I find with the current ES modules is a lot of statements like this
import {thing} from '../../../thing/in/dir.js'
and then if I move a file I have to change all of its imports to a different number of parent traversals. It would be really nice to be able to define a path that all imports would be relative from.
I think babel has something like this using @, but I don't use babel right now.
- WorldMaker 9y agoThere are proposals being debated for this. The article mentions specifically this one: https://github.com/domenic/package-name-maps https://github.com/domenic/package-name-maps
- Touche 9y agoMoving files around is not something you do all that often. Doing so will present a problem in most any programming language.
- Sophistifunk 9y agoThis is really not true. Moving files around is trivial in anything with a decent type system and IDE.
- mnutt 9y agoEmber has a convention to resolve files in its 'app' directory from your project name, and it makes renaming files a lot easier: `import { thing } from 'my-project/thing/in/dir.js'`
- danlugo92 9y agoRequireJS has this too.
- Can_Not 9y agoYou can set a custom alias, for example nuxt uses ~ for project root. I've seen projects use @ also.
- panopticon 9y agoThat's not part of the ES module spec. That's something transpilers (like Babel + a plugin) support.
- underwater 9y agoCan you also teach systems with their own resolution about the aliases (Jest, Flow, etc.)
- iamnotlarry 9y agoI wrote a very small resolve hook for node that does this. It makes the directory of the initial module / for all imports. Node has made resolver hooks like that dead simple.
- reaktivo 9y agoYou an usually use `NODE_PATH=src` which will make requiring anything inside `src` easy as `import something from 'somewhere';`