Encountering an error when using AngularJS 1.7 with ES6 and webpack: Uncaught TypeError - Property 'module' is undefined

I am currently in the process of setting up the AngularJS app skeleton. Utilizing ES6 with webpack for bundling, I have encountered an error that has been difficult to resolve. The specific error message reads as follows:

Uncaught TypeError: Cannot read property 'module' of undefined

The error seems to be originating from the following file:

import { angular } from 'angular';
import { HomeController } from './app/appName.home';

angular.module('appName', [])
    .controller('HomeController', HomeController);

angular.element(() => {
    angular.bootstrap(document, ['appName']);
}); 

The line triggering the error is:

angular.module('appName', [])

To address this issue, I am using babel to compile ES6 to ES5 and implementing a webpack configuration to bundle the application and Angular files:

module.exports = {
    devtool: 'source-map',
    entry: './src/main.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'app.bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: "babel-loader",
                query: {
                        presets: ['@babel/preset-env']
                }
            }
        ]
    },
};

An image displaying the error can be seen below.

Uncaught ReferenceError: angular is not defined

In my troubleshooting efforts using Chrome debugger, the angular object with the module function does appear to be in scope.

Module function available as property of angular in scope

Edit: Upon further research, I came across a potential solution suggested here. It's worth noting that while importing the script file in the HTML may seem unnecessary since webpack should handle bundling the Angular file from node_modules, there could still be a timing issue where Angular loads later in the bundle causing the problem. Unfortunately, more details on this specific scenario are still eluding me.

Answer №1

I found a solution by importing jQuery and Angular in the following way:

import jQuery from 'jquery'
import 'angular'

Hopefully, this will assist someone else.

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

Updating the database table using javascript

I am looking to dynamically update my database based on a condition in JavaScript. Currently, I have been using the following approach: <a href="#" onclick="hello()">Update me</a> <script> function hello(smthing) { if(smthing == true) ...

Check for the presence of Jquery using vanilla JavaScript, and if it isn't found, load it dynamically

Can anyone help me with detecting the presence of JQuery using regular JavaScript? If it's not present, I want to load the JQuery file from Google or any other website. Here are two solutions that are known to work: Solution from Claudio Redi windo ...

Exploring JSON data with JQuery: A comprehensive guide to looping through with each

I am currently facing an issue with looping through cached JSON data obtained from an Ajax call. Specifically, I keep receiving the error message 'Cannot read property 'title' of undefined'. Can someone provide assistance? $.each(cach ...

Is there a method in the dir-pagination directive for angularjs that allows me to retrieve all the records from the current page in pagination?

Is there a method to retrieve all records on the current page of the dir-pagination directive by @michaelbromley? If you need assistance, you can refer to this link: dir-pagination, where I am looking to extract a collection of 5 records ranging from 100 ...

Attitude: Defiant and Ignoring Authority

So far, no suggestions have been made, indicating that maybe I haven't properly summarized the issue; The problem arises when I absolutely position the section with the class="container" using an additional class or id specific to that <div>. I ...

Utilizing AngularJs to connect server-generated HTML content to an iframe

My Angular app functions as an HTML editor that transmits the template to a server for rendering with dynamic data. The rendered content is then sent back to the client, where it needs to be placed inside an iframe for preview purposes. It appears that ng- ...

What is the best way to transfer information from sqlite3 to jade?

Hey, I have a question that might sound silly to some but I'm just getting started with node.js, jade, and sqlite3. I'm trying to figure out how to pass database data from a JavaScript script to a jade template in order to populate a table. rou ...

Tips for locating precise information within nested object formations using Javascript

Within my code, I have showcased two distinct types of response. Upon closer examination of the following code snippets, it becomes evident that the structure of the response from a service differs slightly between the two types. In the first type, there i ...

Ways to convert a PHP array into a JavaScript array

I have a 3D array in php when using var_dump($arr) it looks like this array(2) { [0]=> array(4) { [0]=> array(2) { [0]=> string(7) "36.3636" [1]=> int(8) } [1]=> array(2) { [0]=> string(7) "27.2727" [1]=> int(5) } [2]=> a ...

Using a self-invoking function in JavaScript with addEventListener

I'm struggling to get an Event Listener to self invoke a function and work correctly. Although the following code runs the function, the Event Listener is not functioning as expected: window.addEventListener("resize", (function () { document.getElem ...

When I apply properties in JavaScript, I find that I am unable to utilize the CSS "hover" feature

For a personal project I am currently working on, I decided to experiment with setting background colors using Javascript. However, I have encountered an issue where my CSS code that changes the hover property does not seem to work after implementing the J ...

What is the process for isolating characters from a variable?

Is there a way to extract the first character up to 1 character after the decimal point? I am receiving data from an API {POWER : 1343.45} {POWER : 15623.34577} {POWER : 123.434} I only require 123.4 from 123.45, 15623.3 from 15623.34577, etc. How can I ...

Date selection tool in Bootstrap showing blank field

I am currently working on implementing a bootstrap datepicker to update layer dates on my website. However, I am facing an issue where the calendar dropdown does not appear when I click on the datepicker box and instead it just shows an empty box. My goal ...

Having trouble assigning more than one custom named property in TypeScript for MUI v5 Palette

I am currently setting up multiple custom attributes to make future updates easier. Nonetheless, I'm encountering a challenge with implementing more than one custom property in MUI v5. TypeScript Error TS2717: Subsequent property declarations must hav ...

Protractor's direct entryway to the factory

Greetings, I am the owner of a small factory called myFactory in my application: .factory('myFactory', ['$q', function ($q) { function myMethod() { ..... } return { myMethod: myMethod }; }]); ...

Tips on obtaining accurate server-side stack trace line numbers within Next.js

Our team is currently working with relatively straightforward Next.js applications and has noticed that the line numbers displayed in stack traces on the console are incorrect when issues arise in our code. For instance, let's consider the beginner c ...

A JavaScript popup displaying selectable options and a callback function

Is there a feature in Javascript that is similar to Java's alert builder? I need a button that, when clicked, will display an alert with various options. When an option is selected, it should trigger a callback function passing the chosen option along ...

Failure to trigger the before-prepareJSApp hook in nativescript-dev-webpack

I am currently facing some challenges while trying to bundle my Nativescript app using webpack. The first issue arises when I run the following command: tns build android --bundle After running this command, it seems like webpack is not doing anything. ...

Angular JS is throwing an error because it cannot recognize the property 'push' of undefined

Would like to automatically update the div using $scope.push encountering an issue: Error: Cannot read property 'push' of undefined Here are my JSON and JavaScript snippets: JSON {"records":[{"total":"156000"}]} JavaScript $scope.plusCar ...

Decoding multiple data with jQuery on the server-side

Scenario: A situation arises where I am utilizing a jQuery.ajax call to send three arrays to the server for database storage. The challenge lies in decoding the combined data object on the server side and breaking it back into the original three arrays. ...