2

Like it works in C# / Java e.t.c, where you don't have to worry about the location of the file, the compiler will be responsible for correctly resolving the dependencies...

At the moment relative paths are used for JavaScript modules import something from '../../../../Something' and that long path can be resolved using configurations in webpack and/or gulp e.t.c, but it is still vulnerable to file location change. So if there is a module being used in many other files moving it is not an option, better to duplicate and use new path for other files and slowly refactor the old ones.

Is there any solution to this? Am i missing something or thinking about in the wrong way? (I tried TypeScript namespaces but they don't act like this, weren't what i expected, and i'm not that into using languages that compile to JS)

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 1
    I just got what you are asking. Node has `var name = require("moduleName");`. If you want to use a method from `moduleName`, you just have to do `name.method();`. – ahitt6345 Nov 26 '15 at 05:18
  • It looks like the OP wants to write `import module`, so that JS knows where this `module` is located without necessity in defining a path. – Yeldar Kurmangaliyev Nov 26 '15 at 05:21
  • yes, what @YeldarKurmangaliyev said. Like it works in compiled languages. I know JS isn't a compiled language, but there might be a tool i don't know about. –  Nov 26 '15 at 06:03
  • This doesn't have anything to do with the JavaScript language, but with your module loader/resolver. As @ahitt6345 said, just name your modules instead of using paths. – Bergi Nov 26 '15 at 06:29
  • 1
    Possible duplicate of [How do I declare a namespace in JavaScript?](http://stackoverflow.com/questions/881515/how-do-i-declare-a-namespace-in-javascript) – Alexander Elgin Nov 26 '15 at 06:43
  • 1
    in _C# / Java e.t.c_ it works because path settings inside project file – Grundy Nov 26 '15 at 06:43

0 Answers0