97

I have a Vue 2 project, and I've written a simple function for translating months in dates, which I would like to import in one of my components, but I'm getting an error:

export 'default' (imported as 'translateDate') was not found in '@/utils/date-translation'

The relative file path from the src folder is correct, and I am exporting the function like this:

export function translateDate(date) {
  // my code
}

And then I am importing it in the component like this:

import translateDate from '@/utils/date-translation'

What am I doing wrong?

Eric J.
  • 147,927
  • 63
  • 340
  • 553
Leff
  • 1,968
  • 24
  • 97
  • 201
  • Use `export default function...` and see answers to [What is "export default" in Javascript?](https://stackoverflow.com/questions/21117160/what-is-export-default-in-javascript) – Jordi Nebot Sep 01 '17 at 07:22
  • Possible duplicate of [What is "export default" in javascript?](https://stackoverflow.com/questions/21117160/what-is-export-default-in-javascript) – nu11p01n73R Sep 01 '17 at 07:26
  • 4
    *"What am I doing wrong?"* You are trying to import the default export of a module which does not have a default export. The module only has a named export, `translateDate`. You should have a look at the accepted answer of [When should I use curly braces for ES6 import?](https://stackoverflow.com/q/36795819/218196) – Felix Kling Sep 01 '17 at 07:31

6 Answers6

75

You have to specify default explicitly:

export default function translateDate(date) {
   ..
}
Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
  • Thanks, It was caused by a Visual Studio shortcut I was using in my .jsx components and pages. I used rafc(ReactArrowFunctionComponent) instead of rafce(ReactArrowFunctionExportComponent). all errors fixed now. rafc didn't have export default Component. – Tankiso Thebe May 24 '22 at 15:15
68

In my case I had to remove '{' and '}' arround the imported component :

import { CustomComponent } from './CustomComponent';

with

import CustomComponent from './CustomComponent';
pushStack
  • 3,315
  • 1
  • 15
  • 14
57

Either specify default as mentioned above, or if you're trying to export multiple items from the same file you need to import them with curly brackets.

So you would have:

export function doWork(){}
export const myVariable = true;

And then you'd import them in a separate file as:

import { doWork, myVariable} from "./myES6Module"
Alex D
  • 970
  • 6
  • 13
  • 5
    When I use the code you provided, I have this issue : "export 'default' (imported as 'doWork') was not found in './mymodule' – HRK44 Aug 07 '18 at 09:24
  • 1
    That's because you didn't export the thing you wanted to export in your module, or you're using a different name for it. When using named exports you need to use the same name in both files. – Alex D Feb 22 '19 at 15:28
  • What if my function looks like this : export const function = (arg) => { } – pushStack Nov 14 '20 at 18:55
  • 2
    I use curly brases, and exported the function. still getting this error – Sh eldeeb Dec 14 '20 at 11:59
2

Maybe you have two files with the same name.For example, "test.vue" and "test.js"

2

Rather than using

   export function translateDate(date) {
  // my code
}
 

use

     function translateDate(date){
         //code
          }

export default translateDate;

it worked for me...

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 27 '22 at 09:17
1

You need to set symlink setting in vue.config.js

config.resolve.symlinks(false);
Farid Vatani
  • 626
  • 1
  • 7
  • 24