Search code examples
javascriptecmascript-6es6-modulesjavascript-import

ES6-Importing method from one file to another gives error


I have an API utils file React JS where i have list of endpoints declared like below-

File1.js-
    export const api_endpoints = {
     api1: ()=> '../data/user'
    }

I want api1() from File1.js in my another file (File2.js) hence i am importing this like below-

import {api_endpoints} from '../File1';
const myObj = {
  [api_endpoints.api1()]: '../data/user2'
}

This gives me error in File2 saying-

"cannot read property api1 of undefined"

I am not sure why api_endpoints comes as undefined in File2.js. The paths are correct. Can someone help me understand that.


Solution

  • Looks like you are importing from wrong path. Tried the above example and it is working fine for me..

    https://codesandbox.io/s/objective-meninsky-7gbch?file=/src/File2.js

    enter image description here