Webpack 2 and Babel seem to be having trouble converting the code to ES2015

I've recently put together a sample project using webpack and started incorporating babel. Despite trying out various configurations from different sources, I'm still encountering issues with getting it to work properly.

Below is my webpack.config.js:

const path=require('path');

const config={

    entry:'./src/index.js',
    output:{
        path:path.resolve(__dirname,'build'),
        filename:'bundle.js'
    },
    module: {
        loaders:[
            {
                test:'/\.js?$/',
                loader: "babel",
                exclude: /node_modules/,
                query: {
                    presets: ['es2015'],
                }
            }
        ]
    }

};


module.exports=config;

.babelrc:

{
    "presets":["es2015"]
}

package.json:

{
  "name": "webpack2",
  "version": "1.0.0",
  "description": "This is first Webpack 2 project",
  "main": "index.js",
  "scripts": {
    "build": "webpack"
  },
  "author": "Viraj Nimbalkar",
  "license": "",
  "devDependencies": {
    "babel-core": "^6.23.1",
    "babel-loader": "^6.4.0",
    "babel-preset-env": "^1.2.1",
    "babel-preset-es2015": "^6.22.0",
    "webpack": "^2.2.1"
  }
}

Output in bundle.js:

(Output content in bundle.js)

Can anyone assist me in pinpointing the issue with my setup?

Additionally, I'm unclear on the distinction between .babelrc and the query within the loader configuration.

Answer №1

It is essential to take note that in webpack 2.0, the term loaders has been replaced with rules in the configuration of the module. Ensure your webpack configuration reflects this change by updating it as shown below.

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

module.exports = webpackConfig;

Answer №2

When the .babelrc file is found, it is given priority and read first

npm install  babel-plugin-transform-runtime

Add this to .babelrc

{
  "plugins": [
    "transform-runtime"
  ],
  "presets": [
    "es2015"
  ]
}

Answer №3

module: {
  rules: [
    { test: /\.js$/, exclude: /node_modules/, loader: "babel-loader" }
  ]
}

babel-loader is the recommended option for handling JavaScript files. For more information, you can check out here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Displaying a loading indicator while a file is downloading and the page is being

Is there a way to show a loading indicator while a PDF is being generated in PHP? I redirect to a separate page for the PDF generation process, but the original page stays open and simply downloads the file once it's ready. How can I make a loading in ...

Having trouble with AngularJs 1 functionality?

Having trouble fetching data from a JSON file. I've tried all available options, but nothing seems to be working. Not sure if I need to import something else. The JSON file is located at the same level as the index file. Any help would be much appreci ...

Unlock the full potential of custom authorization providers when integrating them with Supabse

Supabase user here looking to implement authorization with a third-party service that isn't on the supported list. Any suggestions on how to go about this? ...

I must develop a custom function that generates a pure JavaScript string with the 'name' index and includes all the 'props'

My code is almost correct, but instead of returning a ':' in the Json result as desired, it returns a ','. Is there a way to achieve the desired result without modifying the JSON string like I did with the "replaces"? I am searching fo ...

Here's a way to run JavaScript code from a <script> tag included in an AJAX response

Currently, I am making a jQuery GET request in this format: $.get($(this).attr("href"), $(this).serialize(), null, "script"); I'm expecting the response to be enclosed in script tags. I know that the browser won't run the response if it contai ...

Display: Show view once forEach loop finishes execution

I'm facing an issue with my update query execution within a loop. Is there a way to trigger the rendering of a view once the forEach loop completes all its iterations? Here's the snippet of code: conn.query(`SELECT Id, ${sfColumn} from Lead`, ...

Update google maps markers and infoBubbles dynamically with ajax

I am currently using asp mvc in conjunction with jquery and the google maps api to showcase and update various locations on a map. Main Objective: Utilize markers to mark multiple locations Display brief information about these locations using info ...

How can I fetch data from a ManyToMany jointable using Typeorm?

Is there a way to retrieve the categories associated with posts when fetching user data? Data Models: @Entity() export class Category extends BaseEntity { @PrimaryGeneratedColumn() id: string; @Column() name: string; @Column() description: s ...

The incorporation of zoom disrupts the smooth scrolling capability of the menu

My landing page has a menu that scrolls users to the selected section. However, my client prefers the page at a 90% zoom level. To accommodate this request, I added the following line of code: body { zoom:90%; } Unfortunately, when I click on a menu o ...

Using a package with `webfontloader` in NextJs for Server-Side Rendering: A guide

Currently, I am working on creating an application with nextJS and incorporating a package that utilizes Webfontloader internally. After installing the library, my application encountered an error preventing it from running successfully. It seems like th ...

working with a list of Python objects in a JavaScript environment

Currently, I am building a web application using Flask and Python. In my Python code, I have a class that can be parsed as JSON. class uItem: itemCount = 0 def __init__(self, id, name): self.id = id self.name = name I am trying to acce ...

find all occurrences except for the last of a pattern in javascript

When considering the patterns below: "profile[foreclosure_defenses_attributes][0][some_text]" "something[something_else_attributes][0][hello_attributes][0][other_stuff]" It is possible to extract the last part by utilizing non-capturing groups: var rege ...

If the iframe's CSS source is updated, the parent's CSS source will also change

I'm currently working on a unique school project that involves creating multiple CSS styles for different views. <link rel="stylesheet" type="text/css" href="css/main.css" title="main" media="screen"> <link rel="stylesheet" type="text/css" h ...

Modify a single parameter of an element in a Map

Imagine I have a map data type exampleMap: Map<string, any> The key in the map is always a string, and the corresponding value is an object. This object might look like this: { name: 'sampleName', age: 30} Now, let's say the user se ...

Connecting Ionic/Angular directives to scrollbars

I'm having an issue with my scrolling div: <div class="list"> <div class="item" ng-repeat="post in posts"> <img src="post.img" is-visible/> </div> </div> and I've implemented this directive: angular.elemen ...

Looking for specific styles within CSS classes

I am looking to identify all the classes with styling attributes defined using either vanilla JS or jQuery. Specifically, I want to find classes that have the border style defined along with its value. It would be great if I could also modify these classes ...

Tips for organizing your JSON Structure within ReactJs

In the given example, I have a JSON structure with information about different airlines. The Airline Name is dynamic and we need to separate the JSON into an expected array format. const arr = [ { Airline: "Goair", Departure: "01:50" ...

Is it considered fundamentally inappropriate to call $scope.$digest within $scope.$on?

I recently inherited some AngularJS code, and my knowledge of both the codebase and Angular itself is limited. Within the code I inherited, there are instances where $scope.$digest is being called inside a $scope.$on method within a controller. Here's ...

Is there a way for me to ensure that a response is only returned once a method call has been completed in

Seeking assistance with a Node.js application using Express. I am trying to create a REST endpoint that returns the response of an HTTP call. However, no matter what I attempt, it always returns before the HTTP request has completed. Can anyone provide g ...

Filtering deep nested data in Javascript JSON: A guide

I have a JSON data that needs to be filtered based on three values. I have managed to filter based on two values, "timestamp" which is not nested, but I am struggling with the nested value "fromMe". While I can easily access the "timestamp" value as it is ...