Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

184
Visualizações
How can I call a javascript function in a module from HTML when the module has been bundled by webpack?

This is probably a basic question but for some reason Google won't give me the answer.

I have a file called dropzone.stub.js in my src/ folder

import { Dropzone } from "dropzone";
Dropzone.autoDiscover = true;

// Callback takes one argument and that is the file that was 
// added.
export default function createDropZone(selector, url, callback) {
  const myDropzone = new Dropzone(selector, { url: url });
  myDropzone.on("addedfile", callback);
}

Basically I am just trying to call it from with script tags within my HTML file.

My webpack bundler is configured as follows:

const path = require('path');

module.exports = {
  mode: 'development',
  entry: './src/dropzone.stub.js',
  output: {
    filename: 'dropzone.bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  module: {
    rules: [
      {
        test: /\.m?js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
      //      presets: ['@babel/preset-env', {targets: 'defaults'}]
          }
        }
      },
//      {
//        test: /\.css$/i,
//        use: ['style-loader', 'css-loader'],
//      },
      {
        test: /\.(png|svg|jpg|jpeg|gif)$/i,
        type: 'asset/resource',
      },
    ],
    // resolve: {
    //  extensions: [ '.js', '.jsx', '.css' ],
    //}
  },
};

The script section in my HTML browser at the moment just looks like this:

<script type="module">
  import { createDropZone } from '/static/dist/dropzone.bundle.js';

</script>

The browser reports the following error:

Uncaught SyntaxError: The requested module '/static/dist/dropzone.bundle.js' does not provide an export named 'createDropZone'

Any ideas anyone?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You are trying to import a webpack transpiled module from outside of webpack's scope which doesn't work. You really only have 2 options that I'm aware of. You can move your import statement from the html file to a javascript file such as app.js or something and run everything through webpack, or you would need to use babel along with webpack. I'd prefer the former but if that's not an option then maybe this will help point you in the right direction https://webpack.js.org/loaders/babel-loader/

about 4 years ago · Juan Pablo Isaza Relatório

0

What does the bundled file look like. Webpack is most likely not bundling it as a es module.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda