Dismiss the necessity of imports in Angular

I am facing an issue with using a library in Angular, specifically the cubing npm package. This library is designed to run in both the browser and node environments, with specific code for each. I want it to run in the browser, but when compiling with Angular, it fails to find worker_threads. The library owner suggested ignoring this import as it's only needed for the node variant of the code.

However, I'm struggling to figure out how to make Angular ignore this import from the node module. How can I instruct Angular to disregard this import since we won't be using the code that requires it?

Error: Can't resolve 'worker_threads' in REDACTED/node_modules/cubing/dist/esm

If there's no direct solution, I might have to resort to patching the node_modules, but I'm hesitant about doing so due to potential challenges in getting it to work in production environments.

For additional context, here is the GitHub project (switch to branch scrambles for the issue at hand): https://github.com/Lykos/cube_trainer.git

Below are the most relevant files:

Package.json:

{
  "name": "cube-trainer",
  "private": true,
  "author": "Bernhard F. Brodowsky",
  "license": "MIT",
  "repository": {
    "type": "git",
    "url": "https://github.com/Lykos/cube_trainer.git"
  },
  "engines": {
    "node": "16.x",
    "npm": "8.x"
  },
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build --configuration production",
    "build_development": "ng build --configuration development",
    "watch": "ng build --watch --configuration development",
    "test": "ng test",
    "test_ci": "ng test --karma-config client/karma-ci.conf.js",
    "lint": "ng lint"
  },
  ...
}

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  ...
}

tsconfig.json

{
  ...
}

tsconfig.app.json

{
  ...
}

Answer №1

It appears that you have the option to exclude it in either the tsconfig or in the angular.json file, maybe consider trying both methods?

Exclude files from build in Angular 2

// tsconfig
{
   "exclude": [
       "node_modules/thepath/tothedependency/orfile/using/worker_threads/I_guess",
   ]
}
// angular.json
...
          "configurations": {
            "production": {
              "assets": [
                {
                  "ignore": [
                    "**/test.json",
                    "**/test"
                  ],
                },
...

Something similar could be done as well

You can also implement a browser wrapper for worker_threads and then adjust the angular build / webpack to replace the old import with the new one.

If you choose to make modifications, it may not be overly difficult. Creating a fork on github and installing from your fork is relatively simple. Alternatively, saving a git patch file for the change and automatically applying that patch during installation isn't too complicated either.

This type of adjustment tends to be challenging, even in esbuild and rollup scenarios.

You could also tweak your webpack configuration, which I believe ng build utilizes internally, by likely locating it in

/node_modules/angular/somejunk/config/webpack.config.json
and adding this:

Webpack ignore import

module.exports = {
  //...
  externals: {
    fetch: 'cross-fetch'
  }
};

However, this brings you back to applying a patch or utilizing a git branch

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

Any idea how to resolve this typescript typing issue: "The argument, either a string or number, cannot be assigned to the parameter of type 'SetStateAction<string>'"?

Just recently delving into TypeScript, I've encountered a persistent typing problem that has proven challenging to resolve despite numerous attempts. The error message causing me trouble reads as follows: Argument of type 'string | number' ...

Restrict the parameter type using a type predicate

How can I effectively narrow types based on the value of a single field in TypeScript? It seems that using type predicates may not be working as expected to narrow down the types of other parameters within a type. Is there a way to ensure correct type na ...

Why is the data from an Angular service returning as undefined when I call it from a component?

Currently, I'm utilizing the JSONPlace holder Fake API to retrieve data. Despite successfully fetching the data in my service, when I attempt to call that service from my app component, I am encountering an issue where it returns undefined. What could ...

typescript max recursion depth restricted to 9 levels

After countless attempts, I finally managed to create a generic type that provides me with all possible combinations of JSON key lists and values. Additionally, I have developed a method to limit the recursion within this type. type EditAction<T,P exten ...

Bring in a function by its name from the ts-nameof package that is not declared in the d.ts export

Recently, I came across a captivating package that caught my interest and I would love to incorporate it into my TypeScript application: https://github.com/dsherret/ts-nameof However, upon attempting to import the nameof function, I realized it was not be ...

The resolved link in the package-lock.json file is directing to a local artifactory URL rather than the globally specified registry

My .npmrc file is configured as follows: registy=http://artifacts.sample.com/artifactory/api/npm/ The package-lock.json file contains resolved field for dependencies which looks like this: "dependencies": { "acorn": { "version": "5.7.3", ...

Sending various arguments within a single post request in Angular 6 and Node.js

I am currently developing an app in Angular 6 using NodeJS + Mongoose. I have two parameters that need to be sent to the backend with a single POST request. My query is, is it possible to include both parameters in one POST Request? Thank you The paramet ...

Encountered difficulties when compiling the Angular application

When attempting to compile my Angular application, I encounter the following issue: Failed to compile. ./node_modules/@angular/material/esm5/core.es5.js Module not found: Error: Can't resolve '@angular/cdk/a11y' in '/media/DATOS/CBA ...

Is it possible to write TypeScript and execute it directly with Node?

I am attempting to write some basic typescripts but I am encountering an issue with the setup below: node src/getExchangeAndTickerList.ts import * as mkdirp from 'mkdirp'; ^^^^^^ SyntaxError: Cannot use import statement outside a module ...

No pathways can be established within Core UI Angular

I've been attempting to use the router link attribute to redirect to a new page, but instead of landing on the expected page, I keep getting redirected to the dashboard. Below is an overview of how my project's structure looks: [![enter image de ...

React-scripts is not recognizing tests that have the .tsx file extension

Currently in the process of converting my project to TypeScript, everything is almost working perfectly. The code builds without issues and renders correctly. The only hiccup I'm facing is with my tests. I've observed that when I change a test f ...

Expanding IntelliSense and helpful tooltips in VSCode for JavaScript and TypeScript by utilizing Node.js for a deeper understanding

As a beginner in programming, specifically in JS/TS, I've been experimenting with node.js and have encountered a puzzling issue with the IntelliSense or 'helptext' feature in VSCode. For instance, when attempting to use fs.open(), I receive ...

`npm security check reveals a potential vulnerability related to Inefficient Regular Expression Complexity. For more information, refer to: https://github.com/advisories/GHSA-9vvw-cc

# npm audit report debug <3.1.0 debug Inefficient Regular Expression Complexity vulnerability - https://github.com/advisories/GHSA-9vvw-cc9w-f27h No solution currently available node_modules/body-parser/node_modules/debug node_modules/express/node_modu ...

The HTML code is not displaying the value from the array

How can I use interpolation to display a response from an API? I have attempted the following: response['data'][0].value// can console this value here this.variable = response; <p>{{ variable['data'][0].value }}</p> T ...

Typescript disregarding conditional statements

Looking for some assistance with a Next.JS React-Typescript application Here's my code snippet for handling the video HTML element const videoRef = useRef<HTMLVideoElement>(); useEffect(() => { videoRef !== undefined ? videoRef.current. ...

Attempting to implement a typeguard in Typescript that relies on the presence of specific content within an element

Currently, I am attempting to develop a Typescript conditional that verifies if a particular word is already present in the name. The function in question is as follows: isOrganic() { for (let i = 0; i < this.items.length; i++) { if(this.ite ...

An issue occurred: Unable to access the 'login' property because of a TypeError

Setting up a login page and declaring an object in my login.ts file. public User: { login:"", senha:"", }; Utilizing [ngModel] to save values within the parameters of the object. <ion-item> <ion-label floating>Enter ...

Angular is set up to showcase every single image that is stored within an array

I am having trouble displaying the images from the "image_url" array using a for loop. The images are not showing up as expected. Here is the content of the array: image_url: [ 0: "https://xyz/16183424594601618342458.5021539.jpg" 1: "https://xyz/1618342459 ...

Please enter data into the input fields provided in the text

Below is the code where Google transliteration is used for typing in Indian language in a text field. There are two routes with different page IDs. Initially, the transliteration works fine on the default page. However, when changing routes, an error occur ...

I am encountering a problem with HttpClient Angular POST when trying to communicate with Google FCM Server. How can I

I have encountered an issue while trying to send FCM messages using Angular HttpRequest, even though I am able to do so successfully via a POST and HTTP v1 Firebase API through Postman: Error Below are the imports I am using: import { Injectable } from & ...