Skip to content

Cannot find module '../data/patch.json' #2149

Open
@whatworksglobal

Description

@whatworksglobal

I'm mainly just posting this issue for visibility and not expecting a fix since I think this is an issue with css-tree library not svgo.

Describe the bug
When using with nextjs app router and deploying to vercel, get:

⨯ Error: Cannot find module '../data/patch.json'
Require stack:
- /vercel/path0/node_modules/css-tree/lib/data-patch.js
    at <unknown> (../../opt/rust/nodejs.js:2:12456)
    at Function.Hr (../../opt/rust/nodejs.js:2:12834)
    at Ae.e.<computed>.Me._load (../../opt/rust/nodejs.js:2:12426) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [Array],
  page: '/'
}

To Reproduce

import { optimize } from 'svgo'

// call optimize() anywhere during server side rendering

Versions

  • SVGO Version 4.0.0
  • Node.js Version 22.x (on Vercel)

Explanation

  • css-tree uses a dynamic require('../data/patch.json') at runtime
  • The vercel bundler doesn't recognize patch.json as a dependency and therefore excludes it from the bundled output (probably due to esm/commonjs incompatability)

Solution
Add to next.config.mjs:

  serverExternalPackages: ['svgo'],

This will add ~1MB to your final server function build size, but it's the only solution I've found so far that works.

Metadata

Metadata

Assignees

No one assigned

    Labels

    upstreamCaused by upstream dependency. Could contribute, document error & workaround, or write Yarn patch.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions