Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Webpack cannot resolve class-transformer/storage in a NestJS server bundle

ErrorModule not found: Error: Can't resolve 'class-transformer/storage'

Short answer

@nestjs/mapped-types tries two paths for the class-transformer metadata store: class-transformer/cjs/storage first, then class-transformer/storage as a fallback. Node handles the failed one at runtime, but webpack tries to resolve both at build time. Bundle the server without node_modules, or ignore the path that does not exist.

  • Applies to webpack 5, class-transformer 0.5, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    See which class-transformer version is installed and which package asks for the storage path.

    npm ls class-transformer
    grep -rn "class-transformer/storage" node_modules/@nestjs/mapped-types/dist | head
  2. 2Match what you see, then jump to its fix

    1. 1Can't resolve 'class-transformer/storage' while class-transformer is 0.3 or newerWebpack resolves a fallback require that is meant to failFix
    2. 2The error appears only in the bundled server build, not when running with plain nodeThe server bundle includes node_modules that should stay outside itFix

What does the error look like?

  • Module not found: Error: Can't resolve 'class-transformer/storage'

Cause 1 of 2

Webpack resolves a fallback require that is meant to fail

1Check

In @nestjs/mapped-types the code tries class-transformer/cjs/storage (for 0.3.x and later) and, if that throws, class-transformer/storage (for 0.3.x and earlier). At runtime only one is needed. Webpack follows both and reports the one that is missing in your version.

npm ls class-transformer @nestjs/mapped-types

2Fix

Tell webpack to skip the request that does not exist in your version.

// webpack.config.js
const webpack = require('webpack');
module.exports = {
  plugins: [
    new webpack.IgnorePlugin({ resourceRegExp: /^class-transformer\/storage$/ }),
  ],
};

3Verify

Rebuild the server bundle.

npm run build

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 2

The server bundle includes node_modules that should stay outside it

1Check

A server needs no bundled copy of its npm packages. When webpack pulls them into the output, every optional require inside them has to resolve at build time.

grep -n -E 'externals|externalsPresets|target' webpack.config.js

2Fix

Treat Node built-ins and the packages as externals, so they are loaded from node_modules at runtime. List the packages you want left out.

// webpack.config.js
module.exports = {
  target: 'node',
  externalsPresets: { node: true },
  externals: /^(@nestjs\/.*|class-transformer|class-validator)$/,
};

3Verify

Rebuild and start the output with node.

npm run build
node dist/server.js

Confirmed by webpack.js.org

Did this fix it?

How do I stop it coming back?

  • Keep the server build separate from the browser build, with its own webpack config.
  • Run the built server once in CI, so a bundling error fails the pipeline and not production.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

NestJS GraphQL cannot work out the output type of a field or query

Cannot determine a GraphQL output type for the "<name>". Make sure your class is decorated with an appropriate decorator.

In code-first mode Nest reads TypeScript types through decorators, and arrays, promises and unions disappear at runtime.

2 causes, each with a fix

Back end

IntelliJ IDEA breakpoints are ignored in a NestJS app

No 'Debugger listening' line appears when the NestJS app starts

IntelliJ can only debug a NestJS app when the Node.js process runs with the inspector on and the IDE attaches to its port.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.