Absolute Path Imports
See original GitHub issueAccording to the links below I understand .env with NODE_PATH=src
in it, allows the react app to take advantage of Absolute Path Imports without ejecting.
so far I’ve managed to load REACT_APP_TEST_VAR in my process.env which indicates the file is being loaded ! but i cant import anything from src or src/components folder
So far I’ve tried moving the NODE_PATH into .env && .env.development restarting node and webpack, rebooting the system
- https://medium.com/@ktruong008/absolute-imports-with-create-react-app-4338fbca7e3d
- https://medium.com/@tacomanator/environments-with-create-react-app-7b645312c09d
- https://fdp.io/blog/2018/01/18/setting-up-absolute-paths-in-create-react-app/
- https://github.com/facebook/create-react-app/issues/253
- https://github.com/facebook/create-react-app/issues/2230
- https://github.com/facebook/create-react-app/blob/25184c4e91ebabd16fe1cde3d8630830e4a36a01/packages/react-scripts/config/env.js#L61
Environment
Environment: OS: Linux 4.15 Node: 9.10.0 Yarn: 1.7.0 npm: 6.4.0 Watchman: Not Found Xcode: N/A Android Studio: Not Found
Packages: (wanted => installed) react: ^16.4.2 => 16.4.2 react-dom: ^16.4.2 => 16.4.2 react-scripts: ^1.1.5 => 1.1.5
Steps to Reproduce
- Add .env with NODE_PATH
- in any component expect to be able to import from NODE_PATH specified path
- react complain “Module not found: Can’t resolve” in child component
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:13 (2 by maintainers)
Top Results From Across the Web
Advanced Features: Absolute Imports and Module Path Aliases
Configure module path aliases that allow you to remap certain import paths. ... a common pattern is aliasing certain directories to use absolute...
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 >Import via Absolute Paths in Angular - Jacob Neterer - Medium
Are you tired of long and unreadable relative imports in your Angular apps? ... Importing via absolute paths makes your imports clean and...
Read more >Importing with Absolute Paths in JavaScript/TypeScript using ...
The directory (absolute path) that contains your modules. May also be an array of directories. This setting should be used to add individual ......
Read more >How to set an absolute path in TypeScript | Towards the Cloud
Absolute file paths can help organize the imports in your TypeScript projects by improving the way import locations are fetched by the ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi everyone!
If placing
NODE_PATH=src
does not work on your system that means the variable is already set on your system. This is generally considered harmful and should be unset. If it’s set for a reason, you must adjust yourpackage.json
’sbuild
,test
, andstart
scripts to look like this:cross-env NODE_PATH=src react-scripts <...>
.Can confirm this behavior, I created a project with create-react-app about 15 minutes ago and reproduced it.
Node v8.11.4 OSX 10.12.2
Here’s my package.json:
Steps to repro:
npx create-react-app my-app
index.js
insrc/components/Button/index.js
import Button from 'components/Button'
.env
file in project root (sibling of package.json) withNODE_PATH=src/
npm start
Failed to compile.
./src/App.js Module not found: Can’t resolve ‘components/Button’ in ‘/Users/kylebebak/Code/Work/Beam/my-app/src’
Just for more context, here’s src/components/Button/index.js:
It’s worth mentioning that if I change the “start” script in package.json to
"start": "NODE_PATH=src/ react-scripts start",
, then webpack or whatever CRA uses under the hood has no problem finding the Button module.In other words, passing the env var through the package.json script works, while putting it into the .env file doesn’t. This runs counter to the docs, or at least what is mentioned in other issues, so it seems like a bug.