How to avoid using relative path imports (/../../../redux/action/action1) in create-react-app

前端 未结 7 600
太阳男子
太阳男子 2020-12-07 18:21

I\'ve been using create-react-app package for creating a react website. I was using relative paths throughout my app for importing components, resources, redux etc. eg,

7条回答
  •  伪装坚强ぢ
    2020-12-07 19:18

    Create a file called .env in the project root and write there:

    NODE_PATH=src
    

    Then restart the development server. You should be able to import anything inside src without relative paths.

    Note I would not recommend calling your folder src/redux because now it is confusing whether redux import refers to your app or the library. Instead you can call your folder src/app and import things from app/....

    We intentionally don't support custom syntax like @redux because it's not compatible with Node resolution algorithm.

提交回复
热议问题