Hossein Fallah
Hossein Fallah

Reputation: 2559

Why next.js does not export named export?

Here's my code:

const videos = [
    "m_9S5cBNvTs",
    "FrlJl2UXRU0"
]

const Learn = ({ videoCode }) => {

    return <a target='_blank' href={`https://youtube.com/watch?v=${videoCode}`}>Random Video ({videoCode})</a>
}

export default Learn

export async function getServerSideProps({ params, res }) {
    const randomVideo = videos[Math.floor(Math.random() * videos.length)];
    return {
        props: {
            videoCode: randomVideo
        }
    }
}

export { videos as CrashVideos }

And here's next.js error:

Module parse failed: Export 'videos' is not defined (22:9)

What's wrong here? Why next.js does not export this variable?

Here's a CodeSandBox of this problem.

Upvotes: 0

Views: 1258

Answers (1)

Samitha Wijesekara
Samitha Wijesekara

Reputation: 241

You cannot use export default and export in single document.

If you have multiple export items write code like below.

export { videos as CrashVideos, Learn };

If you have only one item to export you can use export default.

export default Learn;

Upvotes: 2

Related Questions