无法在html网站中使用require()或从Node.js导入

问题描述 投票:0回答:1

我试图弄清楚如何做到这一点,但我放弃了。我正在尝试编写一些代码,允许用户访问我的 github 网站并在文本框中输入内容,然后网站将其输出存储到位于网站中的文本文件中。我发现你可以使用 node.js 模块“fs”将文本附加到该文件中,所以我尝试实现它。但无论我怎么尝试,它都不起作用。现在,它给了我错误“require() 不是一个函数”,我真的不明白问题是什么。任何帮助将不胜感激。

我的代码:(粘贴整个html文件,只是为了更好的帮助。)

<html><head>
    <meta property="og:title" content="OTFT: The Videogame Official Website">
    <meta property="og:description" content="The Official Website for OTFT!">
    <meta property="og:image:secure_url" content="https://otft.github.io/otft_tvg/images/OTFTSquare.png">
    <title>OTFT: The Videogame</title>
    <link rel="icon" href="../images/iconv1.png">
    <style>
    h1 {
    font-size: 50px;
    font-family: FlamingHope;
    }
    h2 {
    font-size: 40px;
    font-family: Blackentina;
    }
    body {
     padding:0;
      margin:0;
      width:100%;
      height:100%;
      background-image: url("../images/WebsiteBackground02.png");
      background-size: cover;
      background-repeat: repeat-y;
      background-position: center;
      background-color: rgba(0,0,0,0.6);
    }
    p {
      font-family: Tomorrow;
      font-size:25px;
    }
    text {
      font-family: Blackentina;
      font-size:70px;
      color:red;
      offset-position:50px;
    }
    a {
      font-family: Blackentina;
      font-size: 30px;
      color:red;
    }
    div {
        display: block;
        unicode-bidi: isolate;
    }
    @font-face {
        font-family: FlamingHope;
        src: url("../FlamingHope.ttf") format("opentype");
    }
    @font-face {
        font-family: AnotherDanger;
        src: url("../another danger.otf") format("opentype");
    }
    @font-face {
        font-family: AnotherDanger;
        font-weight: italics;
        src: url("../another danger italics.otf") format("opentype");
    }
    @font-face {
        font-family: Blackentina;
        src: url("../Blackentina 4F.ttf") format("opentype");
    }
    @font-face {
        font-family: Blackentina;
        font-weight: italics;
        src: url("../Blackentina 4F-Italic.ttf") format("opentype");
    }
    @font-face {
        font-family: Tomorrow;
        src: url("../Tomorrow.ttf") format("opentype");
    }
    @font-face {
        font-family: Tomorrow;
        font-weight: italics;
        src: url("../Tomorrow-Italic.ttf") format("opentype");
    }
    @font-face {
        font-family: Tomorrow;
        font-weight: bold;
        src: url("../Tomorrow-Bold.ttf") format("opentype");
    }
    </style>
    </head><body><center><div id="headerthingie" style="background-color: black;color: white;display: flex;justify-content: center;align-items: center;flex-direction: column;"><h1 style="font-size:40px;">OTFT: The Videogame</h1><template id="weblinksDiv"></template><script type="text/javascript" src="../reusedivfix.js"></script></div>
        <div style="width:700px;display: block;"><h1>FAQ</h1>
    <p>For now, the FAQ will be stored in a file that I will check for new entries! Later on, I will update the website to display the questions.</p>

    <textarea id="inputQuestion" rows="4" cols="50" placeholder="Question goes here..."></textarea>
    <br>
    <input type="submit" value="Submit" onclick="submitData()">
    </center>
    <br>
    <div id="footerthingie" style="background-color: black;color: white;display: flex;justify-content: center;align-items: center;flex-direction: column;position: relative;bottom: 0px;width: 100%;"><template id="weblinksDiv01"></template><script type="text/javascript" src="../reusablesubdiv.js"></script><p style="height:20px;font-size:20px;">ⓒ OTFT Studios 2024</p></div>
    </body>
    <script type="module">
        window.submitData = submitData();
        
        import * as require from 'https://requirejs.org/docs/release/2.3.5/minified/require.js';
        
        function submitData() {

        var fs = require('node:fs');

        let newData = "\n" + document.getElementById("inputQuestion").value;

        fs.appendFile('storeduserdata.txt', newData, (err) => {

        if (err) throw err;
        })
    }
    </script>
    </html>

我尝试了一些事情,比如仔细检查拼写,或者我的分号,或者重新组织一切的去向。我还咨询了谷歌,试图找到解决这个问题的方法,但现在我被困在这里了。

javascript html node.js github web
1个回答
0
投票

理解你做错了什么的关键是记住 CommonJS (CJS) 的目的:它的创建是为了允许 JavaScript 在网络浏览器的外部执行!

浏览器仅理解 ECMAScript JavaScript 语法。

CJS 使用

module.exports
require()
,而 ESM 使用
export
/
export default
import

在浏览器中使用

fs-extra
包的正确方法是添加以下内容:

<script type="module">
  import fsExtra from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm
</script>

或者这个:

<script src=" https://cdn.jsdelivr.net/npm/[email protected]/lib/index.min.js "></script>

您应该选择第一个选项,因为您正在使用模块。

您可以在应用程序的后端和前端使用

fs-extra
模块。欲了解更多信息,请查看下面的链接。

© www.soinside.com 2019 - 2024. All rights reserved.