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

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for ./webpack.config.ts #2916

Closed
fregante opened this issue Aug 24, 2021 · 13 comments · Fixed by webpack/webpack.js.org#5361

Comments

@fregante
Copy link

Describe the bug

Webpack is unable to read Webpack’s config file in TypeScript if type is module. This is basically a duplicate of

which reportedly has been fixed by

but it fails with the exact test config included in that PR.

What is the current behavior?

[webpack-cli] Failed to load './webpack.config.ts' config
[webpack-cli] TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for ./webpack.config.ts
    at new NodeError (node:internal/errors:329:5)
    at Loader.defaultGetFormat [as _getFormat] (node:internal/modules/esm/get_format:71:15)
    at Loader.getFormat (node:internal/modules/esm/loader:104:42)
    at Loader.getModuleJob (node:internal/modules/esm/loader:242:31)
    at async Loader.import (node:internal/modules/esm/loader:176:17)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15)
    at async loadConfig (./node_modules/webpack-cli/lib/webpack-cli.js:1182:35)
    at async WebpackCLI.resolveConfig (./node_modules/webpack-cli/lib/webpack-cli.js:1290:38)
    at async WebpackCLI.createCompiler (./node_modules/webpack-cli/lib/webpack-cli.js:1670:22)
    at async WebpackCLI.buildCommand (./node_modules/webpack-cli/lib/webpack-cli.js:1785:20) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

To Reproduce

Steps to reproduce the behavior:

echo '{"type": "module"}' > package.json
echo 'export default {}' > webpack.config.ts
echo 'console.log("hello world")' > index.js
echo '{"compilerOptions":{"module":"esnext", "allowSyntheticDefaultImports":true}}' > tsconfig.json
npm install webpack-cli webpack typescript ts-node
npx webpack-cli --entry ./index.js --mode production

If you drop type:module it reverts to the regular Unexpected token 'export' error.

Please paste the results of npx webpack-cli info here, and mention other relevant information

  System:
    OS: macOS 11.5.2
    CPU: (16) x64 Intel(R)
  Binaries:
    Node: 15.14.0 - /usr/local/bin/node (also tried with node 16)
    Yarn: 1.22.11 - /usr/local/bin/yarn
    npm: 7.7.6 - /usr/local/bin/npm
  Packages:
    webpack: ^5.51.1 => 5.51.1 
    webpack-cli: ^4.8.0 => 4.8.0 
  Global Packages:
    webpack-cli: 4.6.0
    webpack: 5.31.0
@fregante fregante added the Bug label Aug 24, 2021
fregante added a commit to fregante/webpack-sandbox that referenced this issue Aug 24, 2021
@fregante
Copy link
Author

Repro and output (webpack.log) is also available at: https://github.com/fregante/webpack-sandbox/tree/main/ts-node-esm

@alexander-akait
Copy link
Member

alexander-akait commented Aug 24, 2021

It was answered here #2458 (comment), we can't fix it on our side

@fregante
Copy link
Author

we can't fix it on our side

Can you catch the error and display a possible solution?

https://twitter.com/fregante/status/1407043890562179075

@alexander-akait
Copy link
Member

@fregante
Copy link
Author

fregante commented Aug 24, 2021

Correct link, I was showing what a good error looks like, which this isn't:

[webpack-cli] TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for ./webpack.config.ts

I wish tools caught these errors and suggested solutions instead of sending them on Google trips.

@fregante
Copy link
Author

As ESM packages become more popular, it's only time before more people encounter issues related to them. Google brought me to a StackOverflow answer that was "remove type:module" and the Webpack-specific search showed a mind-boggling three results.

Screen Shot

@alexander-akait
Copy link
Member

alexander-akait commented Aug 24, 2021

But we showed the real error and why it happens, nothing to say more here, it should be improved on ts-node, because only they known it is ECMA output and no loader(s)

@alexander-akait
Copy link
Member

Here right solution:

node --loader ts-node/esm node_modules/.bin/webpack-cli --entry ./index.js --mode production

@alexander-akait
Copy link
Member

Only one thing valid here, we can add this to docs

@alexander-akait
Copy link
Member

alexander-akait commented Aug 24, 2021

/cc @snitin315 @anshumanv We should improve our docs for ECMA modules (i.e. type: "module" in package.json) and typescript ECMA modules output:

  1. node --loader ts-node/esm node_modules/.bin/webpack-cli --entry ./index.js --mode production
  2. NODE_OPTIONS="--loader ts-node/esm" npx webpack-cli --entry ./index.js --mode production (simple)

@snitin315
Copy link
Member

I will document it 👍🏻

@fregante
Copy link
Author

I like the second one, without npx

@alexander-akait
Copy link
Member

Added to docs

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

Successfully merging a pull request may close this issue.

3 participants