NodeJS 应用程序在本地工作,但在heroku上出现设置错误

x6yk4ghg  于 2023-01-08  发布在  Node.js
关注(0)|答案(1)|浏览(109)

我正在开发一个亚马逊的克隆应用程序,它可以在本地运行,并且可以部署到heroku,没有任何错误,但是我得到了一个productsController的模块。
文件结构为

├── backend
|   ├── config
│   ├── controllers
│   │   ├── productController.js
│   ├── models
│   ├── routes
│   │   ├── productRoutes.js
│   ├── server.js

下面是heroku错误日志:

2023-01-06T01:09:09.693860+00:00 app[web.1]: ^
2023-01-06T01:09:09.693860+00:00 app[web.1]:
2023-01-06T01:09:09.693861+00:00 app[web.1]: Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/app/backend/controllers/productController.js' imported from /app/backend/routes/productRoutes.js
2023-01-06T01:09:09.693861+00:00 app[web.1]: at new NodeError (node:internal/errors:393:5)
2023-01-06T01:09:09.693862+00:00 app[web.1]: at finalizeResolution (node:internal/modules/esm/resolve:323:11)
2023-01-06T01:09:09.693862+00:00 app[web.1]: at moduleResolve (node:internal/modules/esm/resolve:916:10)
2023-01-06T01:09:09.693862+00:00 app[web.1]: at defaultResolve (node:internal/modules/esm/resolve:1124:11)
2023-01-06T01:09:09.693863+00:00 app[web.1]: at nextResolve (node:internal/modules/esm/loader:163:28)
2023-01-06T01:09:09.693863+00:00 app[web.1]: at ESMLoader.resolve (node:internal/modules/esm/loader:841:30)
2023-01-06T01:09:09.693863+00:00 app[web.1]: at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
2023-01-06T01:09:09.693864+00:00 app[web.1]: at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
2023-01-06T01:09:09.693875+00:00 app[web.1]: at link (node:internal/modules/esm/module_job:75:36) {
2023-01-06T01:09:09.693875+00:00 app[web.1]: code: 'ERR_MODULE_NOT_FOUND'
2023-01-06T01:09:09.693875+00:00 app[web.1]: }
2023-01-06T01:09:09.693880+00:00 app[web.1]:
2023-01-06T01:09:09.693880+00:00 app[web.1]: Node.js v18.12.1
2023-01-06T01:09:09.881953+00:00 heroku[web.1]: Process exited with status 1       
2023-01-06T01:09:09.926097+00:00 heroku[web.1]: State changed from starting to crashed
2023-01-06T01:09:09.936162+00:00 heroku[web.1]: State changed from crashed to starting
2023-01-06T01:09:18.922072+00:00 heroku[web.1]: Starting process with command `npm start`
2023-01-06T01:09:20.986680+00:00 app[web.1]:
2023-01-06T01:09:20.986716+00:00 app[web.1]: > amazona-2@1.0.0 start
2023-01-06T01:09:20.986716+00:00 app[web.1]: > node backend/server.js
2023-01-06T01:09:20.986717+00:00 app[web.1]:
2023-01-06T01:09:21.087388+00:00 app[web.1]: node:internal/errors:484
2023-01-06T01:09:21.087389+00:00 app[web.1]: ErrorCaptureStackTrace(err);
2023-01-06T01:09:21.087390+00:00 app[web.1]: ^
2023-01-06T01:09:21.087390+00:00 app[web.1]:
2023-01-06T01:09:21.087391+00:00 app[web.1]: Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/app/backend/controllers/productController.js' imported from /app/backend/routes/productRoutes.js
2023-01-06T01:09:21.087391+00:00 app[web.1]: at new NodeError (node:internal/errors:393:5)
2023-01-06T01:09:21.087392+00:00 app[web.1]: at finalizeResolution (node:internal/modules/esm/resolve:323:11)
2023-01-06T01:09:21.087392+00:00 app[web.1]: at moduleResolve (node:internal/modules/esm/resolve:916:10)
2023-01-06T01:09:21.087392+00:00 app[web.1]: at defaultResolve (node:internal/modules/esm/resolve:1124:11)
2023-01-06T01:09:21.087393+00:00 app[web.1]: at nextResolve (node:internal/modules/esm/loader:163:28)
2023-01-06T01:09:21.087393+00:00 app[web.1]: at ESMLoader.resolve (node:internal/modules/esm/loader:841:30)
2023-01-06T01:09:21.087393+00:00 app[web.1]: at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
2023-01-06T01:09:21.087394+00:00 app[web.1]: at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
2023-01-06T01:09:21.087395+00:00 app[web.1]: at link (node:internal/modules/esm/module_job:75:36) {
2023-01-06T01:09:21.087396+00:00 app[web.1]: code: 'ERR_MODULE_NOT_FOUND'
2023-01-06T01:09:21.087396+00:00 app[web.1]: }
2023-01-06T01:09:21.087426+00:00 app[web.1]:
2023-01-06T01:09:21.087427+00:00 app[web.1]: Node.js v18.12.1
2023-01-06T01:09:21.248317+00:00 heroku[web.1]: Process exited with status 1       
2023-01-06T01:09:21.315476+00:00 heroku[web.1]: State changed from starting to crashed
2023-01-06T01:09:23.209214+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=mern-amazona2-judo2000.herokuapp.com request_id=dd865b46-6a9f-478d-87ea-bfef1a20773d fwd="136.144.43.249" dyno= connect= service= status=503 bytes= protocol=https
2023-01-06T01:09:23.521669+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=mern-amazona2-judo2000.herokuapp.com request_id=2c983834-c3e2-4a60-b39a-f55ecf4fa2fd fwd="172.98.33.99" dyno= connect= service=
 status=503 bytes= protocol=https
2023-01-06T01:11:30.804412+00:00 heroku[web.1]: State changed from crashed to starting
2023-01-06T01:11:39.211361+00:00 heroku[web.1]: Starting process with command `npm start`
2023-01-06T01:11:41.347517+00:00 app[web.1]:
2023-01-06T01:11:41.347544+00:00 app[web.1]: > amazona-2@1.0.0 start
2023-01-06T01:11:41.347544+00:00 app[web.1]: > node backend/server.js
2023-01-06T01:11:41.347545+00:00 app[web.1]:
2023-01-06T01:11:41.460279+00:00 app[web.1]: node:internal/errors:484
2023-01-06T01:11:41.460280+00:00 app[web.1]: ErrorCaptureStackTrace(err);
2023-01-06T01:11:41.460281+00:00 app[web.1]: ^
2023-01-06T01:11:41.460281+00:00 app[web.1]:
2023-01-06T01:11:41.460281+00:00 app[web.1]: Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/app/backend/controllers/productController.js' imported from /app/backend/routes/productRoutes.js
2023-01-06T01:11:41.460282+00:00 app[web.1]: at new NodeError (node:internal/errors:393:5)
2023-01-06T01:11:41.460282+00:00 app[web.1]: at finalizeResolution (node:internal/modules/esm/resolve:323:11)
2023-01-06T01:11:41.460283+00:00 app[web.1]: at moduleResolve (node:internal/modules/esm/resolve:916:10)
2023-01-06T01:11:41.460283+00:00 app[web.1]: at defaultResolve (node:internal/modules/esm/resolve:1124:11)
2023-01-06T01:11:41.460283+00:00 app[web.1]: at nextResolve (node:internal/modules/esm/loader:163:28)
2023-01-06T01:11:41.460285+00:00 app[web.1]: at ESMLoader.resolve (node:internal/modules/esm/loader:841:30)
2023-01-06T01:11:41.460285+00:00 app[web.1]: at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
2023-01-06T01:11:41.460285+00:00 app[web.1]: at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
2023-01-06T01:11:41.460285+00:00 app[web.1]: at link (node:internal/modules/esm/module_job:75:36) {
2023-01-06T01:11:41.460286+00:00 app[web.1]: code: 'ERR_MODULE_NOT_FOUND'
2023-01-06T01:11:41.460286+00:00 app[web.1]: }
2023-01-06T01:11:41.460297+00:00 app[web.1]:
2023-01-06T01:11:41.460298+00:00 app[web.1]: Node.js v18.12.1
2023-01-06T01:11:41.609123+00:00 heroku[web.1]: Process exited with status 1       
2023-01-06T01:11:41.742528+00:00 heroku[web.1]: State changed from starting to crashed
2023-01-06T01:11:42.461013+00:00 heroku[web.1]: State changed from crashed to starting
2023-01-06T01:11:52.618745+00:00 heroku[web.1]: Starting process with command `npm start`
2023-01-06T01:11:54.470268+00:00 app[web.1]:
2023-01-06T01:11:54.470310+00:00 app[web.1]: > amazona-2@1.0.0 start
2023-01-06T01:11:54.470311+00:00 app[web.1]: > node backend/server.js
2023-01-06T01:11:54.470311+00:00 app[web.1]:
2023-01-06T01:11:54.565973+00:00 app[web.1]: node:internal/errors:484
2023-01-06T01:11:54.565975+00:00 app[web.1]: ErrorCaptureStackTrace(err);
2023-01-06T01:11:54.565975+00:00 app[web.1]: ^
2023-01-06T01:11:54.565976+00:00 app[web.1]:
2023-01-06T01:11:54.565977+00:00 app[web.1]: Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/app/backend/controllers/productController.js' imported from /app/backend/routes/productRoutes.js
2023-01-06T01:11:54.565986+00:00 app[web.1]: at new NodeError (node:internal/errors:393:5)
2023-01-06T01:11:54.565986+00:00 app[web.1]: at finalizeResolution (node:internal/modules/esm/resolve:323:11)
2023-01-06T01:11:54.565986+00:00 app[web.1]: at moduleResolve (node:internal/modules/esm/resolve:916:10)
2023-01-06T01:11:54.565987+00:00 app[web.1]: at defaultResolve (node:internal/modules/esm/resolve:1124:11)
2023-01-06T01:11:54.565987+00:00 app[web.1]: at nextResolve (node:internal/modules/esm/loader:163:28)
2023-01-06T01:11:54.565987+00:00 app[web.1]: at ESMLoader.resolve (node:internal/modules/esm/loader:841:30)
2023-01-06T01:11:54.565987+00:00 app[web.1]: at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
2023-01-06T01:11:54.565993+00:00 app[web.1]: Node.js v18.12.12023-01-06T01:11:54.697714+00:00 heroku[web.1]: Process exited with status 1       
2023-01-06T01:11:54.792281+00:00 heroku[web.1]: State changed from starting to crashed
2023-01-06T01:11:55.983110+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=mern-amazona2-judo2000.herokuapp.com request_id=e373b75d-6a72-432d-9709-29e6c62063bd fwd="173.239.224.8" dyno= connect= service= status=503 bytes= protocol=https
2023-01-06T01:11:56.274300+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=mern-amazona2-judo2000.herokuapp.com request_id=368733e0-a442-422d-97a1-1c3e0bc7681f fwd="173.239.224.10" dyno= connect= service= status=503 bytes= protocol=https
2023-01-06T01:11:30.571160+00:00 app[api]: Release v21 created by user judo2000@comcast.net
2023-01-06T01:11:30.571160+00:00 app[api]: Remove MONGO_URI config vars by user judo2000@comcast.net
2023-01-06T01:11:42.194061+00:00 app[api]: Set MONGODB_URI config vars by user judo2000@comcast.net
2023-01-06T01:11:42.194061+00:00 app[api]: Release v22 created by user judo2000@comcast.net

下面是server.js

import express from 'express';
import path from 'path';
import dotenv from 'dotenv';
import connectDB from './config/connection.js';

import seedRoutes from './routes/seedRoutes.js';
import productRoutes from './routes/productRoutes.js';
import userRoutes from './routes/userRoutes.js';
import orderRoutes from './routes/orderRoutes.js';

dotenv.config();

connectDB();

const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.get('/api/keys/paypal', (req, res) => {
  res.send(process.env.PAYPAL_CLIENT_ID || 'sb');
});

app.use('/api/seed', seedRoutes);
app.use('/api/products', productRoutes);
app.use('/api/users', userRoutes);
app.use('/api/orders', orderRoutes);

const __dirname = path.resolve();
app.use(express.static(path.join(__dirname, '/frontend/build')));

app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, '/frontend/build/index.html'));
});

app.use((err, req, res, next) => {
  res.status(500).send({ message: err.message });
});

const port = process.env.PORT || 5000;
app.listen(port, () => {
  console.log(`Server listening at http://localhost:${port}`);
});

下面是产品Routes.js

import express, { Router } from 'express';
const router = express.Router();
import {
  getProducts,
  getProductBySlug,
  getProductById,
} from '../controllers/productController.js';

router.route('/').get(getProducts);
router.route('/slug/:slug').get(getProductBySlug);
router.route('/:id').get(getProductById);

export default router;

这是产品控制器

import Product from '../models/ProductModel.js';

const getProducts = async (req, res) => {
  const products = await Product.find({});
  res.send(products);
};

const getProductBySlug = async (req, res) => {
  const product = await Product.findOne({ slug: req.params.slug });

  if (product) {
    res.send(product);
  } else {
    res.status(404).send({ message: 'Product Not Found' });
  }
};

const getProductById = async (req, res) => {
  const product = await Product.findById(req.params.id);
  if (product) {
    res.send(product);
  } else {
    res.status(404).send({ message: 'Product not found.' });
  }
};

export { getProducts, getProductBySlug, getProductById };
h5qlskok

h5qlskok1#

路径不匹配。请将controllers更改为controller,反之亦然(以正确者为准)。

  • 错误为:Cannot find module '/app/backend/controllers/productController.js'
  • 你的道路是:/app/backend/controller/productController.js

相关问题