Relative Imports Outside of src Directory in create-react-app
Create-react-app enforces a strict restriction against importing assets from outside the src directory. This restriction is implemented via the ModuleScopePlugin, which prevents relative imports from breaching the src directory boundary.
Root Cause
This restriction ensures that all application-related files remain within the src directory, providing a clear separation between code and other project assets.
Impact
Attempting to import files from outside the src directory, such as accessing images from the public folder, results in an error message indicating the ModuleScopePlugin restriction.
Unofficial Solutions
There are unofficial workaround solutions that leverage rewire to modify the webpack configuration and remove the ModuleScopePlugin. However, this approach is discouraged as it weakens the security and feature enhancements provided by the plugin.
Recommended Approach
To resolve the issue while maintaining the integrity of the create-react-app environment, you can:
Advantages of Importing from src
Importing from src offers several advantages:
The above is the detailed content of How Can I Import Assets Outside the `src` Directory in Create React App?. For more information, please follow other related articles on the PHP Chinese website!