ReferenceError: require is not defined with Express

问题描述 投票:0回答:3

我正在使用节点开发后端 app.js。我已经安装了 node,安装了 express,但是我在 npm start 时一直收到这个 ReferenceError。这是我当前的 app.js 代码:

const express = require("express");
const app = express();
const port = process.env.PORT || 4000;

我已经尝试了几乎所有我在谷歌上找到的东西。我改成:

import * as express from "express";

但这只给了我:

TypeError: express is not a function

我还有一个前端 React 应用程序。我不确定这是否有帮助。对不起,如果我的解释没有意义。我只是想弄清楚我的代码是不好还是其他原因。

node.js reactjs express require
3个回答
1
投票

你介意分享你的 package.json 或者你有你的代码在一些游乐场应用程序如 codepenfiddle


0
投票

您必须检查您的 package.json 文件以查看为“启动”编写的脚本是什么

它说的是

"start": "node app.js"
吗?尝试运行
node app.js
而不是
npm start


0
投票

这应该有效

const express = require('express')
const app = express()
const port = 5000

app.get('/', (req, res) => {
  res.send({hi: 'there'})
})

app.listen(端口, () => { 控制台日志(

Example app listening on port ${port}
) })

© www.soinside.com 2019 - 2024. All rights reserved.