23

Trying to create an xterm react component in Next.js I got stuck as I'm not able to get over an error message I've never got before.

I'm trying to import a npm client-side module called xterm, but if I add the import line the application crashes.

import { Terminal } from 'xterm'

The error reads Server Error... ReferenceError: self is not defined and then shows this chunk of code as Source

module.exports = require("xterm");

According to some research I did, this has to do with Webpack and could be helped if something like this was done:

output: {
  globalObject: 'this'
}

Would you know how to fix this?

1 Answer 1

77

The error occurs because the library requires Web APIs to work, which are not available when Next.js pre-renders the page on the server-side.

In your case, xterm tries to access the window object which is not present on the server. The solution is to avoid loading xterm on the server and dynamically import it so it gets loaded on the client-side only.

There are a couple of ways to achieve this in Next.js.


#1 Using dynamic import() inside useEffect

Move the import to your component's useEffect, then dynamically import the library and add your logic there.

useEffect(() => {
    const initTerminal = async () => {
        const { Terminal } = await import('xterm')
        const term = new Terminal()
        // Add logic with `term`
    }
    initTerminal()
}, [])

#2 Using next/dynamic with ssr: false

Create a component where you add the xterm logic.

// components/terminal-component
import { Terminal } from 'xterm'

function TerminalComponent() {
    const term = new Terminal()
    // Add logic around `term`
    return <></>
}

export default TerminalComponent

Then dynamically import that component when using it.

import dynamic from 'next/dynamic'

const TerminalComponent = dynamic(() => import('<path-to>/components/terminal-component'), {
    ssr: false
})

As an alternative, you could add the logic directly when dynamically importing the library with next/dynamic to avoid having an extra file for it.

import dynamic from 'next/dynamic'

const Terminal = dynamic(
    {
        loader: () => import('xterm').then((mod) => mod.Terminal),
        render: (props, Terminal) => {
            const term = new Terminal()
            // Add logic with `term`
            return <></>
        }
    },
    {
        ssr: false
    }
)
7
  • Thank you for your response! I've tried this but it doesn't seem to be importing the { Terminal } constructor. What I get instead is an someobject = {} with a $$typeof: and render: properties. Would you know why this would be? Feb 8, 2021 at 19:54
  • 2
    I've solved it by doing an import { Terminal } from 'xterm' in my component and then importing that <Component /> with the dynamic() function in the parent page. I could mark your answer as correct if you mention this detail. Feb 8, 2021 at 20:23
  • 1
    Ah right, I assumed Terminal was a component - Next.js dynamic must return a component. I'll update my answer. Feb 8, 2021 at 20:46
  • 1
    You have no idea how your answer saved me.. thanks... Apr 4 at 10:57
  • 1
    @crollywood Using use client does not make the component only render on the client-side. It simply means the component will also get rendered on the client in addition to the server-side rendering. As opposed to Server Components (components that do not use use client), where the rendering only occurs on the server. Sep 8 at 12:44

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.