Compilation error when importing enum from node modules in typescript
See original GitHub issueHi,
I’m trying to use some enum from a library I made in a Symfony project. The library is made with Vue and typescript, bundled with rollup.
The project is made Symfony 4.2, and the front with Vue 2 and typescript, Webpack Encore version is 1.6.1
The library is a dependency from my project, so I try to import the enum like this: import { MyEnum } from 'myLibrary/src/enum/MyEnum';
And the enum looks like this:
// node_modules/myLibrary/src/enum/MyEnum.ts
export enum MyEnum {
One = 'one',
Two = 'two',
Three = 'three'
}
But when I compile I got this error in console:
ERROR Failed to compile with 1 errors 4:37:05 PM
Error loading ./node_modules/myLibrary/src/enum/MyEnum.ts
FIX To process TypeScript files:
1. Add Encore.enableTypeScriptLoader() to your webpack.config.js file.
And this error in browser:
Module parse failed: Unexpected token (4:7)
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
> export enum MyEnum {
| One = 'one',
| Two = 'two'
I obviously already added enableTypeScriptLoader() in webpack.config.js.
If I create the same enum file into my project and import it, it works. And if I import interfaces from the same library, it works too.
I test my library in another project with VueCli and I got no error, so I’m pretty sure it comes from Webpack Encore.
Any idea how to solve this?
Thanks
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (1 by maintainers)
Top Related StackOverflow Question
Hello, I finally find how to fix this.
First, it wasn’t only an enum import issue, because I had the same error message when importing some Classes from .ts files in the library.
@Kocal 's solution was the first step, I added
allowTsInNodeModulesoption inenableTypeScriptLoader’s optionsBut this issue bring some info : https://github.com/symfony/webpack-encore/issues/186 : node_modules are excluded for this rule.
So, instead of deleting
rule.excludeas mentioned in the issue, I modified the “exclude” regex to exclude node_modules but my own modules (that are in a ‘@myCompany’ folder) like thisand so, it works fine!
Hi @floviolleau
I always get the same error. Do you declare the module in a
.d.tsfile in the library?Do you have a
node_modules/whitelisted_module.tsfile or is it a typo? (=>"include": ["node_modules/whitelisted_module"])Thanks!