0

I have the following in my webpack.config.js file:

const webpack = require('webpack');
const path = require('path');

const config = {
  context: path.resolve(__dirname, 'src'),
  entry: './app.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        exclude: path.resolve(__dirname, 'node_modules'),
        use: [ 'style-loader', 'css-loader' ]
      },
      {
        test: /\.js$/,
        exclude: path.resolve(__dirname, 'node_modules'),
        use: [{
          loader: 'babel-loader',
          options: {
            presets: [
              ['es2015']
            ]
          }
        }]
      }
    ]
  }
};

module.exports = config;

When I run webpack I get the following errors:

ERROR in (webpack)/~/buffer/index.js
Module build failed: Error: Couldn't find preset "es2015" relative to directory "/home/echessa/node/node-v6.9.1-linux-x64/lib/node_modules/webpack/node_modules/buffer"
    at /home/echessa/Documents/DEV/demo-project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19
    at Array.map (native)
    at OptionManager.resolvePresets (/home/echessa/Documents/DEV/demo-project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)
    at OptionManager.mergePresets (/home/echessa/Documents/DEV/demo-project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10)
    at OptionManager.mergeOptions (/home/echessa/Documents/DEV/demo-project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14)
    at OptionManager.init (/home/echessa/Documents/DEV/demo-project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
    at File.initOptions (/home/echessa/Documents/DEV/demo-project/node_modules/babel-core/lib/transformation/file/index.js:212:65)
    at new File (/home/echessa/Documents/DEV/demo-project/node_modules/babel-core/lib/transformation/file/index.js:135:24)
    at Pipeline.transform (/home/echessa/Documents/DEV/demo-project/node_modules/babel-core/lib/transformation/pipeline.js:46:16)
    at transpile (/home/echessa/Documents/DEV/demo-project/node_modules/babel-loader/lib/index.js:48:20)
    at Object.module.exports (/home/echessa/Documents/DEV/demo-project/node_modules/babel-loader/lib/index.js:163:20)
 @ ../~/css-loader/lib/css-base.js 1:0-117
 @ ../~/css-loader!./main.css
 @ ./main.css
 @ ./app.js

I'm using Webpack version 2.3.2.

The following are the dev dependencies I'm using:

"devDependencies": {
  "babel-core": "^6.24.0",
  "babel-loader": "^7.0.0-beta.1",
  "babel-preset-es2015": "^6.24.0",
  "css-loader": "^0.27.3",
  "style-loader": "^0.16.1"
}

For babel-loader, I'm using a beta version because version 7.x is recommended for use with Webpack 2. The following is from the Github repo:

webpack 1.x | babel-loader <= 6.x

webpack 2.x | babel-loader >= 7.x (recommended) (^6.2.10 will also work, but with deprecation warnings)

Version 7.0.0 isn't out yet, so I'm using the beta. I've tried using 6.2.10 but I still get the same bugs.

When I only use one of the rules, webpack builds successfully. E.g. when I only have the js rules (and remove CSS files), it works.

module: {
  rules: [
    {
      test: /\.js$/,
      exclude: path.resolve(__dirname, 'node_modules'),
      use: [{
        loader: 'babel-loader',
        options: {
          presets: [
            ['es2015']
          ]
        }
      }]
    }
  ]
}

Or when I only have the CSS rules (and remove any ES6 code), it works.

module: {
  rules: [
    {
      test: /\.css$/,
      exclude: path.resolve(__dirname, 'node_modules'),
      use: [ 'style-loader', 'css-loader' ]
    }
  ]
}

What could be up? Could it be an issue with babel-loader or is my config file not structured correctly?

Community
  • 1
  • 1
echessa
  • 49
  • 2
  • 8

1 Answers1

0

I've found a solution that works. If I move the following from the webpack.config.js file to a .babelrc file, then webpack runs fine.

{
  "presets": ["es2015"]
}

I'm not sure why having the presets in the config file wasn't working. Any Webpack 2 guide I've seen online has them in the config file. Anyway, I thought I should leave this solution here in case someone else gets stuck.

echessa
  • 49
  • 2
  • 8