Webpack cannot find the specified module extension

I am facing a challenge with my project that involves both express and Angular. In this discussion, I will focus only on the express side.

https://i.sstatic.net/C1blt.png

When importing custom modules on the server side, I use the following syntax:

import { PayPal } from "@helpers/paypal";
import { Database } from "@models/database";

I have set up a baseUrl and paths in tsconfig.json as follows:

"baseUrl": ".", 
    "paths": {
      "@helpers/*": [
        "helpers/*"
      ],
      "@models/*": [
        "models/*"
      ]
    }

While TypeScript (tsc) runs smoothly, webpack encounters an error:

Error: Cannot find module '@helpers/paypal'

To resolve this issue, documentation suggests adding an alias to webpack.config file like this:

   module.exports = {
  "resolve": {
    "extensions": [
      ".ts",
      ".js",
      ".json"
    ],
    alias: {
       "@helpers/*": path.resolve(path.join(__dirname, 'server', 'helpers')),
    },
    "modules": [
      "./node_modules"
    ],
    ...etc

I have experimented with different path configurations, but none have been successful so far.

ADDITIONAL INFORMATION ON FOLDER STRUCTURE IMAGE

The actual path of the helpers folder is: /server/helpers

The import statement that works in TypeScript is: import { PayPal } from "@helpers/paypal";

Answer №1

When setting up Webpack aliases, they do not use globs. Instead, when an alias key is matched, it will be replaced with the assigned value. For example, if you have a directory named server/helpers, you can create an alias @helpers for it. Then, whenever you import something like @helpers/paypal, it will translate to

/absolute/path/to/server/helpers/paypal
.

alias: {
  "@helpers": path.resolve(__dirname, "server", "helpers")
}

Using aliases in this way is a common practice, and the documentation for resolve.alias provides more details.

Here's an excerpt from the table on resolve.alias:

| alias:                | import "xyz"       | import "xyz/file.js"          |
|-----------------------|--------------------|-------------------------------|
| { xyz: "/some/dir" }  | /some/dir/index.js | /some/dir/file.js             |
| { xyz$: "/some/dir" } | /some/dir/index.js | /abc/node_modules/xyz/file.js |
| { xyz: "./dir" }      | /abc/dir/index.js  | /abc/dir/file.js              |

Answer №2

The file you're referencing is called payPal, however, you are importing paypal with a different capitalization.

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

Implementing an All-Routes-Except-One CanActivate guard in Angular2

In order to group routes within a module, I am aware that it can be done like this: canActivate: [AuthGuard], children: [ { path: '', children: [ { path: 'crises', component: ManageCrisesComponent }, ...

Utilizing the polymer paper-dialog component in an Angular 2 TypeScript application

I have imported the paper-dialog from bower, but I am facing an issue with showing the dialog using the open() method. app.component.html <paper-icon-button icon="social:person-outline" data-dialog="dialog" id="sing_in_dialog" (click)="clickHandler()" ...

The Typescript intellisense feature in VS Code seems to be malfunctioning

While setting up typings for my Node server, the intellisense suddenly stopped working. I checked my tsconfig.json file: { "version": "0.1.0", "command": "tsc", "isShellCommand": true, "args": ["-p", "."], "showOutput": "silent", " ...

Could Typescript decorators be used as mixins?

In the process of developing a complex Angular2 application, I have created a base class that serves as the foundation for my components: export abstract class ReactiveComponent implements OnInit, OnDestroy, AfterViewInit { abstract ngOnInit(): void; ...

Alerts appear immediately upon beginning to type, asking for 8 characters and ensuring both passwords match

Is it possible to notify users that both passwords should match and they need to enter at least 8 characters after typing? There is currently an issue where a notification appears for entering less than 8 characters, but the password reset still proceeds ...

Creating a custom button for exporting a high chart to CSV

My Angular project involves exporting a chart to various formats, such as png, jpeg, pdf, and SVG. However, I am encountering an issue when trying to export the chart as CSV or . I have attempted the following code: this.lineChart.chart.downloadCSV(); //F ...

Angular - Automatically filling in an empty input field upon dropdown selection

My goal is to create a DropdownBox that will automatically fill input fields based on the selected value. For example, selecting "Arnold" from the dropdown will populate another textbox with "Laptop". How can I accomplish this? { name:'Arnold', i ...

Typescript interface created specifically for React Higher Order Component Props

Consider the React HOC provided below which adds sorting state to a component: import React, {Component, ComponentClass, ComponentType} from 'react' interface WithSortState { sortOrder: string } interface WithSortInjectedProps { sortO ...

Using a pipe in multiple modules can lead to either NG6007 (having the same pipe declaration in more than one NgModule) or NG6002 (failing to resolve the pipe to an NgModule

My Pipe is located in apps\administrator\src\app\modules\messenger\pipes\custom-message.pipe.ts import { Pipe, PipeTransform } from '@angular/core'; /** * Pipe for Custom Message for boolean */ @Pipe({ name ...

What is the best way to dynamically implement text ellipsis using CSS in conjunction with Angular 7?

i need to display a list of cards in a component, each card has a description coming from the server. In my component.html, I am using a ngFor like this: <div [style.background-image]="'url('+row.companyId?.coverUrl+')'" class="img- ...

Issue arising from JQuery libraries when utilizing Webpack

Having an issue with my form renderer JS script. When making an AJAX request to retrieve it, it applies functions and properties to jQuery for rendering a form. However, due to the use of webpack with another framework, there are two jQuery contexts causin ...

Updating the default value for react-i18next (stored in localstorage)

When utilizing a backend and language detector, an issue arises: The localstorage contains the key I18nextLng with the value en-US. How can we set the default value to be I18nextLng with the value En? init and other settings are as per the default docume ...

Error NG8001: The element 'router-outlet' is not recognized: Make sure that 'router-outlet' is a component in Angular, and confirm that it is included in this module

Having trouble running ng serve or ng build due to an error message stating that 'router-outlet' is not a recognized element. The Angular application structure looks like this: app.module.ts: import { NgModule } from '@angular/core'; i ...

Struggling with "Content" not being recognized in Typescript PouchDB transpilation errors?

I have been diligently working on an Ionic app for the past three months with no major issues during development or deployment to mobile devices. However, yesterday I encountered a frustrating NPM dependency problem while trying to deploy to mobile. In an ...

Tips for managing errors when utilizing pipe and mergemap in Angular

In the code snippet provided, two APIs are being called. If there is an error in the first API call, I want to prevent the second API call from being made. Can you suggest a way to handle errors in this scenario? this.userService.signUp(this.signUpForm.v ...

Issues arising when routing ffmpeg to flac encoder

I am facing an issue with encoding a flac file with seektables. The ffmpeg's flac encoder does not include seektables, so I have to resort to using the flac Command Line Interface (CLI). My goal is to convert any arbitrary audio file into a seekable f ...

JavaScript - Imported function yields varied outcome from module

I have a utility function in my codebase that helps parse URL query parameters, and it is located within my `utils` package. Here is the code snippet for the function: export function urlQueryParamParser(params: URLSearchParams) { const output:any = {}; ...

Utilizing Webpack caching feature to store unique [hash] values within the index source code while leveraging React.js

Currently, I'm working on an isomorphic application that is entirely constructed with React – even the base html is in React. My root html is set as an app component: ... var AppTemplate = React.createClass({ displayName: 'AppTemplate&apo ...

Error code TS1005: Compiling Typescript DefinitelyTyped assets encountered an issue

I'm confident in my setup, but I can't seem to get tsc to compile. Here's the branch of my repository: https://github.com/inosion/sample-atom-typescript-package/tree/add_react Although I have the latest versions of typescript, I'm uns ...

Using Angular to make an HTTP POST request to fetch data

My trusty .net backpack has been working flawlessly. However, I encountered an issue when trying to connect it with the Angular front end. All backend requests are post requests and require passing an ApiKey in the body of each request. Interestingly, ever ...