Home > Blockchain >  Import string from javascript file (react/nextJS)
Import string from javascript file (react/nextJS)

Time:11-17

I have a file "test.js" in my "/constants" folder with the following content:

const test = "test!"

export default test

My page in the "/pages" folder should read the string from "test.js" and print it

import { test } from "../constants/test"

export default function Home() {
    console.log("imported string: "   test)
}

If I run it the browser I get the following output:

"imported string: undefined"

Why is it not reading the string from the file? The path is correct. VSCode autocomplete even finds the file while typing.

CodePudding user response:

You are exporting the constant test as the default export, therefor your import statement should be changed from

import { test } from "../constants/test"

to

import test from "../constants/test"

NOTE: the {} imports are for NON-default exports

CodePudding user response:

If you want to use import { test } then you should use export test instead of using default
Because right now you're telling javascript to find specific export, not default.

  • Related