Webpack encounters issues when compiling ES6 syntax from external packages

I am encountering an issue with integrating an ES6 npm package, specifically one called gamesystem, into my project and building it with webpack (along with babel). For some reason, the build fails to process any ES6 code within the external dependency. However, when I have the same code as a dependency with a relative path, it works perfectly fine.

Here is an example of the app code:

'use strict';

import World from 'gamesystem'; // This line breaks
//import World from '../node_modules/gamesystem'; // Also breaks
//import World from '../gamesystem'; // This line works (I copied the same directory gamesystem outside the node_modules directory)

let world = new World();

Error message:

ERROR in ./~/gamesystem/World.js
Module parse failed: /home/user/project/node_modules/gamesystem/World.js Line 3: Unexpected token
You may need an appropriate loader to handle this file type.
| 'use strict';
| 
| import System from './System';
| 
| export default class World {
 @ ./src/app.js 3:18-39

Here is a snippet of the webpack configuration:

'use strict';

// Required modules
let WebpackNotifierPlugin = require('webpack-notifier');
let HtmlWebpackPlugin = require('html-webpack-plugin');

let config = {
  entry: {
    app: __dirname + '/../src/app.js'
  },
  devtool: 'source-map',
  devServer: {
    stats: {
      modules: false,
      cached: false,
      colors: true,
      chunk: false
    },
    proxy: require('./devProxy')
  },
  module: {
    loaders: [{
      test: /\.js$/,
      loader: 'babel',
      exclude: /node_modules/,
      query: {
        presets: ['es2015', 'stage-0']
      }
    },
    {
      test: /\.css$/,
      loader: 'style!css'
    },
    {
      test: /\.html$/,
      loader: 'raw'
    }]
  },
  plugins: [
    new WebpackNotifierPlugin(),
    new HtmlWebpackPlugin({
      template: __dirname + '/../src/index.html',
      inject: 'body',
      minify: false
    })
  ]
};

module.exports = config;

Answer №1

You've made a specific exclusion of node_modules when using babel-loader:

exclude: /node_modules/,

If you want to include modules from node_modules in babel, you'll need to make some adjustments. One option is to explicitly specify includes like this:

// Make sure to require path
// var path = require('path')

include: [
  // Include all files from your app's JavaScript path
  path.resolve(__dirname, 'my-app-js-path'),

  // Include certain modules related to gamesystem
  /\bgamesystem\b/,
],

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

Challenge encountered when utilizing more complex nested data obtained from API

I am facing an issue while trying to retrieve data from PokeAPI. Below is the code for the PokemonCard component that I have been using. import React, { useEffect, useState } from "react"; import axios from "axios"; const PokemonCard = ({ pokemonID }) ...

Getting the selected value from a dropdown menu in ReactJS

I am working on creating a form that resembles the following structure: var BasicTaskForm = React.createClass({ getInitialState: function() { return { taskName: '', description: '', emp ...

What is the best way to assign an onClick event in React while using document.createElement()?

I am using document.createElement to create an <input>. How can I assign the onClick property in React? var input = document.createElement("input"); input.onClick = {setCount()}; //??? Here is the React code snippet: <input type="s ...

Ways to mandate adjusting an option in <select> tag

Using the code $('select').change(function(){alert('changed');});, I am able to trigger an alert every time the option is changed. However, I also need to trigger the alert when the default option, which is the first one in the list, is ...

The functionality of a website's design acts as a safeguard against unauthorized data transfers

I am encountering a major issue with my website. The layout I have created seems to be hindering me from making any modifications. My website consists of several containers, with three small containers stacked on top of each other as the main section of ...

The method options.domAPI is not a valid function in this context

While attempting to customize global variables using stylus config in Vuetify, I discovered that it is no longer supported. To address this issue, I executed the following command to install the necessary loaders: npm i --save-dev stylus stylus-loader css ...

What are some effective strategies for enhancing the performance of React user interfaces?

I'm currently dealing with a performance challenge in one of my React applications. What are some best practices to enhance the responsiveness of the User Interface? One approach could be to minimize the usage of conditional expressions like { carIsR ...

Why is the image popup feature not functioning properly? What could be causing the issue?

I'm experiencing issues with the functionality of my HTML file and image popup. I've attempted to troubleshoot it multiple times without success. Here is a code snippet for reference: You can view and run the code on CodePen: Open <html> ...

Automatic button rotation

I managed to set up a button that works on click with a delay, making it semi-automatic. However, I'm struggling with getting it to not pause after just one click. Here's what I have so far: <!DOCTYPE html> <html> <body> &l ...

Leveraging the useEffect hook to make multiple API calls in ReactJS

I have a useEffect function in my react component where I am calling the API videoGridState. The issue I am facing is that the API is being called twice; once when the page initially reloads and again when the count changes. I want it to be called only onc ...

Locating elements with Selenium Webdriver using xpath

<a style="color:White;" href="javascript:__doPostBack('dnn$ctr674$Case$gvCaseSearchDetails','Page$777')">777</a> Can anyone help with writing an xpath for the HTML code above? The goal is to locate elements using the identi ...

Using Jquery to fetch data with a specific port by using the $.getJSON method

Struggling to utilize $.getJSON in a scenario where my local app needs to call another local app on a different port. Specifically, my app is hosted on localhost:3000, but I'm attempting to use $.getJSON to request data from an app running on localhos ...

The delete_node() function in jstree does not seem to be removing nodes

In my attempt to create a custom context menu for different nodes, I've managed to display different labels for clicks on folders or files. However, I am facing some challenges when trying to delete them. Take a look at my code snippet. Due to diffic ...

Running the application in production mode using npm is encountering issues after it has been deployed

After successfully running npm production on my localhost, I encountered an error message when trying to run the same command after hosting the application. npm version: 6.10.3 webpack version: [email protected] // node_modules/laravel-mix/setu ...

How can I incorporate Bootstrap 5 into my Storybook setup?

As I work on constructing a storybook, I am exploring the idea of integrating Bootstrap 5 into it. I am wondering if the most effective approach to implement it is by utilizing a preview-head.html file within the .storybook directory. Despite my efforts ...

"Troubleshooting: Fixing the issue with jQuery datepicker not

After implementing the jQuery Datepicker on a field, I noticed that even though assigning a value to the field shows in Chrome's developer tools... https://i.sstatic.net/We7Ua.png Unfortunately, the assigned value does not show on the front end. I ...

Retrieving nested array elements in MongoDB using parent ID element (JavaScript)

Suppose I have a MongoDB collection structured as follows: _id: ObjectId(" <a objectid> ") id: " <a userid> " __v: 0 subscribedTo: Object Regardless of whether or not Mongoose is being used, how can I execute a query to acc ...

What is the best method for reducing the length of the user input in a JQuery auto-complete box?

Is there a method to eliminate leading and trailing spaces from the user's input in a jQuery auto-suggesting text <input> box before comparing it to a list of names and values? In my current setup, users are expected to enter names into a textbo ...

Using JavaScript regular expressions for email validation criteria

Hey there, I am struggling with Regular Expressions, especially when it comes to client side validation for a specific field. Can you please help me come up with a Regular Expression that would verify if an email address is valid based on these criteria: ...

The arrangement of a JSON array can be modified by AngularJS' Ng-repeat functionality

Hello everyone at SO: On March 18, 2014, I encountered a situation while trying to use ng-repeat. The elements inside the array, retrieved from a Json string, seem to be changing their original order. To clarify, the initial variables in the array pertai ...