Skip to content
Advertisement

Webpack fails to parse typescript files. Module parse failed: Unexpected token

I was setting up Typescript and Webpack on an old project of mine and suddenly encountered this error:

You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders

Then I have created a new project from scratch that goes as follow:

webpack.config.js

JavaScript

tsconfig.json

JavaScript

src/index.ts (sourced from here)

JavaScript

… and the error appears again :/

ERROR in ./src/index.ts 2:12 Module parse failed: Unexpected token (2:12) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders

I feel like I’m missing something, what it can be?

ps: I have tried also different loaders as ts-loader and awesome-typescript-loader.

Thanks! Any help is appreciated :*

Advertisement

Answer

The root cause is that your Typescript rule isn’t matching (“currently no loaders are configured to process this file”), so Webpack is reading your TS files as Javascript and getting thrown by the TypeScript-specific : on line 2 character 12. From your webpack.config.js:

JavaScript

This should be a regular expression. Note the lack of single quotes:

JavaScript

See Webpack docs Rule.test and Condition for more.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement