Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Cannot read property 'ts' of undefined

I have been trying to setup React in my .Net Core project and I am getting build failed errors when trying to run WebPack.

I have tried updating ts-loader and webpack versions and trying different solutions from GitHub and StackOverflow but these didn't work.

Webpack console error

Entrypoint global = home-bundle.js home-bundle.js.map
  [0] ./ClientApp/Home/home.tsx 289 bytes {0} [built] [failed] [1 error]

WARNING in configuration
The 'mode' option has not been set. 
Set 'mode' option to 'development' or 'production' to enable defaults for this environment.

ERROR in ./ClientApp/Home/home.tsx
Module build failed: TypeError: Cannot read property 'ts' of undefined
  at getLoaderOptions (D:\MyFirstProject\Travel\node_modules\ts-loader\dist\index.js:70:44)
  at Object.loader (D:\MyFirstProject\Travel\node_modules\ts-loader\dist\index.js:23:19)

webpack.config

module.exports = [
  {
    entry: {
      global: "./ClientApp/Home/home.tsx"
    },
    output: {
      filename: "home-bundle.js",
      path: __dirname + "/wwwroot/app/",
      sourceMapFilename: "home-bundle.js.map"
    },
    devtool: "source-map",
    resolve: {
      extensions: [".ts", ".tsx", ".js"],
      modules: ["node_modules"]
    },
    module: {
      rules: [
        {
          test: /\.tsx?$/,
          loader: "ts-loader"
        },
        {
          enforce: "pre",
          test: /\.tsx?$/,
          use: "source-map-loader",
          exclude: /node_modules/
        }
      ]
    }
  }
];

package.json

{
  "dependencies": {
    "@types/es6-promise": "0.0.33",
    "@types/lodash": "4.14.84",
    "@types/react": "15.0.11",
    "@types/react-dom": "0.14.23",
    "es6-promise": "4.1.1",
    "lodash": "4.17.4",
    "react": "15.6.2",
    "react-dom": "15.6.2",
    "source-map-loader": "0.2.3",
    "ts-loader": "2.3.7",
    "typescript": "2.3.4",
    "webpack": "3.8.1"
  },
  "devDependencies": {
    "ts-loader": "2.3.7",
    "url-loader": "0.5.9",
    "webpack-livereload-plugin": "1.0.0"
  },
  "scripts": {
    "webpack": "webpack -p",
    "test": "echo \"Error: no test specified\" && exit 1",
    "watch": "webpack -w"
  }
}
like image 406
aj12 Avatar asked Mar 25 '18 18:03

aj12


1 Answers

I just got same error when upgrading from webpack 3 to 4.

Solution for me was to upgrade ts-loader from 3.1.1 to 4.4.2

Hope it helps.

like image 70
Sergio Mazzoleni Avatar answered Nov 09 '22 01:11

Sergio Mazzoleni