The create-react-app imports restriction outside of src directory

前端 未结 17 1781
时光说笑
时光说笑 2020-11-22 13:20

I am using create-react-app. I am trying to call an image from my public folder from a file inside my src/components. I am receiving this error message.

17条回答
  •  伪装坚强ぢ
    2020-11-22 13:45

    There are a few answers that provide solutions with react-app-rewired, but customize-cra exposes a special removeModuleScopePlugin() API which is a bit more elegant. (It's the same solution, but abstracted away by the customize-cra package.)

    npm i --save-dev react-app-rewired customize-cra

    package.json

    "scripts": {
        - "start": "react-scripts start"
        + "start": "react-app-rewired start",
        ...
    },
    

    config-overrides.js

    const { removeModuleScopePlugin } = require('customize-cra')
    
    module.exports = removeModuleScopePlugin()
    

提交回复
热议问题