Webpack error: TypeError: data is undefined, although it clearly is

  browser, javascript, typescript, webpack

After minifying my code into a bundle.js and running it through the browser, I see:

TypeError: data is undefined 

Which CLEARLY is wrong. If I inspect the code I can see the variable defined:

const data = await visitors.create({})
console.log(data)

That’s the line the fails. I have no idea why. This is my webpack config:

const path = require('path')

module.exports = (mode = 'development') => {
  return {
    mode,
    devtool: mode === 'development' ? 'inline-source-map' : 'none' ,
    entry: path.resolve(__dirname, 'src/index.ts'),
    module: {
      rules: [
        {
          test: /.ts$/,
          loader: 'ts-loader',
          exclude: /node_modules/,
        },
      ],
    },
    resolve: {
      extensions: ['.ts'],
    },
    output: {
      filename: 'index.js',
      path: path.resolve(__dirname, 'dist'),
    },
  }
}

And this is my tsconfig file:

{
  "compilerOptions": {
    "lib": ["dom", "dom.iterable", "esnext"],
    "module": "es6",
    "moduleResolution": "node",
    "noImplicitAny": true,
    "target": "es5"
  },
  "exclude": ["dist", "test", "node_modules"],
  "include": ["src/**/*.ts"]
}

I’ve spent hours on this, and I couldn’t crack it.

Source: Ask Javascript Questions

LEAVE A COMMENT