TypeError: Cannot read property 'ts' of undefined

时间:2018-03-25 18:48:10

标签: reactjs webpack asp.net-core .net-core

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"
  }
}

1 个答案:

答案 0 :(得分:7)

从webpack 3升级到4时,我遇到了同样的错误。

对我来说,解决方案是将ts-loader从3.1.1升级到4.4.2

希望有帮助。