expatfile / next-runtime-env

Next.js Runtime Environment Configuration - Populates your environment at runtime rather than build time.
https://www.npmjs.com/package/next-runtime-env
MIT License
458 stars 19 forks source link

lib doesn't work for next 14.2 #123

Closed mindmind closed 6 months ago

mindmind commented 7 months ago

Hey everyone, today I upgraded my app with new 14.2.1 next.js version and found next-runtime-env crashing my app in the development runtime:

TypeError: (0 , react_1.createContext) is not a function
    at Object.<anonymous> (/blabla/.next/server/chunks/node_modules_927e2a._.js:1942:48)
    at [project]/node_modules/next-runtime-env/build/provider/env-context.js [app-route] (ecmascript) (/blabla/.next/server/chunks/node_modules_927e2a._.js:1944:3)
    at instantiateModule (/blabla/.next/server/chunks/[turbopack]_runtime.js:520:23)
    at getOrInstantiateModuleFromParent (/blabla/.next/server/chunks/[turbopack]_runtime.js:572:12)
    at commonJsRequire (/blabla/.next/server/chunks/[turbopack]_runtime.js:136:20)
    at Object.<anonymous> (/blabla/.next/server/chunks/node_modules_927e2a._.js:1954:23)
    at [project]/node_modules/next-runtime-env/build/provider/env-provider.js [app-route] (ecmascript) (/blabla/.next/server/chunks/node_modules_927e2a._.js:1972:3)
    at instantiateModule (/blabla/.next/server/chunks/[turbopack]_runtime.js:520:23)
    at getOrInstantiateModuleFromParent (/blabla/.next/server/chunks/[turbopack]_runtime.js:572:12)
    at commonJsRequire (/blabla/.next/server/chunks/[turbopack]_runtime.js:136:20)
    at Object.<anonymous> (/blabla/.next/server/chunks/node_modules_927e2a._.js:2063:22)
    at [project]/node_modules/next-runtime-env/build/provider/index.js [app-route] (ecmascript) (/blabla/.next/server/chunks/node_modules_927e2a._.js:2085:3)
    at instantiateModule (/blabla/.next/server/chunks/[turbopack]_runtime.js:520:23)
    at getOrInstantiateModuleFromParent (/blabla/.next/server/chunks/[turbopack]_runtime.js:572:12)
    at commonJsRequire (/blabla/.next/server/chunks/[turbopack]_runtime.js:136:20)
    at Object.<anonymous> (/blabla/.next/server/chunks/node_modules_927e2a._.js:2434:14)
    at [project]/node_modules/next-runtime-env/build/index.js [app-route] (ecmascript) (/blabla/.next/server/chunks/node_modules_927e2a._.js:2444:3)
    at instantiateModule (/blabla/.next/server/chunks/[turbopack]_runtime.js:520:23)

it works fine for me if I use next.js 14.1

I found that next.js swear on import { createContext } from 'react'; and asks to add 'use client' for it. Maybe the reason is somewhere around.

tefkah commented 7 months ago

Not sure if it's the right way to go about it, but I found that setting

    experimental: {
        serverComponentsExternalPackages: ["next-runtime-env"],
    }

seems to solve my issue.

Even though the issue was happening in API routes for me.

EDIT: This brought up new issues during build, which I solved like

export default (phase, { defaultConfig }) => {
    if (phase !== "phase-development-server") {
        return modifiedConfig;
    }

    return {
        ...modifiedConfig,
        experimental: {
            ...modifiedConfig.experimental,
            serverComponentsExternalPackages: [
                ...(modifiedConfig.experimental?.serverComponentsExternalPackages ?? []),
                // we need this to be external (for now) during dev, but not during build
                // https://github.com/expatfile/next-runtime-env/issues/123
                "next-runtime-env",
            ],
        },
    };
};
mindmind commented 7 months ago

@tefkah thanks

experimental: {
        serverComponentsExternalPackages: ["next-runtime-env"],
    }

solved the issue for me

HofmannZ commented 6 months ago

This seems to only be related to the context approach. The suggested patch seems to work and we've updated the example in #127.