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.

react-scripts 4.0.1 absolute components import doesn't work

See original GitHub issue

Describe the bug

Absolute components import doesn’t work in 4.0.1 CRA version.

For example:

import React, { useState } from 'react';
import Todo from 'components/Todo';

I get this error `Module not found: Can’t resolve ‘components/Todo’ in ‘…/cra-project/src’

Folder components is nested to src folder.

The file .env was added in the project root. .env file content

NODE_PATH=src/

When I change react-scripts version from 4.0.1 to 3.4.4 absolute imports work fine.

Did you try recovering your dependencies?

Yes.

Environment

Environment Info:

current version of create-react-app: 4.0.1 running from /home/sergey-sedykh/.npm/_npx/12865/lib/node_modules/create-react-app

System: OS: Linux 5.4 Ubuntu 18.04.3 LTS (Bionic Beaver) CPU: (4) x64 Intel® Core™ i5-3330 CPU @ 3.00GHz Binaries: Node: 14.6.0 - /usr/local/bin/node Yarn: 1.22.4 - /usr/bin/yarn npm: 6.14.6 - /usr/local/bin/npm Browsers: Chrome: 85.0.4183.121 Firefox: 83.0 npmPackages: react: ^17.0.1 => 17.0.1 react-dom: ^17.0.1 => 17.0.1 react-scripts: 4.0.1 => 4.0.1 npmGlobalPackages: create-react-app: Not Found

Steps to reproduce

  1. Install project with CRA
  2. Create the file .env in the project root.
  3. Add to the .env file next content NODE_PATH=src/
  4. Use absolute components import

Expected behavior

Absolute module imports should work.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:8

github_iconTop GitHub Comments

4reactions
Toubcommented, Dec 11, 2020

Replace NODE_PATH=./ in your .env file by the following in tsconfig.json

  "compilerOptions": {
    "baseUrl": "./"
  }

This could be more explicit in the changelog.

3reactions
Nogias9xcommented, Oct 5, 2021

None of those work for me in 2021 with create-react-app@4.0.3

Read more comments on GitHub >

github_iconTop Results From Across the Web

Absolute path not working in create-react-app - Stack Overflow
json file in my root directory in order to be able to import components using absolute paths in my React Application (which was...
Read more >
Clean Up Your Imports using Absolute Imports & Alias on ...
For Create React App without using Craco in latest version (v5.0.1) , absolute import doesn't work. it's a bug. please revise the post....
Read more >
Migrating a Create React App project to Vite - Darek Kay
Absolute imports. Your project might depend on absolute imports: import Button from "common/button"; ...
Read more >
jsconfig.json not working react - You.com | The AI Search ...
According to Create React App documentation about absolute imports you should ... it doesn't work at all, when I delete include: ["src"], some...
Read more >
Absolute imports with Create React App | by David Gilbertson
Unfortunately it doesn't sort them the way I want to (npm packages first, relative imports last). Maybe this is possible and I just...
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