Skip to content

Self-reference / import of the current package doesn't work in an mjs file, but does in a ts file in src #50270

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

Closed
jakebailey opened this issue Aug 11, 2022 · 10 comments
Assignees
Labels
Duplicate An existing issue was already created Needs Investigation This issue needs a team member to investigate its status.

Comments

@jakebailey
Copy link
Member

jakebailey commented Aug 11, 2022

Bug Report

node16 nodenext esm mjs self reference referential module package name

🔎 Search Terms

🕗 Version & Regression Information

  • This is the behavior in every version I tried

💻 Code

Repro here: https://github.com/jakebailey/self-reference-mjs

In short, I have a mjs file with:

// @ts-check

import * as index from "self-reference-mjs";

console.log(index.foo);

And a package.json like:

  "name": "self-reference-mjs",
  "version": "1.0.0",
  "type": "module",
  "exports": {
    ".": "./dist/index.js"
  },

This code executes and does the right thing, but TS gives an error on the import. Writing the same code in a TS file works, but the emitted code is identical to the hand-written code.

🙁 Actual behavior

I can import the current package by name in an mjs file.

🙂 Expected behavior

I can only import the current package in TS, not in an mjs file.

I sort of expected this to work post #46762 or something, but the behavior is inconsistent.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Aug 11, 2022
@jakebailey
Copy link
Member Author

@andrewbranch is this just "not a bug", then, per #50390?

I guess I'm surprised, as I had assumed we'd recognize that mjs can do this, but...

@andrewbranch
Copy link
Member

This is totally separate from #50390—that repro was not showing what it looked like it was showing. It was just misconfigured.

@sheetalkamat
Copy link
Member

This seems configuration error.. tsc --explainFiles shows that it does not include b.mjs so you arent seeing errors.. The inferred project has which is why i think you have errors?

          "module": "ESNext",
          "moduleResolution": "Node",

@jakebailey
Copy link
Member Author

Yes, I am opening this up in VS Code, so I suppose this is the default project. I guess I would have expected that "type": "module" would be enough information, but maybe that isn't the case because there's nothing that indicates that it's a node project?

@andrewbranch
Copy link
Member

Then the question is, why isn’t b.mjs picking up tsconfig.json and opening a ConfiguredProject?

@andrewbranch
Copy link
Member

Oh wait, you don’t have allowJs turned on.

I also don’t know if .[cm][tj]s files are in the default include glob? That may be something to look at changing under nodenext if they’re not.

@jakebailey
Copy link
Member Author

Oh wait, you don't have allowJs turned on.

Adding allowJs does work, yeah (so likely no glob changes needed), but I have it turned off because I don't want this file to appear in my dist output; in my case it's effectively a build script (like a gulpfile).

@jakebailey
Copy link
Member Author

Specifically, once I enable the option and open the file, about 10 duplicate errors that say "cannot write file because it would overwrite input file" appear in my tsconfig.json.

So, maybe it's not trying to write to dist, and I just don't know how to configure my project. 😅

@andrewbranch
Copy link
Member

In that case, I would call this a duplicate of #49270 (I could update the title to include .mjs variants as well)

@jakebailey
Copy link
Member Author

Sounds good to me.

@jakebailey jakebailey added the Duplicate An existing issue was already created label Sep 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

4 participants