微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ReferenceError:我在Heroku上推送时未定义require

如何解决ReferenceError:我在Heroku上推送时未定义require

我想将我的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上的存储库并创建了一个新存储库。 然后,我再次启动该过程以将我的项目链接到新的存储库。 然后我推送了项目,现在没有更多错误了。

我不明白为什么,但是现在可以了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。