webpack-cli: TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for ./webpack.config.ts
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
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 6
- Comments: 16 (9 by maintainers)
Commits related to this issue
- Add `ts-node-esm` (failing) https://github.com/webpack/webpack-cli/issues/2916 — committed to fregante/webpack-sandbox by fregante 5 years ago
Correct link, I was showing what a good error looks like, which this isn’t:
I wish tools caught these errors and suggested solutions instead of sending them on Google trips.
Here right solution:
/cc @snitin315 @anshumanv We should improve our docs for ECMA modules (i.e.
type: "module"inpackage.json) and typescript ECMA modules output:node --loader ts-node/esm node_modules/.bin/webpack-cli --entry ./index.js --mode productionNODE_OPTIONS="--loader ts-node/esm" npx webpack-cli --entry ./index.js --mode production(simple)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.@kasir-barati You can use https://nodejs.org/api/cli.html#node_optionsoptions
Cannot be easily done in tools and build systems like Nx.
Only one thing valid here, we can add this to docs