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

ERR_UNKNOWN_FILE_EXTENSION after adding "type": "module" to package.json #1171

Closed
realmayus opened this issue Dec 6, 2020 · 16 comments
Closed

Comments

@realmayus
Copy link

I tried to create a test file to test the structure of my typescript code.

test/index.ts

import Player from "../models/Player";

const testplayer = new Player();

When i executed it, i got this error:

import Player from "../models/Player";
^^^^^^

SyntaxError: Cannot use import statement outside a module

After a bit of research, I found this issue which suggested adding "type": "module" to the package.json. After I did this, i got the following error:

node:internal/process/esm_loader:74
    internalBinding('errors').triggerUncaughtException(
                              ^

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for XXX/test/index.ts

How can I run my file now? I am using ES6 (?) imports in the Player class: export default class Player {}

Specifications

  • ts-node version: 9.1.0
  • node version: 15.3.0
  • TypeScript version: 4.1.2
  • Operating system and version: macOS Catalina
@cspotcode
Copy link
Collaborator

Are you following the instructions from here? #1007

node forces a different style of registration to hook into the ESM loader. So if you choose to use node's ESM loader, you'll also need to use this new style of registration with ts-node. If we're not registered correctly, then we have no way of resolving and compiling your .ts files.

@cspotcode
Copy link
Collaborator

Typically you should have your tsconfig set to "module": "CommonJS" so that imports are compiled into commonjs calls. Then you don't need to mess with node's ESM loader. This is probably what you want: use "module": "CommonJS"

@realmayus let me know if this works or not.

@realmayus
Copy link
Author

Thanks, that worked 👍

@guoyunhe
Copy link

I am having the same issue:

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/guoyunhe/Git/little-yellow-duck/src/cli.ts
    at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:71:15)
    at Loader.getFormat (internal/modules/esm/loader.js:102:42)
    at Loader.getModuleJob (internal/modules/esm/loader.js:231:31)
    at Loader.import (internal/modules/esm/loader.js:165:17)
    at Object.loadESM (internal/process/esm_loader.js:68:5)

However, "module": "commonjs" doesn't work for me.

@cspotcode
Copy link
Collaborator

@guoyunhe check this out
#1171 (comment)

@buzuosheng
Copy link

I tried to create a test file to test the structure of my typescript code.

test/index.ts

import Player from "../models/Player";

const testplayer = new Player();

When i executed it, i got this error:

import Player from "../models/Player";
^^^^^^

SyntaxError: Cannot use import statement outside a module

After a bit of research, I found this issue which suggested adding "type": "module" to the package.json. After I did this, i got the following error:

node:internal/process/esm_loader:74
    internalBinding('errors').triggerUncaughtException(
                              ^

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for XXX/test/index.ts

How can I run my file now? I am using ES6 (?) imports in the Player class: export default class Player {}

Specifications

  • ts-node version: 9.1.0
  • node version: 15.3.0
  • TypeScript version: 4.1.2
  • Operating system and version: macOS Catalina

use node --loader ts-node/esm ./my-script.ts instead ts-node ./my-script.ts

@guoyunhe
Copy link

@guoyunhe check this out
#1171 (comment)

I tried but no luck.

@buzuosheng
Copy link

after i added "type": "module", when run npm run dev, i got this

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Users\1\xxxx\next.config.js
require() of ES modules is not supported.

@sodhisaab
Copy link

after i added "type": "module", when run npm run dev, i got this

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Users\1\xxxx\next.config.js
require() of ES modules is not supported.

any solution? I am facing same problem

@lovetingyuan
Copy link

lovetingyuan commented Sep 14, 2021

https://github.com/TypeStrong/ts-node/tree/main/esm-usage-example

@AhmadRaza9
Copy link

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".json" for D:\Installations\laragon\www\wp-pre-theme\wp-content\themes\firsttheme\package.json
at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:71:15)
at Loader.getFormat (internal/modules/esm/loader.js:102:42)
at Loader.getModuleJob (internal/modules/esm/loader.js:231:31)
at async ModuleWrap. (internal/modules/esm/module_job.js:58:21)
at async Promise.all (index 10)
at async link (internal/modules/esm/module_job.js:63:9) {
code: 'ERR_UNKNOWN_FILE_EXTENSION'

@cspotcode
Copy link
Collaborator

Node has restrictions about JSON files in ESM, maybe you're hitting that.

@AhmadRaza9
Copy link

I have no idea, i can't understand 😒

@bishop254
Copy link

Node has restrictions about JSON files in ESM, maybe you're hitting that.

@cspotcode. Could you kindly expound on this. Seems really interesting. And how can we mitigate this problem?

@cspotcode
Copy link
Collaborator

Node's documentation has everything you need.

@CSchulz
Copy link

CSchulz commented Aug 23, 2022

Node has restrictions about JSON files in ESM, maybe you're hitting that.

@cspotcode. Could you kindly expound on this. Seems really interesting. And how can we mitigate this problem?

https://nodejs.org/api/esm.html#json-modules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants