Issue
I've published a package to NPM lately, which includes source. I'm also using webpack in various apps to pull in that source. I found that when I reference code under a node_modules folder (eg; myapp/node_modules/spjsomfluent/src/fuent.ts) I would get:
Module parse failed: Unexpected token (15:11)
You may need an appropriate loader to handle this file type.
Cause:
The webpack rule exclude: /node_modules/
matches any folder in the path. If the source is located under a node_modules folder that error can occur with webpack and ts-loader (TypeScript).
My original rule looked like:
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
},
Fix:
Change exclude: /node_modules/
to exclude: path.resolve(__dirname, '/node_modules')
This will stop the rule matching node_modules anywhere in the path and instead match it under the apps node_modules folder.
My new rule is:
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: path.resolve(__dirname, '/node_modules')
}
]
},