The imported class in Angular seems to be misplaced or not properly defined,

My npm project structure includes the following:

app-dep
├── dist
│   ├── bundle.js
│   └── bundle.js.map
├── node_modules/
├── package.json
├── package-lock.json
├── src
│   ├── base-component.ts
│   ├── factory.ts
│   ├── app.ts
│   └── modules/
├── tsconfig.json
└── webpack.config.js

The App class in my app.ts file uses all .ts files inside src/

export default class App extends HTMLElement { //some-content }

I utilize webpack to create a bundle.js in the dist/ directory.


In another angular project, I include this app-dep project by running

npm install --save ../app-dep

However, when trying to use it in my angular component:

import App from 'app-renderer/dist/bundle';

ngOnInit() {
    window.customElements.define('micro-app', App);
}

An error is thrown:

'Failed to execute 'define' on 'CustomElementRegistry': parameter 2 is not of type 'Function'. When logging to console, only undefined is seen.

It appears that even basic functions cannot be imported. Can you assist me with identifying the issue?

Answer №1

It appears that the issue may stem from either the import process or the packaging method. Below, I provide detailed steps to troubleshoot this matter.

Ensure that all the classes you wish to be accessible externally are properly exported.

For TypeScript libraries,

You can compile using "tsc," pack the generated code into the dist folder with "npm pack," and then install the dependency in your application with "npm install."

This task can become complex due to various module systems and bundlers; for more information on Webpack, refer to the following links:

https://webpack.js.org/guides/author-libraries/

When dealing with CSS libraries,

Execute "npm pack" in the root folder, considering processing styles with Sass beforehand and only packing the outcome.

For Angular libraries,

By default, when building a library project with Angular CLI, the code is created in /dist/mylibrary folder.

If you aim to utilize this code in another project, follow these steps:

  1. Build your library using "ng build mylibrary" (add --prod for production).
  2. Navigate to /dist/mylibrary within your library, execute "npm pack" to create a tgz package.
  3. In your application requiring the library, run "npm install " to integrate the dependency.
  4. To incorporate code from the library, use "import * from 'mylibrary'".

An alternative approach involves utilizing "npm link," establishing a connection between your node_modules and library code, elucidated here:

This method is suitable for local libraries; typically, such libraries are published on npm repositories (public or private) and remotely installed via "npm install." These instructions primarily pertain to local usage scenarios.

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

Is it possible to incorporate button classes from the <a> tag into a JQuery fade in/fade out function?

My jQuery image thumb Slider features thumb buttons that link to different div elements. When a user clicks on a thumb button, I want the associated div to smoothly fade in, replacing the previous one. The goal is to have a seamless transition between the ...

Is it possible to modify an element within a list based on the identifier of its containing div?

I have created some additional software for my project, which includes a large div with smaller divs inside. Here is an example of how it is structured: <div class="scroll-area" id="lista"> <div class="box" id="item1"> < ...

Executing a jQuery AJAX request for a second time

Upon hitting the submit button for the first time, the codes work successfully. However, upon hitting the button for the second time with correct email and password values, nothing happens and the user cannot log in. I have identified that the issue lies w ...

Using Browserify to Import CodeMirror Modes, Themes, and Addons

Has anyone attempted to utilize Code Mirror through browserify? I can't see anything on the screen, even though all the HTML tags have been generated. The code snippet is as follows: var CodeMirror = require('codemirror'); require(' ...

Is there a way to verify in AngularJS whether ng-model contains a string or a numerical value?

In my Angular application, I have written a JavaScript function that checks if the value of a text field is undefined or empty, and it is working properly. $scope.checkNumber = function(user_answer){ if(user_answer == undefined){ return false; } } My ...

What is the best method for maneuvering a vehicle along a curved path?

Is it possible to create a scenario where a car moves and rotates along a curved path, with the wheels' and car's orientations adjusting accordingly? While I can set an initial orientation for the car, how can I ensure that it follows the rest o ...

Is there a way to display the transition in a highchart series graph without the need to refresh the HTML page?

Utilizing an API, I retrieve JSON data that gets stored in a data.json file. Subsequently, a JavaScript script is employed to access this data.json file and generate highcharts based on it. Given that the JSON file undergoes constant changes with updated d ...

Utilize AngularJS to bind a variable and display an external HTML file without the need to open it in a browser

In my setup, I have two HTML Views - one is for application purposes and the other is for printing. Let's call them Application.html and PrintForm.html, respectively. Here is a snippet from Application.html: <!DOCTYPE html> <html> < ...

What is the best way to retrieve the current element during an event in a React application

Code: class Locations extends React.Component { render() { let { home, work, onChange } = this.props; return <div className="controls"> <GoogleMapLoader containerElement={ <d ...

Navigating using ViewChild in Ionic 2 Beta

I recently updated my Ionic 2 app to use Angular 2 RC1, which has been a great improvement. However, I am facing some challenges with the routing implementation. Despite following the update guide, I still encounter issues with my navigation component bein ...

Why does my script seem to be missing from this GET request?

Encountering an issue while setting up a page using npm and grunt. Request URL:http://localhost:9997/bower_components/requirejs/require.js Request Method:GET Status Code:404 Not Found The problematic html code is as follows: <script> ...

Creating a default option of "please select" on an Angular select element with a null value for validation

Within my Angular application, I am using a select element with an ngFor loop: <select formControlName="type" required> <option *ngFor="let type of typeList" [ngValue]="type.value">{{ type.caption }}</option> </select> When view ...

Exploring ways to manipulate checkboxes using jQuery for enhanced form validation

I am working on a custom validation for four fields, with one required to be filled in. However, I am facing an issue where the validation process is interfering with the functionality of my checkboxes. Once the jQuery click function runs, the checkboxes a ...

What is the best way to display digits in a sequential manner like on an LCD clock?

Creating a digital clock style number. https://i.sstatic.net/4H5Rs.jpg I want to stack numbers on top of each other, such as 123. https://i.sstatic.net/EytS7.jpg However, I am unsure how to achieve this. Thank you for your help. <div id="preformat ...

Troubleshooting display glitches on Bootstrap modals in Internet Explorer 11 and Microsoft Edge

I have been encountering rendering artifacts consistently across various versions of IE11 and Edge, on different devices with varying graphics cards and drivers, as well as different update statuses of Windows 10. The screenshots indicate some of these ar ...

"Learn how to convert an integer to a date format of hours, minutes, and seconds

I am currently working on a TypeScript code where I need to subtract two dates to calculate the time difference between them. Once I have the time difference as an integer, I want to display it in HTML in the format hh/mm/ss. For instance, if I subtract tw ...

Executing function statement

I'm currently learning about React hooks, and I have a question regarding the behavior of two function expressions within different useEffect hooks. In one case, the function expression named timerId in the first useEffect is invoked automatically wit ...

quiz stuck on last step due to xmhttp redirection problem

My learning project involves creating a PHP quiz with AJAX, which I am currently studying on Youtube. The goal is to have question number navigation displayed on the sidebar so that when a user clicks on any specific question number, the corresponding ques ...

Rearrange the provided string in a particular manner

Looking to transform a string like '12:13:45.123 UTC Sun Oct 17 2021' into 'Sun Oct 17 2021 12:13:45.123 UTC' without calling slice twice. Is there a more elegant and efficient way to achieve this? Currently using: str.slice(18)+&apo ...

What is the best way to organize a data tree in JavaScript for easy parsing on the frontend?

I'm struggling with a unique tree data structure in my code. Each node is represented by an object, and I need to transfer the entire tree to the front-end. Unfortunately, JavaScript's limitation on using objects as keys prevents me from implemen ...