You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

HTML引入etherjs.js和web3-min.js失败求助(Node本地服务器)

Troubleshooting Failed JS File Imports in Local Node Server

Hey there, let's work through this script loading issue together! I’ve seen this exact problem pop up a lot, so here are the most likely culprits and how to fix them:

1. Double-check your script tag paths in main.html

First off, make sure your relative paths are correct since the JS files are in the same directory as your HTML. You should be using either:

<script src="etherjs.js"></script>
<script src="web3-min.js"></script>

or explicitly using the current directory indicator:

<script src="./etherjs.js"></script>
<script src="./web3-min.js"></script>
  • Watch for typos: Even a tiny mismatch (like etherJS.js vs etherjs.js) will break things, especially on Linux/macOS where file names are case-sensitive.
  • Avoid absolute paths: If you used something like /etherjs.js, that points to the root of your server, which might not align with your HTML file’s directory.

2. Ensure your Node server is set up to serve static files

If you’re using a framework like Express, you need to explicitly tell it to serve static assets. Add this line to your server code:

const express = require('express');
const app = express();

// Serve files from the current directory (where main.html lives)
app.use(express.static(__dirname));

// ... rest of your server setup

If you’re using the raw http module without a framework, you have to handle file requests manually. Make sure your server reads the JS files correctly and sends the right Content-Type header:

const http = require('http');
const fs = require('fs');
const path = require('path');

http.createServer((req, res) => {
  let filePath = path.join(__dirname, req.url === '/' ? 'main.html' : req.url);
  const ext = path.extname(filePath);
  let contentType = 'text/html';
  if (ext === '.js') contentType = 'application/javascript';

  fs.readFile(filePath, (err, content) => {
    if (err) {
      res.writeHead(404);
      res.end('File not found');
    } else {
      res.writeHead(200, { 'Content-Type': contentType });
      res.end(content, 'utf-8');
    }
  });
}).listen(3000);

Without this setup, your server won’t know how to respond to requests for .js files.

3. Dig into the browser console errors

Open your browser’s DevTools (F12) and check the Network tab:

  • A 404 Not Found error means your server can’t locate the JS files. Go back to verify your paths and server static file configuration.
  • A 403 Forbidden error points to file permission issues—make sure your Node process has read access to etherjs.js and web3-min.js.
  • A MIME type error (like "Refused to execute script from '...' because its MIME type ('text/plain') is not executable") means your server is sending the wrong Content-Type header for JS files. Fix this by adding the correct header as shown in the raw http server example above.

4. Force a browser cache refresh

Sometimes browsers hang onto old failed requests. Hit Ctrl+Shift+R (Windows/Linux) or Cmd+Shift+R (macOS) to do a hard refresh, which will bypass the cache and reload all resources fresh.


内容的提问来源于stack exchange,提问作者Itération 122442

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:56:47