我正在尝试托管我的网站,该网站在我的 raspberry PI 上使用 expressJS。我收到这个错误,我不知道该怎么做,特别是因为这不是我的代码。
TypeError: require(...) is not a function at Object.<anonymous> (/home/pi/node_modules/express/lib/router/route.js:16:29) at Module._compile (internal/modules/cjs/loader.js:778:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Module.require (internal/modules/cjs/loader.js:692:17) at require (internal/modules/cjs/helpers.js:25:18) at Object.<anonymous> (/home/pi/node_modules/express/lib/router/index.js:16:13) at Module._compile (internal/modules/cjs/loader.js:778:30)
我的代码:
const express = require('express');
const app = express();
console.log('required express');
const { readFileSync } = require('fs');
const { join } = require('path');
let domain = require('domain')
app.use((req, res, next) => {
var reqd = domain.create();
domain.active = reqd;
reqd.add(req);
reqd.add(res);
reqd.on('error', function(err) {
req.next(err);
});
res.on('end', function() {
console.log('disposing domain for url ' + req.url);
reqd.dispose();
});
reqd.run(next);
})
app.get('/public/styles.css', (req, res) => { res.sendFile('/home/pi/RSite/styles.css')
})
app.get('/public/navbar.js', (req, res) => { res.sendFile('/home/pi/RSite/navbar.js')
})
app.get('/', (req, res) => {
res.redirect('/home');
})
app.get('/home', (req, res) => {
res.send(readFileSync('index.html', 'utf-8'));
})
app.get('/forum', (req, res) => {
res.send(readFileSync('forum.html', 'utf-8'))
})
app.get('/project', (req, res) => {
res.send(readFileSync('project.html', 'utf-8'))
})
app.get('/videos', (req, res) => {
res.send(readFileSync('videos.html', 'utf-8'))
})
app.get('/other', (req, res) => {
res.send(readFileSync('other.html', 'utf-8'))
})
let port = 49481; // this is the only open port on my IP
console.log('listening on port ' + port);
app.listen(port, "0.0.0.0", () => {"Listening on port " + port}); // I use an actual IP address, that isn't the error.
更奇怪的是,它可以在我的笔记本电脑上运行,但不能在 raspberry pi 上运行。 我该怎么办?
答案 0 :(得分:0)
我有一个旧版本的 Node.js,它被列为稳定版,所以我更新了它。 现在工作正常。