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.

[next-js] setImmediate is not defined

See original GitHub issue

šŸ› Bug Report

Environment

  Expo CLI 3.18.6 environment info:
    System:
      OS: macOS 10.15.2
      Shell: 3.2.57 - /bin/bash
    Binaries:
      Node: 12.13.1 - /usr/local/bin/node
      Yarn: 1.22.4 - /usr/local/bin/yarn
      npm: 6.12.1 - /usr/local/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    IDEs:
      Xcode: 11.3/11C29 - /usr/bin/xcodebuild
    npmPackages:
      expo: ~37.0.3 => 37.0.7 
      react: ~16.9.0 => 16.9.0 
      react-native: https://github.com/expo/react-native/archive/sdk-37.0.1.tar.gz => 0.61.4 
    npmGlobalPackages:
      expo-cli: 3.18.6

expo + next.js app (SDK 37)

Steps to Reproduce

Create a next.js project with expo, import react-native-reanimated, use it in a component, and you’ll see Error was not caught ReferenceError: setImmediate is not defined. in the browser console errors.

This also might be related to expo-web-browser being imported. I haven’t dug too deep, but I found the solution and will put it at the bottom of this issue.

Expected Behavior

App works.

Actual Behavior

App throws error:

Screen Shot 2020-04-24 at 3 26 22 PM
Error was not caught ReferenceError: setImmediate is not defined

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

6reactions
nandorojocommented, Jun 10, 2020

Solution

Polyfill setImmediate, which is used by react-native-reanimated.

yarn add setimmediate

In pages/_app.tsx, at the top of the file:

import 'setimmediate'

Here’s my pages/_app.tsx file, for reference:

// fix the setImmediate usage from react-native-reanimated
import 'setimmediate'

if (!global.setImmediate) {
  global.setImmediate = setTimeout
}

import React from 'react'
import { AppProps } from 'next/app'
import Providers from '../src/providers'
import styled from 'styled-components/native'
import { ThemeProps } from '../src/theme'
import Header from '../src/components/Header.web'


if (process.browser) {
  if (location.protocol !== 'https:' && process.env.NODE_ENV !== 'production') {
    console.warn(
      'This app requires HTTPS. Try cancelling this session and running yarn https.'
    )
  } else {
    const WebBrowser = require('expo-web-browser')
    WebBrowser.maybeCompleteAuthSession()
  }
}

const Wrapper = styled.View`
  flex: 1;
  background-color: ${({ theme }: ThemeProps) => theme.colors.background};
`

export default ({ Component, pageProps, router }: AppProps) => {
  return (
    <Providers>
      <Header />
      <Wrapper>
        <Component {...pageProps} key={router.route} />
      </Wrapper>
    </Providers>
  )
}

I figured I’d post this for anyone who finds themselves here. I bet I’ll also run into this again someday, so this is for future me as well.

2reactions
nandorojocommented, May 29, 2020

Try this:

if (!global.setImmediate) {
  global.setImmediate = setTimeout
}

In pages/_app.tsx

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest Test Error: browserType.launch: setImmediate is not ...
I just added the polyfill.js in my test class : import 'core-js'; after that setImmediate was defined again and works now as intended....
Read more >
Window.setImmediate() - Web APIs - MDN Web Docs
This method is used to break up long running operations and run a callback function immediately after the browser has completed other operationsĀ ......
Read more >
Using Next.js with Expo for Web
Fixes setImmediate is not defined error. A lot of libraries in the React ecosystem use the setImmediate() API (like react-native-reanimated ), which Next.js...
Read more >
react global is not defined | The AI Search Engine You Control
The solution is to either remove this piece of configuration (so React will be bundled with your javascript) or load the React framework...
Read more >
setimmediate examples - CodeSandbox
Learn how to use setimmediate by viewing and forking setimmediate example apps on CodeSandbox. ... nextjs-testing Ā· sangwa7/to-do-list-app.
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