question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add ability to import modules using absolute paths

See original GitHub issue

Since working with relative paths (../../../) can be such a pain, we should investigate adding absolute import paths to the pipeline.

This Gist outlines four possibilities:

  1. The Symlink
  2. The Global
  3. The Module
  4. The Environment

In my experience I’ve only used the NODE_PATH= environment variable (which worked as expected), but in a complex environment like Force (and possibly Heroku, though this article seems to suggest it’s fine) there could be problems to investigate.

The Browserify Handbook also outlines some options.

Additionally, since we’re using Babel, it would be worth investigating babel-plugin-module-resolver which compiles down paths into a safe format via a config step. We’re currently using this in in Prediction.

cc @craigspaeth

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
damassicommented, Mar 1, 2017

Another thing I’d like to add about relative path difficulties is project exploration and especially refactoring – it’s difficult to reason about what is what and whats been used where when each path can potentially be different:

# a.coffee
filter = require('../index.coffee')

# b.coffee
filter = require('../../../index.coffee')

# c.coffee
filter = require('./index.coffee')

Are they all the same, or different? In terms of refactoring, one of the biggest points of stress is being able to ask yourself if a change you make to a file in one place will have cascading effects elsewhere. When pathnames are absolute, you can do a global search to see exactly where the component or whatever has been used, and safely make changes. Whole layers of complexity get removed when pathnames are locked down.

1reaction
alloycommented, Mar 20, 2017

Spoke to an MS employee, they’re using the following combination:

  • TS/Intellisense works by specifying the baseUrl option in the TS config.
  • babel-plugin-module-resolver to actually rewrite the paths in the resulting JS.

So the babel plugin would work after all.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I import a module dynamically given the full path?
The advantage of adding a path to sys.path (over using imp) is that it simplifies things when importing more than one module from...
Read more >
Use Absolute Paths with React - Better Programming
By default, relative paths are the supported way of importing modules to React and other frameworks. You will usually see or write something ......
Read more >
Absolute vs Relative Imports in Python
An absolute import specifies the resource to be imported using its full path from the project's root folder. Syntax and Practical Examples. Let's...
Read more >
Importing with Absolute Paths in JavaScript/TypeScript using ...
How to use absolute paths in JavaScript and TypeScript to improve your import statements, using the right webpack configuration.
Read more >
Using absolute (alias) imports in Javascript and VSCode
Learn how to import javascript or typescript modules with absolute paths in webpack & VSCode. Developers love productivity hacks.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found