4

I am having trouble getting the simplest Javascript source block in org-mode to run.

  #+begin_src js
    console.log("Hello");
  #+end_src

I always get this error:

/tmp/babel-8kqQwQ/js-script-shBzKH:1
require('sys').print(require('sys').inspect(function(){
               ^

TypeError: require(...).print is not a function
    at Object.<anonymous> (/tmp/babel-8kqQwQ/js-script-shBzKH:1:16)
    at Module._compile (internal/modules/cjs/loader.js:1151:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1171:10)
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

I am running on Spacemacs and did the configuration as described in the Org Wiki https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-js.html

OS is the latest Manjaro. Emacs 26.3

The funny thing is, I am running my Emacs on my other machine with the exact same config. Same OS, same Emacs, same .spacemacs file. No issues.
I even did a fresh install of Spacemacs with the current config template on both systems. Same result. The desktop works, the notebook doesn't.

I can only imagine that an external JS runner is missing or broken?

I really don't know how to debug this.

  • Can you try adding a :results output header on the code block? That worked for me but I would second @wasamasa's suggestion of reporting a bug. – NickD Feb 23 at 1:41
5

Looks like a bug as wasamasa said, but until that gets fixed this works for me:

(setq org-babel-js-function-wrapper
      "console.log(require('util').inspect(function(){\n%s\n}(), { depth: 100 }))")

If you want to print deeper or otherwise modify output, change the options for require('util').inspect(...) in that string, as documented here.

If you want to print a value into org without explicitly calling console.log in your own source block, put a return statement in your source block:

#+BEGIN_SRC js
  return 1 + 1;
#+END_SRC

#+RESULTS:
: 2
| improve this answer | |
  • you saved my day. I hunted everywhere for this. – Anuvrat Parashar May 11 at 21:04
2

Org supports evaluating JavaScript either via Node.js or mozrepl. Your error message suggests you're using Node, but your current version of it doesn't support the sys module. Which is kind of bad, consider reporting an Org bug about that.

| improve this answer | |
  • that is a good tipp. I was running node version 13.x and on my working desktop I am running v10.16.3 I then downgraded on my laptop to the same version via nvm threw away the .spacemacs.env ffile and tried again. An eshell inside spacemacs grabs node version 10.16.3 now but the error on code evaluation stays the same. – sebastian Feb 22 at 16:36
  • what would you suggest? completly removing node and installing node from scratch? Is there a version or perhaps node distribution that is sure to work? I am not a node guy so I don't have the answer to that myself. – sebastian Feb 22 at 16:39
  • My suggestion is in my reply, report a bug with Org and have them figure out a solution compatible with recent versions of Node. – wasamasa Feb 22 at 19:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.