Import Error States File is outside of src/ folder when the file is for sure really inside of src/

Viewed 15

I was attempting to build a practice project to learn Redux. I'm getting an error message saying that the file I'm importing is outside of src/ but the file is for sure located inside of src/. I've tried changing my folder structure, migrating my code to a new React App and the error proceeds. I really just want to understand the source of this error so that I can avoid it in the future.

The error message, screenshot of my folder structure is below. I've also included the Users component where the import error occurs.

Thank you for your time!

File Structure

Error Message:

Compiled with problems:X

ERROR in ./src/Users.js 4:0-43

Module not found: Error: You attempted to import ../FakeData.js which falls outside of the project src/ directory. 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/.

    import {createSlice} from '@reduxjs/toolkit';
    import { UsersData } from '../FakeData.js';
    
    export const userSlice = createSlice({
            name: 'users',
            initialState: {value: UsersData },
            reducers: {
                addUser: (state, action) => {
                    // Write code for adding a user 
                }
            },
            
        }
    );
    
    export default userSlice.reducer; 
1 Answers

The import needs to be import { UsersData } from './FakeData.js'.

Not: import { UsersData } from '../FakeData.js';

The extra period in ../FakeDate.js was causing the error by telling it to look outside of the src folder.

This was a simple copy and paste error. When I migrated the App and changed the folder structure I forgot to take away one of the periods. I will be asking a follow up question about why I got the error before migrating the App. In that scenario the complier was looking inside of the same folder that Users.js is located in even with ../FakeDate.js directing it go back outside of the features folder.

Related