无法在js中加载模块

cannot load modules in js

当我尝试使用 require 加载模块时,它在终端中工作正常但在浏览器中不起作用: const express = require("express");

Uncaught ReferenceError: require is not defined

然后我尝试了 import express from 'express';,它说:

Uncaught SyntaxError: Cannot use import statement outside a module

然后有人建议将 type="module" 添加到我的脚本中,它抛出另一个错误:

Uncaught TypeError: Failed to resolve module specifier "express". Relative references must start with either "/", "./", or "../"

基本上,当我尝试加载模块时,它不会在 vs code 终端上抛出任何错误,但会在浏览器控制台中抛出

// JS

const express = require("express");

// Package.json
{
  "dependencies": {
    "express": "^4.17.1",
    "p": "^0.2.0",
    "requirejs": "^2.3.6"
  },
  "name": "fetching-data",
  "version": "1.0.0",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "description": ""
}
head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script src="main.js" type="module"></script>
</body>
</html>

Nodejs 应用不同于静态网站。

将脚本加载到 html 文件中不起作用。

读这个Nodejs 开始节点开发。