简体   繁体   English

JS 模块/导入节点和 ejs 的错误

[英]JS module/imports errors with node and ejs

I'm having problem linking/importing my javascript files in my node-project.我在节点项目中链接/导入我的 javascript 文件时遇到问题。 I'm trying to link to a public javascript map from my ejs- file.我正在尝试从我的 ejs- 文件链接到公共 javascript 地图。 In my project so far, I have made due by writing my javascript within the code in the ejs/html- script tags, but it doesn't seem to be able to import.到目前为止,在我的项目中,我已经通过在 ejs/html-script 标记中的代码中编写我的 javascript,但它似乎无法导入。 Since I would like to import from different .js-files, I find myself struggling.由于我想从不同的 .js 文件导入,我发现自己很挣扎。

This is from app.js:这是来自 app.js:

const express = require('express');
const app = express();
app.set('view engine', 'ejs');
app.use(express.static('public'));```

this is from my taskboard.ejs:这是来自我的 taskboard.ejs:

<script src="/public/javascript/loggedinFunctions/taskboard/api/kanbanAPI.js" type="module">
  </script>
  <script>
  import KanbanAPI from "/public/javascript/api/kanbanAPI.js"
  console.log(KanbanAPI.getItems(1));
</script>

Here is my file structure: enter image description here这是我的文件结构:在此处输入图像描述

I have double-checked that the linking (ie /public/javascript..) works in Visual Studio Code by click+c (to open it), so it's not an issue of (../) or (./).我已经通过click+c(打开它)仔细检查了链接(即/public/javascript..)在Visual Studio Code中是否有效,所以这不是(../)或(./)的问题。

The errors I get on the browser (Firefox):我在浏览器(Firefox)上遇到的错误:

GEThttp://localhost:3000/public/javascript/loggedinFunctions/taskboard/api/kanbanAPI.js
[HTTP/1.1 404 Not Found 3ms]

Loading module from “http://localhost:3000/public/javascript/loggedinFunctions/taskboard/api/kanbanAPI.js” was blocked because of a disallowed MIME type (“text/html”).

Uncaught SyntaxError: import declarations may only appear at top level of a module

When I skip the type="module" in the script tag, I cannot link it either in the browser.当我跳过脚本标签中的 type="module" 时,我也无法在浏览器中链接它。 In both cases, it throws the 404 Not Found.在这两种情况下,它都会抛出 404 Not Found。 I have read about type="module" as a way to circumvent the browser misreading the type of the document, but it doesn't seem to work in either case.我已经阅读了关于 type="module" 作为一种规避浏览器误读文档类型的方法,但它似乎在这两种情况下都不起作用。 If I add (../) to the src like so:如果我像这样将 (../) 添加到 src:

<script src="../public/javascript/loggedinFunctions/taskboard/api/kanbanAPI.js">

it is not found either, which makes me believe that there is maybe something wrong with the routing(?) Here is my map structure for the moment: enter image description here它也没有找到,这让我相信路由可能有问题(?)这是我目前的地图结构:在此处输入图像描述

I also have this in app.js:我在 app.js 中也有这个:

app.use((req, res, next) => {
  res.locals.path = req.path;
  next();
});

And this is the top code in kanbanAPI.js:这是 kanbanAPI.js 中的顶级代码:

export default class KanbanAPI {
  static getItems(columnId){
    const kanbanColumn = read().find(column => column.id == columnId);
    if (!column){
      return [];
    }
  }
}

when you use a folder name as static , it's implied that it will look inside that folder, meaning that you don't need to write public when looking for a file, but just the path inside the folder当您将文件夹名称用作 static 时,这意味着它将在该文件夹内查找,这意味着您在查找文件时不需要写public ,而只需写文件夹内的路径

<script type="module">
 import KanbanAPI from "/javascript/api/kanbanAPI.js"
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM