I can't find solution for days, Nextjs and Nodejs Together problem #53927
Answered
by
icyJoseph
muhammedalibilgin
asked this question in
Help
Summaryserver1.js and server2.js is the same but server1.js is not work. Additional informationi created "create-next-app" and then server2.js create into this.
"node server2" is succes no problem but server1.js is not work.
server1.js == server2.js same.
just diffrent between add "dir" in the " const app = next({ dev, dir: "./client2" }); "Example |
Answered by
icyJoseph
Aug 13, 2023
Replies: 2 comments 3 replies
|
Example: const path = require("path");
const express = require("express");
const next = require("next");
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev, dir: path.resolve('client2') });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = express();
server.get("*", (req, res) => {
return handle(req, res);
});
server.listen(3000, (err) => {
if (err) throw err;
console.log("> Ready on http://localhost:3000");
});
}); |
2 replies
|
You need to fix how you access const express = require("./client2/node_modules/express");
const next = require("./client2/node_modules/next");
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev, dir: "./client2" });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = express();
server.get("*", (req, res) => {
return handle(req, res);
});
server.listen(3000, (err) => {
if (err) throw err;
console.log("> Ready on http://localhost:3000");
});
});Notice the first two lines. Since neither express, or next, are native Node packages, the Node.js module resolution algorithm won't find them. In this case I pointed to the express and next, contained within client2. |
1 reply
Answer selected by
muhammedalibilgin
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need to fix how you access
expressandnext, for example:Notice the first two lines. Since neither express, or next, are native Node packages, the Node.js module resolution algorithm won't find th…