Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read property 'once' of undefined #137

Closed
Lxxyx opened this issue Nov 30, 2016 · 11 comments
Closed

Cannot read property 'once' of undefined #137

Lxxyx opened this issue Nov 30, 2016 · 11 comments
Labels

Comments

@Lxxyx
Copy link

Lxxyx commented Nov 30, 2016

When start the project by express.

/Users/lxxyx/Desktop/own/yun-type/node_modules/electron-pdf/lib/index.js:32
electronApp.once('ready', () => {
^

TypeError: Cannot read property 'once' of undefined
at PDFExporter.start (/Users/lxxyx/Desktop/own/yun-type/node_modules/electron-pdf/lib/index.js:32:16)
at Object. (/Users/lxxyx/Desktop/own/yun-type/js_server/app.js:22:10)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3

import * as express from 'express'
import * as bodyParser from 'body-parser'
const ElectronPDF = require('electron-pdf')

const app = express()
app.use(bodyParser.json())

const exporter = new ElectronPDF()

app.post('/pdfexport', (req, res) => {
    const source:string = req.body.source
    exporter.createJob(source, './')
    .then((job) => {
        job.render()
        job.on('job-complete', (r) => {
            console.log('pdf files:', r.results)
        })
    })
})

exporter.on('charged', () => {
    //Only start the express server once the exporter is ready
    app.listen(3000)
})

exporter.start()
@codecounselor
Copy link
Collaborator

Can you post your scripts from package.json? I suspect that you are running your server with node, and you need to run it with electron.

@Lxxyx
Copy link
Author

Lxxyx commented Dec 9, 2016

@codecounselor Thank you. I run it at Node but not electron.

@javy1103
Copy link

javy1103 commented Sep 1, 2017

I get the same error when using electron to run the app. What am I doing wrong?

Here is my package.json

  "name": "pdfgenerator",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.17.2",
    "electron": "^1.7.5",
    "electron-pdf": "^1.3.0",
    "express": "^4.15.4"
  }
}

@codecounselor
Copy link
Collaborator

You should have a script that looks like this "start": "electron app.js"

@codecounselor
Copy link
Collaborator

Probably unrelated, but I see that you have electron 1.7.5 specified. You don't really need that, and in your case it could potentially cause problems because electron-pdf 1.3.0 uses 1.6.x

@javy1103
Copy link

javy1103 commented Sep 1, 2017

I tried both those suggestions.

Changed my package.json to look like:

{
  "name": "pdfgenerator",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "electron app.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.17.2",
    "electron-pdf": "^1.3.0",
    "electron": "1.6.5",
    "express": "^4.15.4"
  }
}

and my app.js like this:

var ElectronPDF = require('electron-pdf')
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
app.use(bodyParser.json())

var exporter = new ElectronPDF()

exporter.on('charged', () => {
    //Only start the express server once the exporter is ready
    app.listen(4000, function() {
        console.log(`Export Server running at http://${port}`);
    })
})
exporter.start()

Do I need to install electron globally in a specific way, dont know what is going wrong here since Im starting the app with electron.

I was debugging your lib/index.js file and logged the electron variable and got this path to a .exe
(\PDFGenerator\node_modules\electron-pdf\node_modules\electron\dist\electron.exe). Is that supposed to give me that path?

BTW thank you for your help

@javy1103
Copy link

javy1103 commented Sep 1, 2017

Do you have a working example somewhere?

@codecounselor
Copy link
Collaborator

That looks right to me, you are running npm start?
electron will be installed as a transitive dependency of electron-pdf and be available in ./node_modules/.bin. That is how your start script is able to execute it - you do not need it installed globally.

Which version of npm are you using? I think 3+ should dedup dependencies and electron should be in ./node_modules/electron and not nested under ./node_modules/electron-pdf/electron. Atleast that is what I am observing with NPM 4.x

Perhaps that is your problem.

@codecounselor
Copy link
Collaborator

And I don't have a public example of an API server, but perhaps I will publish one.

@javy1103
Copy link

javy1103 commented Sep 5, 2017

Is this package compatible with windows?

@codecounselor
Copy link
Collaborator

Yes it runs on windows, but you have to run npm install from the target OS or you won't get the right binaries

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants