Making Angular Material compatible with Webpack and TypeScript

I am facing a challenge with my angular-typescript project managed by webpack. The issue arises when I attempt to integrate the angular-material library, and I am encountering significant difficulties.

Currently, the html portion of my code appears as follows:

<div class="container">
  <my-app></my-app>
</div>

<script>
  require("zone.js");
</script>
<script src="../build/common.js"></script>
<script src="../build/angular2.js"></script>
<script src="../build/app.js"></script>

My initial hurdle was that no matter where I placed the <script> tags pointing to angular-aria, animate, and material, Chrome consistently displayed the error:

Cannot read property 'module' of undefined angular-aria.js:57 etc.

for all three angular-material libraries. I then suspected that Angular was loaded at a later stage, which hindered the proper functioning of the material libraries. In an attempt to resolve this, I wrote a function that would execute on

<body onload="loadAngMat()">
, invoking require() on each of the angular-material libraries, and initially, it seemed to work.

Subsequently, I created a simple <md-input-container> in the template of one of my Angular components. However, I was unable to determine the URL from which to load the ngMaterial Module within my app's namespace. The structure, following a standard node install for installing the material libraries, is somewhat condensed as illustrated below:

> app
    > app.module.ts
    > index.html
    > index.js
    > etc.
> build
    > built project
> node_modules
    > angular-material
        > index.js
    > angular-aria
        > angular-aria.js
    > angular-animate
        > angular-animate.js
    > other angular and node modules

To incorporate the library into my Angular project, I assumed that I could simply employ:

import { ngMaterial } from 'angular-material';

Unfortunately, this was unsuccessful, and Chrome now returns the error message:

Template parse errors: md-input-container is not a known element.

Therefore, my query is how can I effectively load all the libraries and have app.module.ts successfully import ngMaterial?

Below is my webpack.config:

var path = require('path');
var webpack = require('webpack');
var CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin;

module.exports = {
  devtool: 'source-map',
  debug: true,

  entry: {
    'angular2': [
      'rxjs',
      'reflect-metadata',
      '@angular/core'
    ],
    'app': './app/app.module',
  },

  output: {
    path: __dirname + '/build/',
    publicPath: 'build/',
    filename: '[name].js',
    sourceMapFilename: '[name].js.map',
    chunkFilename: '[id].chunk.js'
  },

  resolve: {
    extensions: ['','.ts','.js','.json', '.css', '.html']
  },

  module: {
    loaders: [
      {
        test: /\.ts$/,
        loader: 'ts',
        exclude: [ /node_modules/ ]
      }
    ]
  },

  plugins: [
    new CommonsChunkPlugin({ name: 'angular2', filename: 'angular2.js', minChunks: Infinity }),
    new CommonsChunkPlugin({ name: 'common',   filename: 'common.js' })
  ],
  target:'node-webkit'
};

It is worth mentioning that I attempted to load the material libraries using this configuration file, which resulted in a

a dependency to an entry point is not allowed
error upon building the project.

Answer №1

i have reviewed your code

import { ngMaterial } from 'angular-material';

It seems that you are utilizing angular-material, but please note that this repository is intended for Angular Material v1.x and not for angular 2.

For Angular 2, there is a npm package available called @angular2-material, which should be utilized instead.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with updating initial state that is null in Redux Toolkit

Encountered an issue while using Redux Toolkit with Redux Persist. Unable to update the initial state of a user if it's null. The code consistently assigns null to the store regardless of passing parameters. import { createSlice, PayloadAction } from ...

Bring in jspm libraries to your project via typescript

While researching how to import jspm packages into typescript, I noticed that most examples assumed the use of SystemJS for loading and interpreting them in the browser. However, I prefer using tsc to compile commonjs modules and only import the js code, a ...

Having trouble with [at-loader] while incorporating Typescript and react-bootstrap in webpack?

While working with webpack, I encountered an error message when using a component in react-bootstrap along with typescript. The error displayed is as follows: ERROR in [at-loader] ./node_modules/react-bootstrap/esm/NavbarCollapse.d.ts:4:18 TS2320: ...

Unit testing in Angular: Testing HTTP functionality with live APIs

I am currently working on writing unit tests in Angular CLI using Karma. I'm trying to test HTTP calls with a real API instead of using mocks because the API models tend to change frequently. I want to ensure that my endpoint responses are accurate. I ...

What could be the root of this next.js build issue occurring on the Vercel platform?

I recently upgraded my next.js project to version 12.0.7, along with Typescript (4.5.4) and pdfjs-dist (2.11.228), among other libraries. Locally, everything runs smoothly with the commands yarn dev for development and yarn build for building. However, af ...

Customizing the Position of Material UI Select in a Theme Override

I'm trying to customize the position of the dropdown menu for select fields in my theme without having to implement it individually on each select element. Here's what I've attempted: createMuiTheme({ overrides: { MuiSelect: { ...

Error: Unable to locate module with associated type definitions when utilizing Typescript in Next.js

Currently, I am working on a next.js project that I'm attempting to integrate typescript into. The structure of my folders is organized as follows: api aggregation.ts interfaces index.ts components Component1 index.js index.module.css ...

ES6 import of CSS file results in string output instead of object

Too long; Didn't read I'm facing an issue where the css file I import into a typescript module resolves to a string instead of an object. Can someone help me understand why this is happening? For Instance // preview.ts import test from './ ...

TimeStamp Recorder - Typescript

I'm trying to create a timer that counts the time when a button is pressed. Currently, I have managed to display the minutes and seconds on the screen as soon as the button is clicked. For example: 21(min):02(sec) What I am struggling with is updati ...

The creation of a Firebase user account with the specified email and password

I'm currently facing an issue while creating a new user with email and password using firebase authentication. The problem arises when I try to access the displayName from the returned async call before the updateProfile function, as it returns a null ...

The TypeScript error message is saying that it cannot find the property 'push' of an undefined value, resulting in a error within the

Issue: Unable to access property 'push' of undefined in [null]. class B implements OnInit { messageArr: string[]; ngOnInit() { for(let i=0; i<5; i++) { this.messageArr.push("bbb"); } } } ...

Angular 2 Routing 3.0: Paying Attention to Letter Case

let routesList: Routes = [ { path: 'x', component: xComponent }, { path: 'y', component: yComponent }, { path: 'zComponent', component: zComponent } ]; When entering "x" in the URL, it navigates to the component page. Ho ...

Angular allows users to click on elements that have been dynamically added to the Document Object Model (DOM), such

Incorporating the ngx-bootstrap datepicker into my project was a game changer. I have a submit button positioned below the datepicker, and I have a specific goal in mind. I want the submit button to become active as soon as I select a date from the datepic ...

The version of Angular CLI on my local machine is newer than the global

I have been trying to set up a new Angular project using Angular CLI version 13.3.10. However, every time I create a new project, it ends up being in version 13.3.11. Prior to creating this project, I followed the steps of running npm uninstall -g @angula ...

Is it possible to set up a universal type definition in TypeScript version 2 and above?

I have a collection of straightforward .ts files, not part of any projects but standalone .ts scripts. They implement certain node.js features. Both TypeScript and node type definitions are set up through the following commands: npm install -g typescript ...

The communication between Angular and Unity using SignalR for messaging is not functioning properly, as I am unable to

Trying to establish a connection between Angular and Unity has been challenging for me. I can't seem to get them to communicate with each other. My goal is to have Angular "announce" when someone enters a room, and have Unity "greet" the user enterin ...

Navigating through segment tabs on Ionic 3 with a simple swipe

In the code snippet provided, segments from Ionic 3 are used with ngSwitch and ngModel. However, I am looking for a way to switch between segment tabs by simply swiping on the content area, rather than tapping the tabs at the top. Is there a way to achieve ...

How can the outcome of the useQuery be integrated with the defaultValues in the useForm function?

Hey there amazing developers! I need some help with a query. When using useQuery, the imported values can be undefined which makes it tricky to apply them as defaultValues. Does anyone have a good solution for this? Maybe something like this would work. ...

Issue encountered when working with interface and Observable during the http parsing process

Visual Studio File Structure Error app(folder) -->employee-list(folder) -->employee-list.component.html -->employee-list.component.ts -->app.component.html -->app.component.ts -->app.module.ts -->employee.json ...

How to retrieve the data from a PHP file using Angular 4 CLI?

Is there a way to retrieve the response from a PHP file using Angular 4? If the PHP file is placed in the assets folder, the GET request will identify the file and proceed to download its content. For example: headers: Headers ok: true status: 200 status ...