简体   繁体   English

使用 express 找不到模块“js”

[英]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'.嗨,我正在尝试使用 express 来展示我的 react 应用程序,因为我将使用 docker 容器,但是当我使用res.render()时,它会出错,提示找不到模块“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 .首先检查您的 package.json 是否包含,然后尝试npm install Second use common js instead of using import use require.第二次使用普通js而不是使用import使用require。

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

If its dont still work it maybe cant find the path location of index.js如果它仍然不起作用,它可能无法找到 index.js 的路径位置

You should be returning an HTML file if you're using React.如果你使用 React,你应该返回一个 HTML 文件。 You don't need a view engine, either.您也不需要视图引擎。

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

where在哪里

".." 

is the path to your build folder.是构建文件夹的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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