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

前端 未结 17 1936
时光说笑
时光说笑 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:54

    This restriction makes sure all files or modules (exports) are inside src/ directory, the implementation is in ./node_modules/react-dev-utils/ModuleScopePlugin.js, in following lines of code.

    // Resolve the issuer from our appSrc and make sure it's one of our files
    // Maybe an indexOf === 0 would be better?
         const relative = path.relative(appSrc, request.context.issuer);
    // If it's not in src/ or a subdirectory, not our request!
         if (relative.startsWith('../') || relative.startsWith('..\\')) {
            return callback();
          }
    

    You can remove this restriction by

    1. either changing this piece of code (not recommended)
    2. or do eject then remove ModuleScopePlugin.js from the directory.
    3. or comment/remove const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin'); from ./node_modules/react-scripts/config/webpack.config.dev.js

    PS: beware of the consequences of eject.

提交回复
热议问题