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.

Document src/node_modules as official solution for absolute imports

See original GitHub issue

After a long discussion in #741 it seems to me that src/node_modules seems like the best solution for absolute imports.

It has the following benefits:

  • Doesn’t rely on symlinks (they don’t work well on Windows).
  • Works with IDEs and any existing tooling because it relies on Node resolution mechanism.
  • If you really want to, you can run ln -s src/node_modules src/packages and use the “nicer” packages (or any other) folder.

This issue is a call to help to make this approach the official one. We need to:

  • Document it
  • Make Jest preprocess this subfolder #607
  • Make Jest run tests in it #1042
  • Fix any other integration issues we find

If you want to work on either please leave a comment and then submit a PR! Cheers.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:45
  • Comments:30 (12 by maintainers)

github_iconTop GitHub Comments

22reactions
alex35milcommented, Apr 8, 2017

In my mental model node_modules is something I can easily rm -rf and regenerate w/o worries to lose anything. I never expect any app code in there. A lot of various configs ignore it for the same reason: in most of the cases, it’s something not related to user’s code. Also, usually you have to move a whole app in this directory.

13reactions
icoppcommented, Sep 22, 2017

For absolute imports, it seems like it’d be a lot better to have some kind of special prefix and set up webpack’s alias functionality for it. For example, my personal projects use:

{
  resolve: {
    alias: {
      "@": path.resolve(__dirname, 'src')
    }
  }
}

…after which you can reference files via…

import App from '@/App' // the file is at `src/App/index.jsx`

You can also inject the same schema into Jest:

"moduleNameMapper": {
  "^@/(.*)$": "<rootDir>/src/$1"
}

It keeps a clear distinction from npm-tracked node_modules, avoiding the “I can’t get it to work and I don’t understand why” potential of having overlapping names or having imports start with just ./.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Absolute vs Relative Imports in Python
In this tutorial, you'll not only cover the pros and cons of absolute and relative ... You can find out more about imports...
Read more >
Why and How to Use Absolute Imports in React
Easily copy-paste the code with imports into another file in the project and not have to tinker with import paths. It is short...
Read more >
Understand and Configure Absolute Import Paths in JavaScript
Variables, functions, and classes that are declared in a module need to be explicitly exported. All modules intended to be used in other...
Read more >
Making Life Easier with Absolute Imports - React in Javascript ...
According to create-react-app Docs, We can use absolute imports in our react project by configuring a jsconfig. json / tsconfig. json (for ...
Read more >
Absolute and Relative Imports in Python - GeeksforGeeks
Python modules can get access to code from another module by importing the file/function using import. The import statement is the most ...
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