Stanley Lau
Stanley Lau

Reputation: 21

Webpack's alias doesn't work when target module is in node_modules

Background

I'm writing an SSR react app with typescript, there's a client-side module I have to use (client-logger) and it can only be run on client-side since it's AMD module system. running on Node will give errors.

My current solution is to replace the client-logger module with a rewrite-client-logger module at compile time ONLY for server bundle.

I have tried Webpack's NormalModuleReplacementPlugin and alias, even write my own loader, none of them works, this is my Webpack config for the alias.

This is the demo link for repro: https://github.com/stanleyyylau/demo

Repro Steps:

Expected result:

Actual result:

Upvotes: 1

Views: 805

Answers (1)

You can fix the issue by changing your webpack-node-externals configuration to include the logger. That way the alias rule has a chance to work. Try

externals: [nodeExternals({
    whitelist: ['client-logger']
})]

Upvotes: 1

Related Questions