question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exported members that are used in SSR/SSG/ISG page function are undefined on the client (Attempted import error: '..' is not exported from '..' (imported as '..').)

See original GitHub issue

What version of Next.js are you using?

12.0.8

What version of Node.js are you using?

16.13.1

What browser are you using?

Chrome Version 96 (Official Build) (64-bit)

What operating system are you using?

Ubuntu 20.04 via WSL 2 & Windows 10

How are you deploying your application?

next start

Describe the Bug

When you export a member that is used in the page function of getStaticProps, it will be undefined on the client.

For example:

// pages/index.tsx
export const revalidateInSeconds = 5 * 60;

export const getStaticProps: GetStaticProps = async () => {
  return {
    props: {},
    revalidate: revalidateInSeconds,
  };
};

// components/TestComponent.tsx
import { revalidateInSeconds } from "../pages";

export function TestComponent(): JSX.Element {
  console.log("revalidateInSeconds: ", revalidateInSeconds);
  return <p>Test component</p>;
}

Expected Behavior

Exported variables shouldn’t be undefined on the client.

To Reproduce

Reproduction repo

I created a reproduction repo for this bug:

https://github.com/bennettdams/nextjs-bug-export-undefined

  1. git clone git@github.com:bennettdams/nextjs-bug-export-undefined.git
  2. npm i
  3. npm run build
  4. npm run start

OR

Manual reproduce

  1. Create Next.js app: npx create-next-app@latest --use-npm --ts .
  2. Add getStaticProps and a const to export:
export const revalidateInSeconds = 5 * 60;

export const getStaticProps: GetStaticProps = async () => {
  return {
    props: {},
    revalidate: revalidateInSeconds,
  };
};
  1. Add a test component that logs the imported const (revalidateInSeconds) and use the component on the page:
// components/TestComponent.tsx
import { revalidateInSeconds } from "../pages";

export function TestComponent(): JSX.Element {
  console.log("revalidateInSeconds: ", revalidateInSeconds);
  return <p>Test component</p>;
}

// pages/index.ts
...
  <main className={styles.main}>
        <TestComponent />

        <h1 className={styles.title}>
          Welcome to <a href="https://nextjs.org">Next.js!</a>
...
  1. Build & start the app: npm run build & npm run start

Results, looking at the console logs

Terminal that is used for building:

Click for text!
> next build

info  - Checking validity of types  
info  - Creating an optimized production build  
warn  - Compiled with warnings

./components/TestComponent.tsx
Attempted import error: 'revalidateInSeconds' is not exported from '../pages' (imported as 'revalidateInSeconds').

Import trace for requested module:
./pages/index.tsx

info  - Collecting page data  

image

Browser:

image


Additional information:

  • When you run npm run dev instead of using the build, you can see that the console.log on the server actually has the value, instead of being undefined: image Just on the client it is still undefined.

  • I checked this behavior also for Next.js v11.1.2 - there, it is not happening. The variable has its value on the client as expected.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
andtos90commented, Apr 13, 2022

Can confirm it’s still happening to me with Next 12.1.5 (latest version available). I noticed that adding a console log right after the export fix the issue, for example:

export const revalidateInSeconds = 5 * 60;
console.log(revalidateInSeconds);

export const getStaticProps: GetStaticProps = async () => {
  return {
    props: {},
    revalidate: revalidateInSeconds,
  };
};
1reaction
balazsorban44commented, Jan 15, 2022

This sounds like an edge-case in my opinion, but here is what I’ve found.

You have multiple options that might make more sense in this case, and would work as intended after testing with your reproduction.

  1. The TestComponent being used in the same page file could receive the variable as a prop, so you wouldn’t need to export it and import it somewhere else.

  2. Use an environment variable to share information that is used both client and server-side. Even if it’s not a secret, you might want to be mindful about exposing any values being used server-side, so something like process.env.NEXT_PUBLIC_REVALIDATE could make this more explicit

  3. Define the value in a separate config file, that both the component file and the page file import. This way Next.js won’t cut out the value being imported in the component file that’s used in a client bundle.

I checked and indeed this works with Babel, so this is SWC related indeed, but also might be the more correct solution. Let me check with the team!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Receiving "Attempted import error:" in react app - Stack Overflow
Here's how I'm exporting combineReducers : import { combineReducers } from 'redux'; import userReducers from './userReducers' ...
Read more >
import - JavaScript - MDN Web Docs - Mozilla
The static import declaration is used to import read-only live bindings which are exported by another module. The imported bindings are ...
Read more >
Common Errors with Exporting and Importing Translation Files
This error message only applies to data translation files. Data translation was disabled on the field after the translation file was exported. Enable...
Read more >
Documentation - TypeScript 3.8
TypeScript 3.8 adds a new syntax for type-only imports and exports. ... error! 'Component' only refers to a type, but is being used...
Read more >
module.exports – How to Export in Node.js and JavaScript
In programming, modules are components of a program with one or more functions or values. These values can also be shared across the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found