Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

application builder unable to resolve import #27841

Closed
1 task done
mcdenhoed opened this issue Jun 13, 2024 · 4 comments · Fixed by #27848
Closed
1 task done

application builder unable to resolve import #27841

mcdenhoed opened this issue Jun 13, 2024 · 4 comments · Fixed by #27848

Comments

@mcdenhoed
Copy link

Command

serve

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

When using the new application builder, running ng serve fails at runtime if any code from the package [email protected] is used. This same package/version works using the legacy builder.

Minimal Reproduction

Create a new angular project

ng new <whatever>
cd <whatever>

Install the following package

Use an export from this library

import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterOutlet } from '@angular/router';
import { imageSize } from "image-size";

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [CommonModule, RouterOutlet],
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'title';

  constructor() {
    imageSize(new Uint8Array())
  }
}

Exception or Error

$ ng serve 
Initial chunk files | Names         |  Raw size
polyfills.js        | polyfills     |  86.27 kB | 
main.js             | main          |  23.01 kB | 
styles.css          | styles        |  95 bytes | 

                    | Initial total | 109.37 kB

Application bundle generation complete. [1.139 seconds]

Watch mode enabled. Watching for file changes...
  ➜  Local:   http://localhost:4200/
  ➜  press h + enter to show help
✘ [ERROR] Could not resolve "./types"

    node_modules/image-size/dist/esm/index.mjs:1:22:
      1 │ export { types } from './types';
        ╵                       ~~~~~~~~~

✘ [ERROR] Could not resolve "./lookup"

    node_modules/image-size/dist/esm/index.mjs:2:50:
      2 │ export { lookup as imageSize, disableTypes } from './lookup';
        ╵                                                   ~~~~~~~~~~


### Your Environment

```text
_                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 18.0.2
Node: 21.4.0 (Unsupported)
Package Manager: npm 10.2.4
OS: darwin arm64

Angular: 18.0.1
... animations, cdk, common, compiler, compiler-cli, core, forms
... language-service, material, platform-browser
... platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1800.2
@angular-devkit/build-angular   18.0.2
@angular-devkit/core            18.0.3
@angular-devkit/schematics      18.0.3
@angular/cli                    18.0.2
@schematics/angular             18.0.2
rxjs                            7.8.1
typescript                      5.4.5
zone.js                         0.14.7

Anything else relevant?

No response

@clydin
Copy link
Member

clydin commented Jun 13, 2024

This is expected behavior. .mjs files require file extensions for all import specifiers. This is an issue with the image-size package in the prerelease 2.0 versions. The stable 1.1.1 version does not appear to have this problem.

@clydin clydin closed this as completed Jun 13, 2024
@mcdenhoed
Copy link
Author

Ahhh ok, I had worried that was the case. Thanks for the clarification!

alan-agius4 added a commit to alan-agius4/angular-cli that referenced this issue Jun 13, 2024
Previously, ESM file resolution without extensions failed when using Vite, causing issues in module loading. This commit addresses the problem by automatically resolving `.mjs` files, aligning the behavior with the application builder and ensuring consistent module resolution across different build tools.

Closes angular#27841
alan-agius4 added a commit to alan-agius4/angular-cli that referenced this issue Jun 13, 2024
Previously, ESM file resolution without extensions failed when using Vite, causing issues in module loading. This commit addresses the problem by automatically resolving `.mjs` files, aligning the behavior with the application builder and ensuring consistent module resolution across different build tools.

**NB**:  This is a workaround as valid ESM imports should always have an extension.

Closes angular#27841
@alan-agius4
Copy link
Collaborator

Note: We will add a workaround for ng serve (using Vite) to address this issue, as it is already managed when using ng build with esbuild.

Again, this is a workaround since valid ESM imports must include a file extension.

@alan-agius4 alan-agius4 reopened this Jun 13, 2024
alan-agius4 added a commit that referenced this issue Jun 13, 2024
Previously, ESM file resolution without extensions failed when using Vite, causing issues in module loading. This commit addresses the problem by automatically resolving `.mjs` files, aligning the behavior with the application builder and ensuring consistent module resolution across different build tools.

**NB**:  This is a workaround as valid ESM imports should always have an extension.

Closes #27841

(cherry picked from commit 2324d5a)
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jul 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants