notablemind / jupyter-nodejs

A node.js kernel for jupyter/ipython
http://nbviewer.ipython.org/gist/jaredly/404a36306fdee6a1737a
MIT License
682 stars 70 forks source link

Cannot require other packages (phantomjs-node) installed globally #46

Open cloudgen opened 7 years ago

cloudgen commented 7 years ago

I use the following script to install the kernel to jupyterhub , nodeJS version 4.2.6 , Jupyter 5.1 :

  cd ~
  git clone https://github.com/notablemind/jupyter-nodejs.git
  sudo mv ~/jupyter-nodejs /etc/
  cd /etc/jupyter-nodejs/
  sudo npm install 
  sudo npm run build
  sudo npm run build-ext
  cd /etc/jupyter-nodejs/
  sudo node install.js /usr/local/share/jupyter/kernels/nodejs/

Most of the functions works properly, except when I install phantomjs-node, I tried to require the this package in jupyterhub,

require('phantom');

The error message is as following:

Error: Cannot find module 'phantom'
    at Function.Module._resolveFilename (module.js:326:15)
    at Function.Module._load (module.js:277:25)
    at Module.require (module.js:354:17)
    at require (internal/module.js:12:17)
    at /etc/jupyter-nodejs/build/context.js:78:16
    at require (/etc/jupyter-nodejs/build/context.js:58:27)
    at evalmachine.<anonymous>:1:1
    at Object.exports.runInContext (vm.js:44:17)
    at Context.rawRun (/etc/jupyter-nodejs/build/context.js:184:30)
    at Domain.<anonymous> (/etc/jupyter-nodejs/build/context.js:204:27)

(Updated) I figure out the solution here, modify the kernel.json to include NODE_PATH as env:

{
  "argv": [
    "node",
    "/etc/jupyter-nodejs/build/run.js",
    "{connection_file}"
  ],
  "display_name": "NodeJS 4.3.10",
  "language": "javascript",
  "env":{
    "NODE_PATH" : "/usr/local/lib/node_modules"
  }
}
jenningsanderson commented 7 years ago

Yes, I just spent a long time debugging the same issue. Does jupyter-nodejs not by default look for global modules in /usr/lib/node_modules or /usr/local/lib/node_modules?

Further, I'm running it with jupyterhub, and it seems that whoever compiles it wins... meaning it was only looking in MY home directory's node_modules for modules; other users could not load modules from their local node_modules folder.

Amertz08 commented 6 years ago

+1 Should maybe add directions for this to the docs imo.