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

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

    The package react-app-rewired can be used to remove the plugin. This way you do not have to eject.

    Follow the steps on the npm package page (install the package and flip the calls in the package.json file) and use a config-overrides.js file similar to this one:

    const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
    
    module.exports = function override(config, env) {
        config.resolve.plugins = config.resolve.plugins.filter(plugin => !(plugin instanceof ModuleScopePlugin));
    
        return config;
    };
    

    This will remove the ModuleScopePlugin from the used WebPack plugins, but leave the rest as it was and removes the necessity to eject.

    0 讨论(0)
  • 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()
    
    0 讨论(0)
  • 2020-11-22 13:46

    This is special restriction added by developers of create-react-app. It is implemented in ModuleScopePlugin to ensure files reside in src/. That plugin ensures that relative imports from app's source directory don't reach outside of it.

    You can disable this feature (one of the ways) by eject operation of create-react-app project.

    Most features and its updates are hidden into the internals of create-react-app system. If you make eject you will no more have some features and its update. So if you are not ready to manage and configure application included to configure webpack and so on - do not do eject operation.

    Play by the existing rules (move to src). But now you can know how to remove restriction: do eject and remove ModuleScopePlugin from webpack configuration file.


    Instead of eject there are intermediate solutions, like rewire which allows you to programmatically modify the webpack config without eject. But removing the ModuleScopePlugin plugin is not good - this loses some protection and does not adds some features available in src.

    The better way is to add fully working additional directories similar to src. This can be done using react-app-rewire-alias


    Do not import from public folder - that will be duplicated in the build folder and will be available by two different url (or with different ways to load), which ultimately worsen the package download size.

    Importing from the src folder is preferable and has advantages. Everything will be packed by webpack to the bundle with chunks optimal size and for best loading efficiency.

    0 讨论(0)
  • 2020-11-22 13:46

    If your images are in the public folder then you should use

    "/images/logo_2016.png"
    

    in your <img> src instead of importing

    '../../public/images/logo_2016.png'; 
    

    This will work

    <img className="Header-logo" src="/images/logo_2016.png" alt="Logo" />
    
    0 讨论(0)
  • 2020-11-22 13:47

    You don't need to eject, you can modify the react-scripts config with the rescripts library

    This would work then:

    module.exports = config => {
      const scopePluginIndex = config.resolve.plugins.findIndex(
        ({ constructor }) => constructor && constructor.name === "ModuleScopePlugin"
      );
    
      config.resolve.plugins.splice(scopePluginIndex, 1);
    
      return config;
    };
    
    0 讨论(0)
  • 2020-11-22 13:49

    To offer a little bit more information to other's answers. You have two options regarding how to deliver the .png file to the user. The file structure should conform to the method you choose. The two options are:

    1. Use the module system (import x from y) provided with react-create-app and bundle it with your JS. Place the image inside the src folder.

    2. Serve it from the public folder and let Node serve the file. create-react-app also apparently comes with an environment variable e.g. <img src={process.env.PUBLIC_URL + '/img/logo.png'} />;. This means you can reference it in your React app but still have it served through Node, with your browser asking for it separately in a normal GET request.

    Source: create-react-app

    0 讨论(0)
提交回复
热议问题