skip to Main Content

I have the following project structure:

.src
    .FolderA
        .File3.js
    .FolderB
        .File2.js
    .File1.js

How can I import File1.js from File3.js?
I have tried import ‘../src/File1.js’. But its not working

2

Answers


  1. Because ../ lands inside src (considering folders and files does not start with . as in your example), and there is no src folder.

    Try ../File1.js

    Or add alias to your src folder, e.g.

    // tsconfig.json
    {
      "compilerOptions": {
        "paths": {
          "@/*": ["./src/*"]
        }
      }
    }
    
    import ... from '@File1';
    import ... from '@FolderA/File3';
    
    
    Login or Signup to reply.
  2. You need to adjust your import path to correctly navigate the file structure. Here’s how:

    // File3.js
    import File1 from '../FolderB/File1.js';
    

    Explanation:

    • ..: This represents going up one level in the directory structure.
    • FolderB: Navigates to the FolderB directory.
    • File1.js: Imports the File1.js file within FolderB.

    Example:

    Let’s say you have the following file structure:

    src
      └── FolderA
          └── File3.js
    src
      └── FolderB
          └── File1.js
    

    To import File1.js from File3.js:

    // src/FolderA/File3.js
    import File1 from '../../FolderB/File1.js';
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search