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 `resolve` configuration in Webpack to avoid using relative paths in `import`

See original GitHub issue

Hi guys. I was wondering why don’t you added some config like this in Webpack, to start doing import foo from 'components/foo'; instead of import foo from '../../components/foo';

Is there any issue about it? Thank for your patience.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

10reactions
gaearoncommented, May 20, 2017

It’s been suggested quite a few times before (please search for “absolute imports” in issues). We won’t be supporting it on Webpack level because it is incompatible with Node resolution mechanism.

But if you’d like, you can work around it by adding

NODE_PATH=src

to your .env file in project root.

Then this will work. Alternatively you can symlink node_modules/components to src/components and it will work too.

Hope this helps!

2reactions
codeaidcommented, May 28, 2017

Adding the path to .env file doesn’t seem to work.

I generated a new project using create-react-app test, edited index.js to import App from 'App'; instead of import App from './App'; and all I’m getting is

Failed to compile.

./src/index.js
Module not found: Can't resolve 'App' in '/var/www/test/src'

The only way I could get it working was by changing the start script to be "start": "NODE_PATH=src react-scripts start".

Is there a reason why my .env file is not being picked up?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting rid of relative paths in imports using webpack alias
Getting rid of relative paths in imports using webpack alias ... You can add an alias under resolve option provided by web pack...
Read more >
Module Resolution - webpack
A resolver is a library which helps in locating a module by its absolute path. A module can be required as a dependency...
Read more >
Avoid relative import paths in React.js using Webpack aliases
By setting resolve.aliases in your Webpack config ( webpack.config.js ), you can make the process of importing modules pretty easy. So, ...
Read more >
Webpack can't resolve relative path import express static
Since you're setting the extensions: [".ts", ". · You specify .ts and . · If you are using Typescript, you will need to...
Read more >
ES6 Import Statement Without Relative Paths Using Webpack
Use this in your Webpack v1.x configuration. You might already have a 'resolve' setting in there. Keep everything there, but add a root...
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