leo / electron-next

Build Electron apps using Next.js
https://github.com/leo/site/blob/beef3a7dc1dfd435a9d8377e3b1b59761ccb7fc7/pages/2017/electron-next.js
MIT License
548 stars 41 forks source link

"TypeError: Cannot read property 'send' of undefined" when running next export #21

Open tom-sherman opened 5 years ago

tom-sherman commented 5 years ago

When I run electron . the app works perfectly, however when I run next build renderer && next export renderer (specifically the next export command) I get the following log:

Note: all of my client code lives in /renderer

[00:22:52] Compiling client
[00:22:52] Compiling server
[00:22:52] Compiled server in 792ms
[00:22:55] Compiled client in 4s
> using build directory: D:\klipped\renderer\.next
  copying "static build" directory
> exporting path: /start
> exporting path: /file
TypeError: Cannot read property 'send' of undefined
    at D:\klipped\renderer\.next\server\static\lXCFzg2cjgmkVrnUKjqkQ\pages\file.js:358:39
    at new Promise (<anonymous>)
    at resolveFilePath (D:\klipped\renderer\.next\server\static\lXCFzg2cjgmkVrnUKjqkQ\pages\file.js:357:10)
    at Function._callee$ (D:\klipped\renderer\.next\server\static\lXCFzg2cjgmkVrnUKjqkQ\pages\file.js:307:24)
    at tryCatch (D:\klipped\node_modules\regenerator-runtime\runtime.js:62:40)
    at Generator.invoke [as _invoke] (D:\klipped\node_modules\regenerator-runtime\runtime.js:288:22)
    at Generator.prototype.(anonymous function) [as next] (D:\klipped\node_modules\regenerator-runtime\runtime.js:114:21)
    at asyncGeneratorStep (D:\klipped\renderer\.next\server\static\lXCFzg2cjgmkVrnUKjqkQ\pages\file.js:228:103)
    at _next (D:\klipped\renderer\.next\server\static\lXCFzg2cjgmkVrnUKjqkQ\pages\file.js:230:194)
    at D:\klipped\renderer\.next\server\static\lXCFzg2cjgmkVrnUKjqkQ\pages\file.js:230:364

Export successful

Here's my code:

next.config.js

exports.webpack = config => Object.assign(config, {
  target: 'electron-renderer'
})

exports.exportPathMap = () => ({
  '/start': { page: '/start' },
  '/file': { page: '/file', query: { data: null, name: null } }
})

file.js

import { ipcRenderer } from 'electron'
import { Component } from 'react'
import { FileDraggable } from '../components/file-draggable'
import Error from 'next/error'

export default class File extends Component {
  static async getInitialProps ({ query }) {
    let filePath

    try {
      if (query.name) {
        filePath = await resolveFilePath({ data: query.data, name: query.name })
      } else {
        filePath = await resolveFilePath({ data: query.data })
      }
    } catch (err) {
      console.error(err)
      return { error: 'Failed to save file.' }
    }

    return { filePath, data: query.data, name: query.name }
  }

  render () {
    return this.props.error
      ? <Error statusCode={500} /> // TODO: Custom error component
      : <FileDraggable filePath={this.props.filePath} name={this.props.name} />
  }
}

function resolveFilePath ({ data, name }) {
  return new Promise((resolve, reject) => {
    ipcRenderer.send('data', { data, name })
    ipcRenderer.on('file', (_, filePath) => resolve(filePath))
    ipcRenderer.on('error', err => reject(err))
  })
}
apolkingg8 commented 5 years ago

I think that's the reason https://stackoverflow.com/questions/45274548/node-js-require-returns-a-string-instead-of-module-object

But not sure how to solve it. @tom-sherman did you resolved it?