webpack: Module not found: Error: Can't resolve (with relative path)
Your file structure says that folder name is Container
with a capital C. But you are trying to import it by container
with a lowercase c. You will need to change the import or the folder name because the paths are case sensitive.
Webpack can't resolve relative path import express static
You're using an absolute path
import getGraphAccessToken from "/javascripts/ssoAuthES6.js";
// ^ this will look in your topmost directory on your OS
The relative path, from commands.js
, would be:
import getGraphAccessToken from "../../javascripts/ssoAuthES6.js";
Alternatively, you can set Webpack to look for modules from your root directory by adding the following to your webpack configuration:
{
// ...
resolve: {
modules: [path.resolve(__dirname, "src"), "node_modules"],
},
// ...
}
Then you can import from your project's root directory from anywhere, like so:
import getGraphAccessToken from "javascripts/ssoAuthES6.js";
Some other points:
- Since you're setting the
extensions: [".ts", ".tsx", ".html", ".js"]
, you don't need to provide file extensions for those imports - You specify
.ts
and.tsx
in your webpack config, but you are using.js
files. Consider removing the Typescript extensions - If you are using Typescript, you will need to update import paths in your
tsconfig.json
- You can consider import path aliases in both Webpack and Typescript to be more explicit that your imports are coming from your project root. Instructions here
How to Use Absolute Path for npm run build
In the Webpack configuration file, add resolve.alias
in order to create aliases and to import modules more easily.
module.exports = {
//...
resolve: {
alias: {
Components: path.resolve(__dirname, 'src/components/')
}
}
}
For instance, all modules that live inside src/components/
can now use absolute paths.
And for the VS code
to recognize the path
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"components/*": ["src/components/*"]
}
},
"exclude": ["node_modules"]
}
Unable to import module when using alias in package json
That question applies to nodejs, not React. You can add configurations to your React app with craco
.
- Install
craco
withyarn
ornpm
yarn add @craco/craco
- Create a
craco.config.js
file in your project's root and insert your aliases
const path = require(`path`);
module.exports = {
webpack: {
alias: {
'#components': path.resolve(__dirname, 'src/components'),
'#x': path.resolve(__dirname, 'src/x'),
}
},
};
- Replace
react-scripts
under thescripts
section inpackage.json
tocraco
like this
"scripts": {
"start": "craco start",
"build": "craco build",
...
},
- Restart the dev server and you should be able to use your aliases.
Related Topics
How to Convert Raw Mp3 Binary into Blob Url and Play It in Audio Tag
Using Thymeleaf Variable in Onclick Attribute
How to Clear Input After Onclick With Reactjs
Pass Data from HTML Table to Modal and Then to PHP Script
Call PHP Function With Onclick Event
Open Html5 Date Picker on Icon Click
How to Store Data After Refreshing the Web Page
Convert Time Stamp to Date Inside Angular Expression Like {{New Date(Timestamp)}}
Filter Json Object Array on Multiple Values or Arguments JavaScript
How to Uncheck All Below Checkboxes When One Checkbox Is Unchecked
How to Output a Variable from JavaScript to HTML
Horizontal Scrollbar on Top and Bottom of Table
How to Execute a Function After Another One
How to Unit Test If an Element Is Visible When the *Ngif Directive Is Used Using Jasmine in Angular
How to Share and Edit Global Variables Between Two JavaScript Node Runs
Reactjs and Images in Public Folder