What is the best way to structure my modules for dynamic importing in webpack using import()?

When I attempt to utilize import() with webpack in one of our components, instead of getting the module, I receive an object like this:

{
  __webpackChunkName: "_nteract_notebook_app_component_6bdbaebb993d75f174e800c88ff17fc7"
}

Here's an example of the code:

import("@nteract/notebook-app-component").then(App => {
  // Use App here!
  // ...
  // Unfortunately, we're not able to use it properly due to the strange webpack object returned
})

This behavior is quite different from how lodash loads and provides the actual module.

import("lodash").then(_ => {
  // Full implementation of lodash is available here!
})

Upon examining the generated chunk file, I noticed that it does contain JavaScript code. One peculiar thing to note is that some modules contain /*! no static exports found */ even though they have code within them. Here are the first 10 lines of the chunk file:

$ head -n 10 dist/chunks/_nteract_notebook_app_component_6bdbaebb993d75f174e800c88ff17fc7.js
(window["webpackJsonp"] = window["webpackJsonp"] || []).push([["chunks/_nteract_notebook_app_component_6bdbaebb993d75f174e800c88ff17fc7"],{

/***/ "../../../packages/dropdown-menu/src/index.js":
/*!************************************************************************************************!*\
  !*** /Users/kylek/code/src/github.com/nteract/nteract-ext/packages/dropdown-menu/src/index.js ***!
  \************************************************************************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {

"use strict";

Following that section, there is a large portion of code that has been evaluated using eval, which represents the actual module.

Answer №1

The reason behind this behavior stems from the implementation of the next.js preset for babel, particularly their unique approach to handling import(). They generate an object containing the webpackChunkName, which is later utilized in a lookup process through their dynamic() function.

To enable dynamic imports with webpack (and babel), it is essential to avoid using the next/babel preset in applications other than next.js and include the syntax-dynamic-import plugin in your babelrc configuration.

npm install --save-dev babel-plugin-syntax-dynamic-import

An example of a babelrc setup:

{
  "presets": ["env", "react"],
  "plugins": [
    "syntax-dynamic-import",
    "styled-jsx/babel"
  ]
}

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

NPM - Utilizing JavaScript for Importing, Concatenating, and Compiling (similar to SC

I need to find an npm tool that can help me build my JS files in a similar way to SCSS where I can have a main file with imports. The ultimate objective is to create a BAT file that can be used in PhpStorm file watcher to automatically compile my js files. ...

Guide on invoking a POST endpoint within another POST API in a Node.js Express server

I encountered an issue while attempting to use fetch in react.js with a backend node.js API URL, which then proceeds to make a POST API call within the server to another route utilizing a different URL. How can I tackle this task effectively? See the code ...

Managing keypress events on a single page within Vue Router

Looking for a solution in my Vue.js SPA, where I have a specific page requiring keyboard interaction and using Vue Router for navigation. Currently, I have set up the following to handle keyboard events: const interactiveComponent = { // methods: ...

The parameter '{ validator: any; }' cannot be assigned to the ValidatorFn type in this context

I am currently experiencing a challenge while attempting to create a custom validator using Angular. I have created a form for my sign-up page and wanted to ensure that the password and confirm password fields match by implementing a custom validator. Des ...

I am puzzled as to why I keep receiving the error message "Unexpected token }"

Apologies for the syntax-related query, but after exhausting all my debugging efforts, I am unable to identify any errors. My current JavaScript file looks like this: $(function () { // equivalent to $(document).ready(function() { var snakeBoardHold ...

Angular ngClass and ngIf directives failing to update upon alterations

In my current Angular project, I am working on a functionality where I need to dynamically change a class based on a variable without having to refresh the page. I have experimented with *ngIf/else and [ngClass] directives, which do work, but unfortunatel ...

Is there a way to leverage the "locals" parameter in the jade compile() function on the client-side?

My Objective The server is a Node.js application. My goal is to share code with the client using Jade template helpers (for functions) or globals (for objects/variables). Any new helpers and globals should be accessible in every client template. This quer ...

"Running a react-app on a node server: Step-by-step guide

Is it possible to configure Webpack manually without using create-react-app? Can it be integrated with a Webpack dev server? What is the simplest method to do this without having to use Express? ...

What exactly sets one string apart from another? (JavaScript)

Is it possible to use JavaScript to detect the specific part of strings that makes them different from each other? For example, consider the following three strings: String1 = "Java1String" String2 = "Java2String" String3 = "Java3String" If String1 is t ...

Issues with the directory for Chrome

Currently, I am utilizing jQuery and running an HTML file on my local machine without a server. Interestingly, the code works perfectly fine on Firefox but encounters issues on Chrome: $('#result').load('test.html'); It appears that t ...

What is the best way to exclude a particular character from a text element utilizing jquery?

Looking to extract the numerical value from a div containing: <div class="balance"...>$500.48</div> The goal is to retrieve 500.48 as a number, not a string. One approach is to use alert($(".balance").text()) to verify that the content is ret ...

Explore the Filter List without being affected by the arrangement of words or the level of search precision

I was able to resolve the issue by adjusting my search filter algorithm. Now, I can search regardless of word order, but the results are not as specific as I need them to be. When I type "Correy" in the search bar, it shows all results containing that wo ...

jQuery Animation Issue: SlideDown Effect Not Working as Expected

I'm feeling quite confused about this situation. I've been attempting to utilize the slideDown function in jQuery, but when I click on the 'information' div, it just jumps instead of animating smoothly. I suspect that one of the cause ...

I'm experiencing some unexpected behavior in JavaScript when I try to apply style changes using JavaScript. Is it possible that transitions are not occurring in both cases as expected?

Everything seems to be working fine with the transition, but when I skip using setTimeout and directly apply the transform, the div instantly reaches the end of the transition. function move(x, y, delay) { let el = document.getElementById('myDiv& ...

Is having async as false really detrimental?

Splitting my inquiry into two sections. Within my website, I am dynamically generating some divs by utilizing ajax post requests to retrieve data from the database. Following is the structure of my setup. <html> <body> <script type=" ...

React Dilemma: Moving from Controlled to Uncontrolled Component

I am currently working on a FORM that should store the values in state upon submission. However, I'm encountering an issue while trying to achieve this. The problem is that I am only able to retrieve the value of the last input in my formcontrol, wh ...

Encountering a connection error while running a Node.js Selenium test case on Chrome. The specific error message received is: "Error: ECONNREFUSED - Connection refused to 127.0

When attempting to run a test case on a Selenium Node.js server, an error was encountered: Error: ECONNREFUSED connect ECONNREFUSED. The test case script is as follows: var assert = require('assert'), test = require('selenium-webdriver ...

A guide on extracting data from a Bootstrap table and removing a specific row

In my ejs file, I have a Bootstrap table set up as shown below. I am trying to implement a feature where clicking a button will trigger my del() function to delete the selected row. However, I am facing an issue where my function does not receive the &apos ...

Leverage the power of ssh2-promise in NodeJS to run Linux commands on a remote server

When attempting to run the command yum install <package_name> on a remote Linux server using the ssh2-promise package, I encountered an issue where I couldn't retrieve the response from the command for further processing and validation. I' ...

Struggling to make a basic JavaScript prompt function as expected

<html> <title>UniqueTitle</title> <head> <link rel="stylesheet" type="text/css" href="style.css" > <script type="text/javascript"> function modifyDates() { var dates = pr ...