Ben Racicot
Ben Racicot

Reputation: 5971

Module has been externalized for browser compatibility error

In my Angular app I'm getting an error when using console.log();

Error: Module "console" has been externalized for browser compatibility. Cannot access "console.log" in client code. See https://vitejs.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.

After researching the links I cannot see how console.log applies to node modules. Any insight would be very much appreciated.

Upvotes: 2

Views: 2670

Answers (1)

Charlie
Charlie

Reputation: 23838

The console polyfill is available via vite-plugin-node-polyfills package.

Install via npm and use in the config

import { defineConfig } from 'vite'
import { nodePolyfills } from 'vite-plugin-node-polyfills'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    nodePolyfills(),
  ],
})

This package provides the following polyfills.

[
  '_stream_duplex',
  '_stream_passthrough',
  '_stream_readable',
  '_stream_transform',
  '_stream_writable',
  'assert',
  'buffer',
  'child_process',
  'cluster',
  'console',
  'constants',
  'crypto',
  'dgram',
  'dns',
  'domain',
  'events',
  'fs',
  'http',
  'http2',
  'https',
  'module',
  'net',
  'os',
  'path',
  'process',
  'punycode',
  'querystring',
  'readline',
  'repl',
  'stream',
  'string_decoder',
  'sys',
  'timers',
  'timers/promises',
  'tls',
  'tty',
  'url',
  'util',
  'vm',
  'zlib',
]

Upvotes: 5

Related Questions