Skip to content

Getting "document is not defined" with Next.js #80

Open

Description

@shehzadasif

Hi there,

I was trying your awesome library that works seamlessly without any framework. I tried it with Next.js and no matter what I do, I am constantly getting the error as attached "document is not defined". Any idea on how to solve this issue and use it with next.js.

splitting error

Activity

  1. jeff-zhenz commented on Dec 31, 2021

    @jeff-zhenz

    see first section of this https://nextjs.org/docs/advanced-features/dynamic-import, splittingjs client-side work only

  2. shehzadasif commented on Dec 31, 2021

    @shehzadasif
    Author
  3. TomSmedley commented on Jan 21, 2022

    @TomSmedley

    I'm having this issue with Svelte. I think since everything is moving over to Vite we really need a ESM module as well as IIFE.

  4. d-e-v-esh commented on Feb 2, 2022

    @d-e-v-esh

    Yes, I am having the same issue with next.js. A working solution with dynamic imports will be really appreciated.

  5. onopko81 commented on Mar 12, 2022

    @onopko81

    Hi, try:
    const Splitting = null;
    if (typeof document !== undefined) Splitting = require('Splitting')

  6. joshtom commented on May 17, 2022

    @joshtom

    Also having some difficulties to get it to work in NextJS.

    I managed to bypass the document undefined using a dynamic import using

     const callSplitting = async () => {
        const Splitting = (await import("splitting")).default;
        if (splitRef) {
          let split_res = Splitting();
          setLines(split_res[0].el);
          console.log(`Split text into ${split_res[0].el} lines`);
        }
      };
    

    And this

    <div ref={splitRef} data-splitting="">
    Content Here
    </div>
    

    But somehow, It's not just working. I would appreciate a solution regarding this @shshaw

  7. draZer0 commented on Aug 19, 2022

    @draZer0

    For anyone who is still struggling to use Splitting with Next.js, I combined all the answers from previous issues and I managed to get it working in with the following code:

    import "splitting/dist/splitting.css";
    import "splitting/dist/splitting-cells.css";
    
    const Component = () => {
    
        let target;
    
        setTimeout(() => {
            if ( window && document && target ) {
                const Splitting = require('Splitting');
                Splitting({ by: "chars", target: target,  });
            }
        });
    
        return (
            <>
                <span ref={(el) => { target = el; }}>Lorem ipsum dolor sit amet</span>
            </>
        )
    }
    
    export default Component;
  8. joshtom commented on Aug 19, 2022

    @joshtom

    For anyone who is still struggling to use Splitting with Next.js, I combined all the answers from previous issues and I managed to get it working in with the following code:

    import "splitting/dist/splitting.css";
    import "splitting/dist/splitting-cells.css";
    
    const Component = () => {
    
        let target;
    
        setTimeout(() => {
            if ( window && document && target ) {
                const Splitting = require('Splitting');
                Splitting({ by: "chars", target: target,  });
            }
        });
    
        return (
            <>
                <span ref={(el) => { target = el; }}>Lorem ipsum dolor sit amet</span>
            </>
        )
    }
    
    export default Component;

    Thank you for this @draZer0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions