27

I have such project structure:

- MyApp
`- firstFolder
 `- firstFile.js
`- secondFolder
 `- thirdFolder
  `- thirdFile.js

How can I import firstFile.js from thirdFile.js?

Something like import myFunc from '../firstFolder/firstFile'; in thirdFile.js, doesn't work.

4
  • import ../../firstFolder/firstFile.js; or import ./firstFolder/firstFile.js Commented Feb 13, 2017 at 17:10
  • Have you tried '../../firstFolder/firstFile', since you need to go two dirs up? Commented Feb 13, 2017 at 17:11
  • 1
    @abc123 second answer doesn't work. Commented Feb 13, 2017 at 17:13
  • See levelup.gitconnected.com/… Commented Jul 22, 2020 at 16:39

4 Answers 4

24

Description

There are multiple pathing options, I'll describe a couple below.

Folder Structure

MyApp
├── firstFolder
│   └── firstFile.js
└── secondFolder
    └── thirdFolder
        └── thirdFile.js

Relative path

Here we will import the firstFile from our thirdFile.

../ will go back 1 folder, this is why we go back two folders:

import myFunc from '../../firstFolder/firstFile'

So .. takes us back to secondFolder then we need to go back one more folder .. into the MyApp folder. Now we can traverse forward into firstFolder then point to firstFile.

or ./ - this is the present working directory (pwd), which will be thirdFolder from here we will need to go back 2 directories or ../..

import myFunc from './../../firstFolder/firstFile'

Full path

Since you didn't specify the full paths these are incomplete

/ - will start at the root directory of the Operating System

import myFunc from '/fullPathtoProject/MyApp/firstFolder/firstFile'

~/ this is the current users home directory

import myFunc from '~/pathFromHomeDirectory/MyApp/firstFolder/firstFile'
Sign up to request clarification or add additional context in comments.

10 Comments

@YuriyN. try / instead of ./ that is a nodejs reference
@YuriyN. k, then you'll need to use ../, can you try ~/, also are you just using these as the first character? can you post the full strings?
I'll try all, and then write results, but I can say now, with certainty, that second variant doesn't work, it relates to current folder.
The example import myFunc from ./firstFolder/firstFile.js is wrong. Single . references the current directory.
The paths must be quoted for anything to work! Use import myFunc from '../../firstFolder/firstFile' with single ' or double quotes ".
|
4

in ES5:

You should use two '..' to back 1 folder level

var myFunc = require('../../firstFolder/firstFile');

ES6 (ecmascript6):

import myFunc from '../../firstFolder/firstFile';

1 Comment

Thanks for the answer! But abc123 was answered it first.
3

If you use webpack you can tweak the loader to search for files relative to your base directory. Change your webpack.config.js to this

resolve: {
  modules: [
    path.resolve('./'),
    path.resolve('./node_modules')
  ]
},

or, if your sources are all relative to ./src, use path.resolve('./src'). If you want to be more specific you can also take a look at resolve.alias (see the webpack docs here: https://webpack.js.org/configuration/resolve/)

BTW: I'm using next.js in my application, which has its own webpack config, but you can modify it if you put something like

const path = require('path');
module.exports = ({
    webpack: (config, options) => {
        config.resolve.modules.push( path.resolve('./') )
        return config
    },
})

into your next.config.js, in order to modify the default one.

Comments

2

use=>

import myFunc from '../../firstFolder/firstFile';

or

import * as myFunc from '../../firstFolder/firstFile';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.