在Debian系统中,如果你想要加载JavaScript模块,你可以使用Node.js或者浏览器环境。这里分别介绍两种方法:
方法一:使用Node.js
sudo apt update
sudo apt install nodejs
sudo apt install npm
app.js,并在其中编写你的代码。例如:const fs = require('fs');
fs.readFile('example.txt', 'utf-8', (err, data) => {
if (err) {
console.error(err);
return;
}
console.log(data);
});
node app.js
方法二:在浏览器环境中加载JavaScript模块
index.html,并在其中编写你的代码。例如:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Modules in Debian</title>
</head>
<body>
<script type="module" src="app.js"></script>
</body>
</html>
app.js,并在其中编写你的代码。例如:import { sayHello } from './hello.js';
sayHello('World');
hello.js,并在其中编写你的代码。例如:export function sayHello(name) {
console.log(`Hello, ${name}!`);
}
index.html文件,你将看到控制台输出"Hello, World!"。注意:在使用ES6模块语法(import和export)时,需要在HTML文件中的<script>标签上添加type="module"属性。