When using ES6 modules, the way you handle file paths can indeed be a bit different, but the basic principles of relative paths remain the same. Some examples are:
Root-relative path (/): This is not typically used in ES6 module imports because ES6 modules usually rely on relative paths or module resolution strategies defined by the environment or bundler.
Current directory (./): This works the same way as in HTML. EG;
import myModule from './myModule.js'; looks for myModule.js in the same directory as the current file.
Parent directory (../): This also works the same way. EG;
import myModule from '../myModule.js'; will look for myModule.js one directory level up from the current file.
No prefix: In ES6 modules, this is not typically used for local files. Instead, it is used for importing modules from node_modules or other configured module directories. EG;
import React from 'react'; will look for the react module in node_modules.
Additionally, ES6 modules can be configured with module bundlers like Webpack, which allow you to set up custom path resolutions. For example, you can define aliases to simplify your import paths.
Here's an example of how you might use these paths in ES6 modules:
// Current directory
import myModule from './myModule.js';
// Parent directory
import myModule from '../myModule.js';
// Node modules or configured paths
import React from 'react';
If you're using a bundler like Webpack, you can configure it to handle paths more flexibly:
// webpack.config.js
module.exports = {
resolve: {
alias: {
'@components': path.resolve(__dirname, 'src/components/'),
},
},
};
This way, you can import modules using the alias:
import MyComponent from '@components/MyComponent';
In HTML, the src attribute is used to specify the path to an external resource. Some examples are:
/ (Root-relative path): This prefix points to the root directory of the website. EG; src="/images/pic.jpg" looks for the pic.jpg file in the images folder located at the root of the website.
./ (Current directory): This prefix refers to the current directory where the HTML file is located. EG; src="./images/pic.jpg" looks for the pic.jpg file in the images folder within the same directory as the HTML file.
../ (Parent directory): This prefix moves up one directory level. EG; src="../images/pic.jpg" looks for the pic.jpg file in the images folder located one level up from the current directory.
No prefix: When you don't use any prefix, the path is considered relative to the current directory. EG; src="images/pic.jpg" looks for the pic.jpg file in the images folder within the same directory as the HTML file.
Summary in code form:
<img src="images/pic.jpg" alt="Root-relative path">
<img src="./images/pic.jpg" alt="Current directory">
<img src="../images/pic.jpg" alt="Parent directory">
<img src="images pic.jpg" alt="No prefix">