簡體   English   中英

未找到導入:使用 webpack 進行 web 開發時出現默認錯誤

[英]import not found: default error when using webpack for web development

我正在使用 webpack,一個 node.js 模塊,以便捆綁 node.js 腳本以在瀏覽器中使用。 該模塊完成了它的本意,但是在瀏覽器中從 index.js 中出現一個奇怪的錯誤。 問題似乎出在 index.js 上,但我不確定:

Uncaught SyntaxError: import not found: default

我將在下面提供代碼。

HTML:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Typing Practice</title>
        <link rel="stylesheet" href="style.css">
        <script src="index.js" type="module" defer></script>
        <script src="wordpicker.js" type="module" defer></script>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <section id="top-container" class="top-bar">
            <h1 class="heading">Typing Practice</h1>
        </section>
        <section id="inpContainer" class="input-container">
            <div class="centeralign">
                <br><br>
                <h2 id="output" class="paragraph"></h2>
                <input type="text" id="input" class="text-field"><br>
                <button type="button" id="start" class="button">Start</button>
            </div>
        </section>
    </body>
</html>

JAVASCRIPT(我正在捆綁的腳本)

const rw = require('random-words');

function getWords() {
    let words = [];

    while (words.length != 10) {
        let word = rw();
        if (word.length < 6) continue;
        else if (word.length > 10) continue;
        else words.push(word)
    }
    return words;
}

export default getWords();

webpack.config.js 文件:

const path = require('path');

module.exports = {
  entry: './src/wordchecker.js',
  output: {
    filename: 'wordpicker.js',
    path: path.resolve(__dirname, 'dist'),
    library: 'typingpractice',
    libraryTarget: 'window',
    libraryExport: 'default'
  },
};

我在 index.js 中使用的導入語句: import getWords from './wordpicker.js';

如果您可以提供幫助,或者您需要更多信息,請告訴我。

在 webpack.config.js 中,添加此屬性

  target: "node",

你也有一個錯誤:

 const rw = require('random-words');
 import rw from "random-words" 

你不能再使用 require

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM