Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Externals defined in webpack.config still getting error module not found

I have defined externals in webpack.config for material-ui

module.exports = [{
  entry: ...
  output:...
  externals: {
    react: {
      commonjs: "react",
      commonjs2: "react"
    },
    "material-ui": {
      commonjs: "material-ui",
      commonjs2: "material-ui"
    }
  },
  module: ...
}];

Still its giving error like-

Cannot resolve module 'material-ui/IconButton'......

In my entry js file, I have

import React, {Component} from "react";
import IconButton from "material-ui/IconButton";
.....
.....
like image 775
VISHAL DAGA Avatar asked Jan 24 '17 08:01

VISHAL DAGA


People also ask

What is externals in webpack config?

The Webpack Externals are used to ignore the packages from the application while bundling and adding them as external CDN script references.

How do webpack externals work?

Webpack externals tell Webpack to exclude a certain import from the bundle. Often externals are used to exclude imports that will be loaded via CDN. For example, suppose you are implementing server-side rendering with Vue and Express, but your client-side code imports Vue via a CDN.

Does webpack include node_modules?

webpack does not include node_modules dependency!!


1 Answers

Ok I solved it. External expects complete path.

So either,

import {IconButton} from "material-ui"

or

externals: {
  "material-ui/IconButton": {
    commonjs: "material-ui/IconButton",
    ...
  }
}

will work. Ofcourse, second option is not reasonable here

like image 54
VISHAL DAGA Avatar answered Sep 24 '22 15:09

VISHAL DAGA