To import component outside src/ directory with React, we can declare a local dependency on package. json. { //... "dependencies": { "app-b-dashboard": "file:./packages/app-b-dashboard" //... } //... } to declare the app-b-dashboard dependency in the dependencies section of package.
Relative imports outside of src/ are not supported. You can either move it inside src/, or add a symlink to it from project's node_modules/.
Use a dot for the path to create a React app in the current directory, e.g. npx create-react-app . or npx create-react-app . --template typescript for TypeScript projects. Make sure your folder name doesn't contain special characters, spaces or capital letters.
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.
There is no official way to disable this feature except using eject
and modify webpack config.
But, most features and its updates are hidden into the internals of create-react-app system. If you make eject
you will have no more new 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 assets to src or use based on public
folder url without import.
However instead of eject
there are much unofficial solutions, based on
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
. ModuleScopePlugin
is designed to support multiple folders.
The better way is to add fully working additional directories similar to src
also protected by ModuleScopePlugin
. This can be done using react-app-rewire-alias
Anyway do not import from public
folder - that will be duplicated in the build
folder and will be available by two different url (and 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.
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.
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" />
Remove it using Craco:
module.exports = {
webpack: {
configure: webpackConfig => {
const scopePluginIndex = webpackConfig.resolve.plugins.findIndex(
({ constructor }) => constructor && constructor.name === 'ModuleScopePlugin'
);
webpackConfig.resolve.plugins.splice(scopePluginIndex, 1);
return webpackConfig;
}
}
};
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:
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With