Why "print" doesn't work in node.js?

13,851

Solution 1

The print or window.print() opens the Print Dialog to print the current document.

In Contrast, the console.log() outputs a message to the Web Console.

If your intention was to actually print a file to a 'hardware printer', from Node.js then have a look at projects such as node-printer.

Solution 2

window.print() opens the printer dialog of the browser to print the current page. It does not print something to the console. See the mdn docs. Hence it is not what you expect.

Share:
13,851
Daniel
Author by

Daniel

I write blogs in English https://preciselab.io and Polish https://gustawdaniel.com

Updated on June 24, 2022

Comments

  • Daniel
    Daniel almost 2 years

    I have JavsScript code with function print. It works correctly in console of web browser, but when I run this code using node i obtain error:

    ReferenceError: print is not defined
    

    Of course I can use console.log, but I would like to know why print is not implemented in node?

    • Maxx
      Maxx almost 8 years
      Provide some code, please.
    • Daniel
      Daniel almost 8 years
      Program with code: print("test"); gives error ReferenceError: print is not defined at Object.<anonymous> (/home/daniel/print.js:1:63) at Module._compile (module.js:410:26) at Object.Module._extensions..js (module.js:417:10) at Module.load (module.js:344:32) at Function.Module._load (module.js:301:12) at Function.Module.runMain (module.js:442:10) at startup (node.js:136:18) at node.js:966:3
    • Jaromanda X
      Jaromanda X almost 8 years
      what does print do in the console of the browser for you? ... because global print in a browser sends the current page to a printer - not sure how this can translate to a node.js application - what exactly would that send to a printer?
    • Jaromanda X
      Jaromanda X almost 8 years
      @Daniel - what would you like print to actually do?
    • Iceman
      Iceman almost 8 years
      @Daniel edit the code into your question, don't put them in comments.
    • Daniel
      Daniel almost 8 years
      This has nothing to do with printing by printer. I study javascript from probably to old book... I checked that in other one only "colsole.log" is used... Any way I was interested only if print is depreciated or maybe there is other reason.
  • Quentin
    Quentin over 5 years
    The question is asking why Node.js doesn't support the print function. Your answer is about installing SpiderMonkey (which is not Node.js) and doesn't mention the print function at all. Did you type your answer in the answer box on the wrong question?
  • Quentin
    Quentin over 5 years
    The question is asking why Node.js doesn't support the print function. Your answer is about installing SpiderMonkey (which is not Node.js) and doesn't mention the print function at all. Did you type your answer in the answer box on the wrong question?
  • noobninja
    noobninja almost 4 years
    Speaking of console.log(), was able to get console.log fooFunction(x) (without braces) to print to terminal emulator using a coffeescript interactive shell from the Debian repo. And used coffee command to run a script.