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

import.meta.url error in .mts files #49842

Closed
AlexXanderGrib opened this issue Jul 9, 2022 · 12 comments · Fixed by microsoft/TypeScript-Website#2533
Closed

import.meta.url error in .mts files #49842

AlexXanderGrib opened this issue Jul 9, 2022 · 12 comments · Fixed by microsoft/TypeScript-Website#2533
Assignees
Labels
Bug A bug in TypeScript Website Deprecated - please forward issue to microsoft/TypeScript-Website

Comments

@AlexXanderGrib
Copy link

Bug Report

🔎 Search Terms

import, imoprt.meta, nodenext

🕗 Version & Regression Information

  • This is a crash
  • This changed between versions 4.5 and 4.6
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about ECMA Script modules in Node.JS
  • I was unable to test this on prior versions because there is nothing about it

⏯ Playground Link

4.5.5 - playground

4.7.4 (both .ts and .mts filex) - playground

💻 Code

// .mts file
import.meta.url

🙁 Actual behavior

error TS1470: The 'import.meta' meta-property is not allowed in files which will build into CommonJS output.

🙂 Expected behavior

It compiles

@Josh-Cena
Copy link
Contributor

Josh-Cena commented Jul 10, 2022

You did not set module: NodeNext in your playground. (It's funny because the module option doesn't seem to be URL-encoded)

@fatcerberus
Copy link

@Josh-Cena module=199 is in fact part of the playground URL; it looks like the same bug that causes it to select ES3 in the dropdown when it's actually targeting ESNext. Manually selecting NodeNext doesn't fix the error.

@Josh-Cena
Copy link
Contributor

Looks like a playground bug. You have to select NodeNext, then unselect it, then select it again.

Screen.Recording.2022-07-10.at.10.43.19.mov

@RyanCavanaugh RyanCavanaugh added Website Deprecated - please forward issue to microsoft/TypeScript-Website Bug A bug in TypeScript labels Jul 11, 2022
@RyanCavanaugh
Copy link
Member

Confirmed this works fine from actual tsc; it does appear to just be the website

@gajus
Copy link

gajus commented Jul 19, 2022

getting the same error with minimal typescript config:

{
  "compilerOptions": {
    "module": "NodeNext",
    "target": "es5",
    "noEmit": true,
    "types": ["cypress"]
  },
  "extends": "@tsconfig/node16/tsconfig.json",
  "include": ["vite.config.ts", "cypress.config.ts"]
}

@AlanMorel
Copy link

I am seeing this issue with these settings:

{
    "compilerOptions": {
        "strict": true,
        "module": "NodeNext",
        "moduleResolution": "Node",
        "jsx": "react-jsx",
        "esModuleInterop": true,
        "types": ["vite/client"],
    },
  }

I'm on TypeScript 4.7.4

@sagargurtu
Copy link

sagargurtu commented Jul 26, 2022

I'm facing the same issue in VSCode with following settings:

{
  "compilerOptions": {
    "target": "esnext",
    "lib": ["dom", "esnext"],
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "module": "NodeNext",
    "moduleResolution": "NodeNext"
  }
}

Following error messages are displayed by VSCode in a file with .ts extension:

// Cannot find module 'pretty-ms' or its corresponding type declarations.
import ms from "pretty-ms";

// The 'import.meta' meta-property is not allowed in files which will build into CommonJS output.ts(1470)
console.log(import.meta.url);

package.json

{
  ...
  "type": "module",
  ...
}

TypeScript version: 4.7.4

@alecmev
Copy link

alecmev commented Aug 21, 2022

Same for "module": "Node16", just in case. Looks like SourceFile.impliedNodeFormat is wrong, somehow?

https://github.com/microsoft/TypeScript/blob/38e91fb12099b526e9a2cb5ad6aacfc89d5060ca/src/compiler/checker.ts#L32468

@okbel
Copy link

okbel commented Sep 10, 2022

Can confirm 👍

@dargmuesli
Copy link

@navya9singh I'm using the just released typescript v4.8.4 and still face this issue. I also don't see how a fix inside the ts website's source should fix an actual typescript issue. I'd vote to reopen this ticket.

@thyming
Copy link

thyming commented Jan 24, 2023

I'm also running into this issue with typescript 4.9.4. compiler options include:

  "compilerOptions": {
    "strict": true,
    "target": "ES2021",
    "module": "Node16",

@matthew-dean
Copy link

matthew-dean commented Mar 27, 2024

I'm also experiencing this in VSCode, even though "module": "NodeNext" is set, contrary to the error.

Edit: Figured out a fix!

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "moduleResolution": "bundler"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Website Deprecated - please forward issue to microsoft/TypeScript-Website
Projects
None yet
Development

Successfully merging a pull request may close this issue.