Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
86 views
in Technique[技术] by (71.8m points)

node.js - require(...) is not a function ExpressJS

I'm trying to host my website which uses expressJS on my raspberry PI. I get this error, I don't know what to do especially since this isn't my code.

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)

My Code:

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.

What's even more strange is that it works on my laptop, but not on the raspberry pi. What should I do?

question from:https://stackoverflow.com/questions/65868028/require-is-not-a-function-expressjs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I had an old version of Node.js that was listed as stable, so I updated it. Works fine now.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...