简体   繁体   中英

Cannot find module 'js' using express

Hi i'm trying to use express to present my react app as i will be using docker container however when i'm using res.render() it errors out saying Cannot find module 'js'.

import express from 'express';
import path from 'path';
import expressReactViews from 'express-react-views';

const app = express();
// Constants
const PORT = 8080;
const HOST = '0.0.0.0';
var __dirname = path.resolve();


app.set('view engine', 'jsx');
app.engine('jsx', expressReactViews.createEngine())


app.use(express.static(path.join(__dirname, "..", "public")));
app.use(express.static("public"));


app.use((req, res, next) => {
  res.render(path.join(__dirname, "..", "index.js"));
});

First check your package.json if it is included then try npm install . Second use common js instead of using import use require.

const express = require("express");
const path = require("path");

If its dont still work it maybe cant find the path location of index.js

You should be returning an HTML file if you're using React. You don't need a view engine, either.

    res.render(path.join(__dirname, "..", "index.html"));

where

".." 

is the path to your build folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM