Skip to content

Module resolution failure when using "#/" in imports #51949

Open
@jakezatecky

Description

@jakezatecky

Version

v20.11.1

Platform

Microsoft Windows NT 10.0.22631.0 x64

Subsystem

npm

What steps will reproduce the bug?

  1. Create a minimal package.json that specifies the following in imports:
{
  "imports": {
    "#/*": "./*"
  }
}
  1. Create two files at the project root:
// file1.js
import number from '#/file2.js';
console.log(number);
// file2.js
export default 2;
  1. Execute the first script: node file1.js
  2. Observe the error:
TypeError [ERR_INVALID_MODULE_SPECIFIER]: Invalid module "#/file2.js" is not a valid internal imports specifier name

How often does it reproduce? Is there a required condition?

This happens every time a key/value pair in imports has a forward slash (/) immediately following the pound sign (#).

What is the expected behavior? Why is that the expected behavior?

I would expect that file1.js is able to resolve file2.js with the specified alias. The documentation does not specify that this is an invalid setting.

What do you see instead?

TypeError [ERR_INVALID_MODULE_SPECIFIER]: Invalid module "#/file2.js" is not a valid internal imports specifier name

Additional information

There is no indication that the above configuration would not work based on the documentation. Indeed, even WebStorm's IntelliSense resolves this alias as if it would work.

Of course, setting the alias to to "#src/*": "./*" and changing file1.js to use import number from '#src/file2.js'; would work. However, I feel that would be inelegant. Imagine we have a project structure where all the files of interest are in src/, such as the following:

└── test-project/
    ├── src/
    │   ├── components/
    │   │   └── SomeComponent.js
    │   └── pages/
    │       └── SomePage.js
    └── package.json

It would be much cleaner to be able to import these files with minimal boilerplate, such as:

import SomeComponent from '#/components/SomeComponent.js';
import SomePage from '#/pages/SomePage.js';

...as opposed to having some needless additional text or specifying each directory as an alias:

{
  "imports": {
    "#components/*": "./src/components/*",
    "#pages/*": "./src/pages/*"
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    esmIssues and PRs related to the ECMAScript Modules implementation.loadersIssues and PRs related to ES module loaders

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions