7

Is there any good way to load a module elegantly (where the IDE can suggest or go into the file) using dynamic path or start from the root directory to import a module?

import * as Q from 'q'; import * as loopback from 'loopback'; import datasources from '../../../datasources.json'; import app from '../../../server'; import {ApiError, ValidationError, DatabaseError} from'../../../utils/error-handlers';

11
  • Hack the import loader? Put a symbolic link in node_modules to your preferred default load location? Use a flatter directory structure? Commented Sep 10, 2015 at 12:37
  • So what does not work in the example code you posted? Or what don't you like about the code? Commented Sep 10, 2015 at 12:37
  • @Bergi Code is working fine but I do not like the ../../ go to the relative directory rather I would like to go from the base directory. Suppose like have directories a->b->c from c if I want to go to b in current structure it will be '../b' rather I would like 'a/b'. Commented Sep 10, 2015 at 13:21
  • So you mean like import app from '/server'? Your module loader is responsible for resolving those strings to modules. Check its docs about support of paths, and maybe file a feature request. Commented Sep 10, 2015 at 13:23
  • 1
    This has nothing to do with ES6. Commented Sep 10, 2015 at 15:53

1 Answer 1

0

The module identifier is not necessarily even a path at all. For example, it often searches the node_modules folder. The quick answer is the syntax depends on the module loader.

But the pragmatic answer, if you are using Babel (as you might be since you specified this was an ES6 import), is that you can specify a location relative to the project root with an '@' at the beginning. For example, in my Vue.js project, I have:

import PageFooter from '@/components/PageFooter' 

In my case, this can avoid issues of having a complex tree of nested components, at various depths.

In your case, you could have:

import app from '@/server' 

assuming server was in the project root (e.g. as server.js and not a folder itself).

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.