Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 BUG: wrangler pages dev failing with error on Identifier '__cf_cjs' has already been declared #7572

Open
anywhichway opened this issue Dec 17, 2024 · 10 comments
Labels
bug Something that isn't working needs reproduction Needs reproduction from OP

Comments

@anywhichway
Copy link

Which Cloudflare product(s) does this pertain to?

Pages

What version(s) of the tool(s) are you using?

Wrangler 3.96.0

What version of Node are you using?

N/A

What operating system and version are you using?

Windows 11

Describe the Bug

Observed behavior

X [ERROR] service core:user:bitter-bread-d12c: Uncaught SyntaxError: Identifier '__cf_cjs' has already been declared

    at null.<anonymous> (fn7s4pcgxfq.js:1026)

Expected behavior

App starts

Steps to reproduce

Occurs when the code below is added as a function

Note: You will need to modify line 251 in json-bigint and comment out //case 'null': not valid because your linter also causes a fatal failure on this line

import { PromptRole } from "@llumiverse/core";
import { TogetherAIDriver } from "@llumiverse/drivers";
export async function onRequestPost(ctx) {
    const driver = new TogetherAIDriver({
        apiKey: "<some key>"
    });

    const prompt = [
        {
            role: PromptRole.user,
            content: 'Please, write a short story about winter in Paris, in no more than 512 characters.'
        }
    ]

    const body = await driver.execute(prompt, {
        model:"meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
        temperature: 0.6,
        max_tokens: 1024
    });
    return new Response(200,{body:JSON.stringify(body)})
    //return new Response(200,{body:JSON.stringify(prompt)});
}

Please provide a link to a minimal reproduction

No response

Please provide any relevant error logs

wrangler-2024-12-17_15-58-15_864.log

@anywhichway anywhichway added the bug Something that isn't working label Dec 17, 2024
@github-project-automation github-project-automation bot moved this to Untriaged in workers-sdk Dec 17, 2024
@Webstablish
Copy link

Webstablish commented Dec 18, 2024

Having the same issue unfortunately.

Uncaught SyntaxError: Identifier '__cf_cjs' has already been declared

In my case it happens when I add a SDK to the project to make API calls to a third party. Commenting out the functions that use the SDK fixes the issue, but breaks the app.

Updating to the latest wrangler version didn't help.

@emily-shen
Copy link
Contributor

Hey thanks for the report! Can I check if you're all using nodejs_compat? And @Webstablish would be super helpful to know which SDK this is happening with.

@Webstablish
Copy link

@emily-shen, currently I have this in my wrangler.toml:
compatibility_flags = ["nodejs_compat_v2"]

The SDK I'm using is monday-sdk-js.

@anywhichway
Copy link
Author

anywhichway commented Dec 18, 2024 via email

@andyjessop andyjessop moved this from Untriaged to Backlog in workers-sdk Dec 20, 2024
@anywhichway
Copy link
Author

@emily-shen @andyjessop FYI, I tried editing the source of wrangler to make this change. It did not fix the problem.

build5.initialOptions.banner.js += typeof __cf_cjs === "undefined" ? esm_default2`
		function __cf_cjs(esm) {
		  const cjs = 'default' in esm ? esm.default : {};
			for (const [k, v] of Object.entries(esm)) {
				if (k !== 'default') {
					Object.defineProperty(cjs, k, {
						enumerable: true,
						value: v,
					});
				}
			}
			return cjs;
		}
		` : __cf_cjs;

@emily-shen
Copy link
Contributor

Thanks for all the responses so far, sorry for the slow responses over the holiday period :) if you’re able to provide a link to a repo with a minimal reproduction that would be super helpful for us to debug and fix 🧡

@liuhuapiaoyuan
Copy link

https://developers.cloudflare.com/workers/runtime-apis/nodejs/streams/

I want to use streams, but the following configurations both result in this error. How should I handle it?

compatibility_flags = ["nodejs_compat_v2"]
compatibility_date = "2024-07-01"
compatibility_flags = ["nodejs_compat"]
compatibility_date = "2024-10-01"

@emily-shen emily-shen added the needs reproduction Needs reproduction from OP label Jan 2, 2025
@tori-takashi
Copy link

tori-takashi commented Jan 8, 2025

I got a same problem trying to use twitter-api-sdk.
https://www.npmjs.com/package/twitter-api-sdk

import { Hono } from 'hono';
import { handle } from 'hono/cloudflare-pages';
import { env } from 'hono/adapter'
import { Client } from "twitter-api-sdk";

export const app = new Hono();

app.post('api/test', async (c) => {
const { X_BEARER_TOKEN } = env<{
        X_BEARER_TOKEN: string;
    }>(c)
const XClient = new Client(X_BEARER_TOKEN as string);
});

export const onRequest = handle(app);

on wrangler 3.100.0.

When I remove const XClient = new Client(X_BEARER_TOKEN as string); then no error displayed.

@devmax214
Copy link

I got the same error using i18next-fs-backend and [email protected].
My configs

compatibility_flags = [ "nodejs_compat" ]
compatibility_date = "2024-10-01"

Also in my case, when I used the function eval, the error was presenting.
After I replaced the eval it fixed. But when I add the module i18next-fs-backend, the error is showing again.

@devmax214
Copy link

devmax214 commented Jan 14, 2025

I changed the wrangler cli source, so I replaced __cf_cjs with globalThis.__cf_cjs.

...
build5.initialOptions.banner.js += esm_default2`
	globalThis.__cf_cjs = function (esm) {
        const cjs = 'default' in esm ? esm.default : {};
        for (const [k, v] of Object.entries(esm)) {
            if (k !== 'default') {
                Object.defineProperty(cjs, k, {
                    enumerable: true,
                    value: v,
                });
            }
        }
        return cjs;
    };
		`;
  build5.onLoad(
    { filter: /.*/, namespace: REQUIRED_UNENV_ALIAS_NAMESPACE },
    ({ path: path70 }) => {
      return {
        contents: esm_default2`
		import * as esm from '${path70}';
		module.exports = globalThis.__cf_cjs(esm);
	`,
        loader: "js"
      };
    }
  );
...

It works for me.
@emily-shen please check at next version. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something that isn't working needs reproduction Needs reproduction from OP
Projects
Status: Backlog
Development

No branches or pull requests

6 participants