facebook / create-react-app

Set up a modern web app by running one command.
https://create-react-app.dev
MIT License
102.73k stars 26.85k forks source link

Import error with Environment variable and absolute import #5770

Closed mlnor27 closed 5 years ago

mlnor27 commented 5 years ago

Hi everyone

I would like to report a bug i came across. Sorry in advance if it's duplicated. I didn't find any related discussions elsewhere.

Which terms did you search for in User Guide?

"Attempted import error alias" "alias import error"

Environment

System: OS: macOS 10.14 CPU: x64 Intel(R) Core(TM) i5-7267U CPU @ 3.10GHz Binaries: Node: 9.10.0 - /usr/local/bin/node Yarn: 1.10.1 - /usr/local/bin/yarn npm: 6.4.1 - /usr/local/bin/npm Browsers: Chrome: 70.0.3538.77 Firefox: 62.0.3 Safari: 12.0 npmPackages: react: ^16.7.0-alpha.0 => 16.7.0-alpha.0 react-dom: ^16.7.0-alpha.0 => 16.7.0-alpha.0 react-scripts: 2.1.1 => 2.1.1 npmGlobalPackages: create-react-app: 2.1.1

Steps to Reproduce

  1. create an .env file in the root directory. mine looks like this -->
    NODE_PATH=./src
    REACT_APP_QWANT_API_URL=http://localhost:8000
  2. in /src/constants/index.js, export the environment variable like such -->
    
    const API_URL = process.env.REACT_APP_QWANT_API_URL

export { API_URL }

3. Use it anywhere like such -->

import { API_URL } from 'constants'

console.log(API_URL)



### Expected Behavior

Import the constant API_URL holding the value specified in the .env file (http://localhost:8000) without problem

### Actual Behavior

<img width="708" alt="capture d ecran 2018-11-11 a 04 17 31" src="https://user-images.githubusercontent.com/7221148/48308580-c50b1f00-e568-11e8-9e4d-6b6232fac1f4.png">

This is pretty strange and only happens when i try importing and reading something from process.env. Otherwise everything works great, NODE_PATH is really useful to me as i used to eject the config to add the aliases by myself in the past.

Thank you for your attention
Timer commented 5 years ago

You can't use the constants name -- that conflicts with a Node built-in, try a file name like appconfig!