blitz-js / next-superjson-plugin

SuperJSON Plugin for Next.js Pages and Components
200 stars 13 forks source link

Error: next tried to access next-superjson-plugin, but it isn't declared in its dependencies; this makes the require call ambiguous and unsound. #56

Closed bazo closed 1 year ago

bazo commented 1 year ago

Verify Next.js canary release

Describe the bug

getting this error:

error - ../../.yarn/__virtual__/next-virtual-aa7c738504/0/cache/next-npm-13.0.6-canary.0-a5832376b0-6f9e3bc3ce.zip/node_modules/next/dist/pages/_document.js
Error: next tried to access next-superjson-plugin, but it isn't declared in its dependencies; this makes the require call ambiguous and unsound.

Required package: next-superjson-plugin
Required by: next@virtual:e293270790bda2411a3626e57e15862403457209cbe755a222c752c37c8f53870ac5708914fca9c46ad11f81505a4af613c8b5e4bba765909191bb5f14d3b656#npm:13.0.6-canary.0 (via monorepo/.yarn/__virtual__/next-virtual-aa7c738504/0/cache/next-npm-13.0.6-canary.0-a5832376b0-6f9e3bc3ce.zip/node_modules/next/dist/build/swc/)

Require stack:
- monorepo/.yarn/__virtual__/next-virtual-aa7c738504/0/cache/next-npm-13.0.6-canary.0-a5832376b0-6f9e3bc3ce.zip/node_modules/next/dist/build/swc/options.js
- monorepo/.yarn/__virtual__/next-virtual-aa7c738504/0/cache/next-npm-13.0.6-canary.0-a5832376b0-6f9e3bc3ce.zip/node_modules/next/dist/build/swc/index.js
- monorepo/.yarn/__virtual__/next-virtual-aa7c738504/0/cache/next-npm-13.0.6-canary.0-a5832376b0-6f9e3bc3ce.zip/node_modules/next/dist/build/output/store.js
- monorepo/.yarn/__virtual__/next-virtual-aa7c738504/0/cache/next-npm-13.0.6-canary.0-a5832376b0-6f9e3bc3ce.zip/node_modules/next/dist/build/output/index.js
- monorepo/.yarn/__virtual__/next-virtual-aa7c738504/0/cache/next-npm-13.0.6-canary.0-a5832376b0-6f9e3bc3ce.zip/node_modules/next/dist/cli/next-dev.js
- monorepo/.yarn/__virtual__/next-virtual-aa7c738504/0/cache/next-npm-13.0.6-canary.0-a5832376b0-6f9e3bc3ce.zip/node_modules/next/dist/lib/commands.js
- monorepo/.yarn/__virtual__/next-virtual-aa7c738504/0/cache/next-npm-13.0.6-canary.0-a5832376b0-6f9e3bc3ce.zip/node_modules/next/dist/bin/next
    at Array.map (<anonymous>)`

### Expected behavior

no errors :)

### Reproduction link

_No response_

### Version

0.4.9

### Config

```javascript
/** @type {import('next').NextConfig} */
const nextConfig = {
    reactStrictMode: true,
    swcMinify: true,
    experimental: {
        swcPlugins: [
            [
                "next-superjson-plugin",
                {
                    excluded: [],
                },
            ],
        ],
    },
};

module.exports = nextConfig;

Additional context

i have a monorepo using yarn workspaces, yarn 3 & pnp

deps of this particular workspace


"dependencies": {
        "@types/node": "18.11.9",
        "@types/react": "18.0.25",
        "@types/react-dom": "18.0.9",
        "cldr": "^7.3.0",
        "database": "workspace:*",
        "firebase": "^9.14.0",
        "firebase-admin": "^11.3.0",
        "next": "canary",
        "next-firebase-auth": "canary",
        "next-superjson-plugin": "^0.4.9",
        "react": "18.2.0",
        "react-dom": "18.2.0",
        "superjson": "^1.11.0",
        "typescript": "4.9.3",
        "winston": "^3.8.2"
    },
``
orionmiz commented 1 year ago

Unfortunately, SWC Plugin system does not support Yarn PnP yet. Related issue

Here are the workarounds:

  1. Use node-modules as nodeLinker option.
  2. Use babel plugin instead.
tonyfromundefined commented 1 year ago

@bazo

This error is intentional by yarn pnp, which occurs when the next package tries to use the next-superjson-plugin package, but next-superjson-plugin is not included in the dependencies of the next package.

You can solve it by following the steps below.

  1. Add dependencies to packages explicitly. This can be done via the packageExtensions extension present in .yarnrc.yml as follows:
packageExtensions:
  next@*:
    dependencies:
      next-superjson-plugin: "^0.5.2"
  1. next-superjson-plugin needs to be unplugged because native module is included. It would be nice if the package was automatically unplugged, but at the moment (version 0.5.2) it doesn't seem to be supported yet. So, unplug it manually with the following command.
$ yarn unplug next-superjson-plugin