I'm using webpack, a node.js module, in order to bundle a node.js script for use in a browser. the module did what it was meant to, but i get a strange error when in the browser from index.js. The problem seems to be with index.js, but i'm not sure:
Uncaught SyntaxError: import not found: default
.
I'll provide code below.
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 (of the script i'm bundling)
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 file:
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'
},
};
import statement i'm using in index.js: import getWords from './wordpicker.js';
please let me know if you can help, or if you need any more information.
in webpack.config.js, add this property
target: "node",
also you have an error here:
const rw = require('random-words');
import rw from "random-words"
you cannot use require anymore
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.