App在本地正常运行,但在Heroku上出现错误。

huangapple go评论84阅读模式
英文:

App works locally but getteting error on heroku

问题

这是您提供的代码的翻译部分:

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}`);
});

productRoutes.js 文件:

import express 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;

productController.js 文件:

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 };

请注意,这些是您提供的代码的翻译部分,不包括任何其他信息或回答您的问题。

英文:

I'm working on an amazon clone app from a turorial. It works locally and it will deploy to heroku with no errors but I get a Module Not Found for the productsController.

The file structure is

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

Here is the heroku error log:

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

Here is the 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}`);
});

Here is the productRoutes.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;

And here is the productController

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 };

答案1

得分: 0

更正路径不匹配。将controllers更改为controller,或者反之(取决于实际情况)。

  • 错误信息为:
    无法找到模块'/app/backend/controllers/productController.js'

  • 而你的路径是:
    /app/backend/controller/productController.js

英文:

You have a path mis-match. Change controllers to controller or vise-versa (whichever is true).

  • The error is:
    Cannot find module '/app/backend/controllers/productController.js'

  • and your path is:
    /app/backend/controller/productController.js

huangapple
  • 本文由 发表于 2023年1月6日 09:22:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定