问题描述
我想将我的NodeJS项目推送到heroku上,但是当我的项目在本地运行正常时,它不起作用。
当我输入“ heroku logs --tail”以查看错误时,我看到了:
#%rAML 1.0
---
title: Data API
baseUri: http://www.baseuri.com
version: v1
uses:
DataLib: libraries/data.raml
resourceTypes:
collection:
/<<name>>:
post:
description: |
Post Data To Order
body:
application/json:
properties:
siteId:
required: true
type: string
time:
type: integer
data: <<objectName>>
/objectOne:
type:
collection:
name: items
objectName: DataLib.DataOne
/objectTwo:
type:
collection:
name: items
objectName: DataLib.DataTwo
这是我的“ index.js”:
2020-08-18T10:39:44.396002+00:00 app[web.1]: const express = require('express')
2020-08-18T10:39:44.396003+00:00 app[web.1]: ^
2020-08-18T10:39:44.396003+00:00 app[web.1]:
2020-08-18T10:39:44.396004+00:00 app[web.1]: ReferenceError: require is not defined
这是完整的“ heroku日志--tail”:
const express = require('express')
if (process.env.NODE_ENV === 'dev') require('dotenv').config()
const routerIdeas = require('./routes/ideas.js')
const PORT = process.env.PORT || 5000
const app = express()
app.use(function (req,res,next) {
res.header("Access-Control-Allow-Origin","*")
res.header("Access-Control-Allow-Headers","Origin,X-Requested-With,Content-Type,Accept")
next()
})
app.use('/api',routerIdeas)
app.listen(PORT,() => {
console.log(`Server is running on port : ${PORT}`)
})
我尝试推送Heroku入门教程中提供的测试项目,“ require”没有问题,所以我不理解。
在此先感谢您能帮助我的人
解决方法
我遇到了同样的问题,我所做的是在 package.json
文件中删除了 type:module
并且它起作用了。
我已删除“ .git”和“ Procfile”文件。 我还删除了Heroku上的存储库并创建了一个新存储库。 然后,我再次启动该过程以将我的项目链接到新的存储库。 然后我推送了项目,现在没有更多错误了。
我不明白为什么,但是现在可以了。