question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Cannot read property 'babel' of undefined when going from v7 to v8

See original GitHub issue

I’m submitting a bug report

Webpack Version: 4.x

Babel Core Version: 7.x

Babel Loader Version: 8.x

Please tell us about your environment: OSX 10.x

Current behavior:

ERROR in ./src/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
TypeError: Cannot read property 'babel' of undefined
    at Object.module.exports (/html-engine/html-engine/node_modules/babel-loader/lib/index.js:104:35)

I tracked it down to this line of code

var globalOptions = this.options.babel || {};

This fixes the issue: var globalOptions = this.options && this.options.babel || {};

I wanted to fork and submit a PR but I couldn’t really find the code I wanted to change in the source.

Also wondering if this is a usage problem. I don’t believe it is.

Anyways here is my babelrc

{
  "presets": ["@babel/preset-env", "@babel/react"]
}

webpack.config

const fs = require("fs");
const path = require("path");
const webpack = require("webpack");
const Dotenv = require("dotenv-webpack");
const MinifyPlugin = require("babel-minify-webpack-plugin");


module.exports = (serverPath, clientPath) => {
  const getBabelPresetPath = name => {
    if (fs.existsSync(path.resolve(clientPath, "node_modules", name))) {
      return path.resolve(clientPath, "node_modules", name);
    } else if (fs.existsSync(path.resolve(serverPath, "node_modules", name))) {
      return path.resolve(serverPath, "node_modules", name);
    } else {
      return name;
    }
  };

  return {
    mode: 'production',
    entry: path.resolve(clientPath, "src", "index.js"),
    output: {
      filename: "bundle.js",
      path: path.resolve(clientPath, "dist"),
      publicPath: "/dist/"
    },
    resolve: {
      modules: [
        path.resolve(clientPath, "node_modules"),
        path.resolve(serverPath, "node_modules")
      ]
    },
    resolveLoader: {
      modules: [
        path.resolve(clientPath, "node_modules"),
        path.resolve(serverPath, "node_modules")
      ]
    },
    module: {
      rules: [
        {
          test: /\.(jsx|js)?$/,
          use: {
            loader: 'babel-loader',
            options: {
              presets: ['@babel/react', '@babel/env']
            }
          },
          exclude: /(demo|__tests__|__mocks__|__snapshots__|examples)/,
          include: path.resolve(clientPath, 'src'),

        },
        {
          test: /\.css$/,
          use: ["style-loader", "css-loader"],
          exclude: path.resolve(clientPath, "node_modules")
        },
        {
          test: /\.(scss|sass)$/,
          use: ["style-loader", "css-loader", "sass-loader"],
          exclude: path.resolve(clientPath, "node_modules")
        },
        {
          test: /\.svg$/,
          use: "url-loader?mimetype=image/svg+xml",
          exclude: path.resolve(clientPath, "node_modules")
        },
        {
          test: /\.png$/,
          use: "url-loader?mimetype=image/png",
          exclude: path.resolve(clientPath, "node_modules")
        },
        {
          test: /\.jpg$/,
          use: "url-loader?mimetype=image/jpg",
          exclude: path.resolve(clientPath, "node_modules")
        },
        {
          test: /\.gif/,
          use: "url-loader?mimetype=image/gif",
          exclude: path.resolve(clientPath, "node_modules")
        },
        {
          test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
          use: "url-loader?mimetype=application/font-woff",
          exclude: path.resolve(clientPath, "node_modules")
        },
        {
          test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/,
          use: "url-loader?mimetype=application/font-woff",
          exclude: path.resolve(clientPath, "node_modules")
        },
        {
          test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
          use: "url-loader?mimetype=application/octet-stream",
          exclude: path.resolve(clientPath, "node_modules")
        },
        {
          test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
          use: "file-loader",
          exclude: path.resolve(clientPath, "node_modules")
        }
      ]
    },
    plugins: [
      new webpack.NamedModulesPlugin(),
      new webpack.NoEmitOnErrorsPlugin(),
      new webpack.LoaderOptionsPlugin({ options: {} }),
      new webpack.EnvironmentPlugin({
        NODE_ENV: "production"
      }),
      new Dotenv({
        path: path.resolve(clientPath, ".env"),
        silent: true
      }),
      new MinifyPlugin({}, {})
    ]
  };
};

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

36reactions
benwiley4000commented, Jan 27, 2019

I have the same issue - using babel 6. Is babel-loader 6 no good for webpack 4? I’m getting the issue keeping the same babel version but upgrading from webpack 1 to 4.

15reactions
loganfsmythcommented, Aug 31, 2018

That option hasn’t existed since babel-loader@6.x, so it seems likely that you’re using an older version of babel-loader somehow. Can you check the version in node_modules/babel-loader/package.json?

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: Cannot read property 'babel' of undefined
Following is my package json file. I have installed node_modules successfully. But Getting the error: "TypeError ...
Read more >
Upgrade to Babel 7
This just means Babel itself won't run on older versions of Node. It can still output code that runs on old Node versions....
Read more >
babel-loader - webpack
webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable...
Read more >
babel-loader - npm
Start using babel-loader in your project by running `npm i babel-loader`. ... This README is for babel-loader v8/v9 with Babel v7 If you...
Read more >
Changelog - Cypress Documentation
In Cypress 12, we enforce running tests in a clean browser context through ... The Cannot read property "fireChangeEvent" of undefined error will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found