How to load node modules in beaker notebook? - node.js

I'm using beaker notebook and would like to use some external node modules - yet because beaker doesn't change the system path to the directory of the currently active file, my attempts to include stream-filter are met with the error:
Error: Cannot find module 'stream-filter'
Error: Cannot find module 'stream-filter'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:286:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at eval (eval at processCode (/opt/homebrew-cask/Caskroom/beaker/1.4.2-1-ge55c059/Beaker.app/Contents/Resources/dist/config/plugins/eval/node/app/app.js:45:23), :5:14)
at processCode (/opt/homebrew-cask/Caskroom/beaker/1.4.2-1-ge55c059/Beaker.app/Contents/Resources/dist/config/plugins/eval/node/app/app.js:45:18)
at /opt/homebrew-cask/Caskroom/beaker/1.4.2-1-ge55c059/Beaker.app/Contents/Resources/dist/config/plugins/eval/node/app/app.js:32:28
at callbacks (/opt/homebrew-cask/Caskroom/beaker/1.4.2-1-ge55c059/Beaker.app/Contents/Resources/dist/config/plugins/eval/node/app/node_modules/express/lib/router/index.js:164:37)
at param (/opt/homebrew-cask/Caskroom/beaker/1.4.2-1-ge55c059/Beaker.app/Contents/Resources/dist/config/plugins/eval/node/app/node_modules/express/lib/router/index.js:138:11)
at pass (/opt/homebrew-cask/Caskroom/beaker/1.4.2-1-ge55c059/Beaker.app/Contents/Resources/dist/config/plugins/eval/node/app/node_modules/express/lib/router/index.js:145:5)
I'm wondering how to solve this problem. I tried process.chdir('/Users/akivalipshitz/Developer/Computational_Linguistics') to no avail, even though stream-filter is installed in node_modules in the same directory.
So how do people use node modules

The context where the node code is eval'd is in your case
/opt/homebrew-cask/Caskroom//beaker/1.4.2-1-ge55c059/Beaker.app/Contents/Resources/dist/config/plugins/eval/node/app/app.js
So require() looks for a module in the node_modules folder there and then recursively up along the parent folders (see docs).
One solution (if you're only working on one project or use this module frequently across all your projects) would be to install your module there:
cd /opt/homebrew-cask/Caskroom//beaker/1.4.2-1-ge55c059/Beaker.app/Contents/Resources/dist/config/plugins/eval/node/app/
npm install stream-filter
Another (ugly) approach could be to build a long relative path in your require(). In your case something like:
require('../../../../../../../../../../../../Users/akivalipshitz/Developer/Computational_Linguistics');
Both cases it would mean that your code is not portable to another beaker instance. However, at least until a better solution is proposed this could get you along prototyping.

as of this writing, the current version of Beaker allows you to import npm modules right from the GUI. Just click notebook -> Language Manager -> Javascript, then type the module names into Loaded Libraries. Beaker autocompletes from the npm registry.
This works both in the electron client and the cloud hosted version.

Related

''adonis' is not recognized as an internal or external command' problem even though I installed the CLI globally

My Windows 10 PC forces me to install CLI and assign environment varriables (happens when I install ionic). This time, same trouble with Adonis. First, the problem is 'adonis' is not recognized as an internal or external command'
To solve this, I have installed Adonis locally in C:\Users\mq003\AppData\Local\node_modules. Which I receive is the only #Adonis directory and it doesn't come with a .cmd file so I can't use this. Therefore, I imported directly the cli from github with npm i -save #adonisjs/cli and copy the .cmd files in npm directory, while others will be put in node_modules (because my Path declare I put the cmd there).
As you can see, all ngrok, ionic, etc. commands belong here. They all work perfectly fine in every terminals.
When I try to type adonis commands, this time, it said.
PS D:\adonis project> adonis --help
internal/modules/cjs/loader.js:626
throw err;
^
Error: Cannot find module 'C:\Users\mq003\AppData\Roaming\#adonisjs\cli\index.js'
?[90m at Function.Module._resolveFilename (internal/modules/cjs/loader.js:623:15)?[39m
?[90m at Function.Module._load (internal/modules/cjs/loader.js:527:27)?[39m
?[90m at Function.Module.runMain (internal/modules/cjs/loader.js:837:10)?[39m
?[90m at internal/main/run_main_module.js:17:11?[39m {
code: ?[32m'MODULE_NOT_FOUND'?[39m,
requireStack: []
}
I think it requires #adonisjs directory outside, not in node_modules. So I get that directory out (as you can see in the picture). However, the error still exists.
Editted: I found an issue posted on Github seems relevant to my problem. I will link here in case it helps.
Github

Electron running SQLlite 3 on Windows - A dynamic link library (DLL) initialization routine failed

This is similar to this Electron Uncaught Error: A dynamic link library (DLL) initialization routine failed
ELECTRON_ASAR.js:173
return old.apply(this, arguments)
^
Error: A dynamic link library (DLL) initialization routine failed.
\\?\C:\workspace\client\client\desktop\node_modules\sqlite3\lib\binding\node-v57
-win32-x64\node_sqlite3.node
at process.module.(anonymous function) [as dlopen] (ELECTRON_ASAR.js:173:20)
at Object.Module._extensions..node (module.js:671:18)
at Object.module.(anonymous function) [as .node] (ELECTRON_ASAR.js:173:20)
at Module.load (module.js:561:32)
at tryModuleLoad (module.js:504:12)
at Function.Module._load (module.js:496:3)
at Module.require (module.js:586:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (C:\workspace\client\client\desktop\node_modules\sqlit
e3\lib\sqlite3.js:4:15)
at Object.<anonymous> (C:\workspace\client\client\desktop\node_modules\sqlit
e3\lib\sqlite3.js:190:3)
I've tried everything suggested in this post and anything else I can find
I've installed electron-rebuild and added "rebuild": "electron-rebuild -f -w sqlite3 -V 1.7"
This is running without errors.
I tried "postinstall": "install-app-deps",
This isn't helping..
Anything else to try?
The files are in there..
The
This ended up being a mashup of a bunch of different issues on my end. I'll start with the simplest and go from there.
Platform
Your error there indicates that it wants node-v57-win32-x64, but if you are running from Electron it should be electron-v2.0-win32-x64 (or electron-v1.7-win32-x64 if you are using an older version). I ran into this as well because I am running sqlite3 in a child process. Child processes in Electron are in a full node context and not an Electron (browser or node+browser) context. I fixed this by passing the Electron version from the main browser process (process.versions.electron) to the child process via an environment variable and setting it on process.versions.electron in the child process.
WARNING: that is a HACK and works because I'm not using anything that expects a full Electron environment in that process. It is literally only to make it find the correct native bindings. A more correct fix would be to look into making electron-builder build node-v57-win32-x64 instead.
Speaking of which, let's create those next.
postinstall
"scripts": {
...
"postinstall": "electron-builder install-app-deps"
}
This should attempt to rebuild your native dependencies when running npm install or yarn install. If you have a project/app directory, they will be placed in project/app/node_modules/. Otherwise they'll go in project/node_modules.
Transitive Dependency
In my case, sqlite3 is actually a transitive dependency of another package in my dependency set. While electron-builder on Linux/OS X picked these up correctly and showed this during installer creation:
• rebuilding native production dependencies platform=linux arch=x64
• rebuilding native dependency name=sqlite3
the Windows build showed:
• no native production dependencies
This was fixed by adding the transitive dependency as a direct dependency. After I did this, the package started to be picked up by electron-builder for compilation and was spitting out errors.
Windows Build
To fix those errors:
Install python (I used the latest 2.7)
Install Microsoft Build Tools 2013 and restart
run your postinstall script again
This is the point where I said it was "half-solved" on my end. This should be the full solution for basic electron project setups.
yarn workspaces
I use the yarn workspaces feature and my project setup is something like this:
yarn-workspace-project/
workspace/
project/
web-app/
project-electron/
app/
For this kind of setup, yarn install is generally run in the yarn-workspace-project or yarn-workspace-project/workspace directory, rather than in each project. This produces a hoisted node_modules/ directory at yarn-workspace-project/node_modules. When creating the packaged version, electron-builder grabs the dependencies from that hoisted location and everything runs.
When running my start script in project-electron, however, electron . had some issues finding the native binding. The package using sqlite3 was installed under yarn-workspace-project/node_modules, and was therefore resolving sqlite3 to yarn-workspace-project/node_modules/sqlite3. This definitely exists per the yarn install, but the electron bindings got put in yarn-workspace-project/workspace/project-electron/app/node_modules/sqlite3.
There are multiple obvious solutions to that, so I'll leave that as an exercise to the reader. The key problem here is that the default bindings that get downloaded (I think? I don't recall them being built) for node-v57-win32-x64 do not work out of the box for Windows 10 like they do for Linux / OS X, even though the error indicates that they don't exist (which they do).

Bundling and tree-shaking node_modules into a single file

I'm trying to create an AWS Lambda Node 8 Application. I realise you shouldn't try to do npm install on Lambda, and instead upload a zip containing your scripts and the node_modules folder. However, the zip is over 7MB and contains a lot of dead-code that I'm not expecting to be run (for instance, node_modules\node-sass\src contains a load of c++ files which I don't think is called through node?).
I know that 7MB isn't "large" but this is just a sample application to test the waters to use Lambda and I want to nail down the concepts :). Also, to avoid confusion: the application is to compile sass on the fly, so I am trying to deploy libsass/nodesass. I know it's usually a build-time only tool!
I've tried using backpack to bundle my scripts, since it claims to create a single main.js file. It does create a file with a lot of external code, which looks like code from node_modules, but it doesn't work when run in a clean folder.
Error: Cannot find module 'fs-extra'
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.module.exports.exports.__esModule (C:\Dev\fsdf\sass-compiler\main.js:3192:18)
at __webpack_require__ (C:\Dev\fsdf\sass-compiler\main.js:21:30)
at Object.<anonymous> (C:\Dev\fsdf\sass-compiler\main.js:7837:16)
at __webpack_require__ (C:\Dev\fsdf\sass-compiler\main.js:21:30)
at Object.module.exports.Object.defineProperty.value (C:\Dev\fsdf\sass-compiler\main.js:7820:18)
at __webpack_require__ (C:\Dev\fsdf\sass-compiler\main.js:21:30)
The only way I can make it work is to use zip up the entire node_modules folder and the main.js file. However, this produces an 8MB zip since main.js is considerably bigger than my 3KB source code!
Have I missed something, or is simply impossible to avoid zipping up node_modules? My theory is that it should be possible since Webpack doesn't need node_modules in the browser.

ElasticBeanstalk nodejs.log can't find module 'hogan.js'

I have a simple node.js app that is using "hogan": "^1.0.2" (from packages.json under "dependencies").
It has been failing to deploy, and looking in the logs, I am seeing (multiple times):
Error: Cannot find module 'hogan.js'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
I'm fairly new to node.js on EB, and have just been following http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_nodejs_express.html with a pre-existing Express app.
Assuming EB runs npm install for me (is that a safe assumption?), what might the issue be?
I was using "hogan": "^1.0.2" instead of "hogan.js": "^3.0.2", which worked locally but not on EB.
hogan is just an unofficial alias someone made to hogan.js.
I do not get hogan.js errors any more after making the switch.
For what it's worth, I also had an issue similar to this and determined that it was due to file case issues. In the node EB container, the OS is case-sensitive, and because I was ignoring file case changes in git, it didn't update the file casing in the repository.
You should run git config core.ignorecase false if this is the issue.
You might also change "hogan": "^3.0.2" to "hogan": "*" because EB does run npm install for you. If EB can't find the specific version with "^", it might not install the package as you did locally. Also you might check your file structure before you do eb create or eb deploy. You might include something unnecessary if you are using the command line. I suggest that you create an application.zip file with files you need, such as package.json, app.js and index.html and upload it through GUI. Don't ever pack up node_modules in your .zip file, that's troublesome.

"Error: Cannot find module 'less'" Node.js module loading preference/order/cache?

Here's the situation… So I've created a Node.js module that acts as an interface to some Node.js template engines, Shift.js. That is included inside another Node.js module, Design.io (it's specified Design.io's package.json dependencies block). Design.io watches files for changes.
Then you build an app and require('design.io'). You also install, say, Less and Stylus into your project.
This way, when you save a .less or .styl file, a chain of methods are called:
require("design.io") gets notified of the filesystem event. Design.io then calls
require('shift').render(string, extension, callback). Shift.js then calls
require(moduleFor(extension)) # require("less"). The string is compiled.
In my app (current working directory) I have installed less and stylus:
npm install less stylus
The problem I'm having is, in step 3 which is called from within the Shift.js module, I get errors like this:
Error: Cannot find module 'less'
at Function._resolveFilename (module.js:334:11)
at Function._load (module.js:279:25)
at Module.require (module.js:357:17)
at require (module.js:368:17)
at Less.engine (/Users/viatropos/Documents/git/plugins/design.io/node_modules/shift/lib/shift/less.js:6:14)
at Less.render (/Users/viatropos/Documents/git/plugins/design.io/node_modules/shift/lib/shift/less.js:18:21)
at /Users/viatropos/Documents/git/plugins/design.io/node_modules/shift/lib/shift.js:69:23
at /Users/viatropos/Documents/git/plugins/design.io/node_modules/async/lib/async.js:118:13
at Object.forEachSeries (/Users/viatropos/Documents/git/plugins/design.io/node_modules/async/lib/async.js:134:9)
at Object.render (/Users/viatropos/Documents/git/plugins/design.io/node_modules/shift/lib/shift.js:78:31)
My question is, why is this happening? I thought that you could dynamically require libraries from a module as long as they were installed somewhere… What am I missing?
The goal would be that libraries like Shift.js wouldn't have to define every single dependency in package.json… For an "interface to the template engines" library, that would require too many dependencies that the app would probably never be using.
Thanks for the help, hope that was somewhat clear.
When you npm install foo, the foo module gets installed in a directory named node_modules in the current working directory.
When you install this shift library, it only looks for modules to require within its own node_modules directory, or in one of the global directories require.resolve() searches.
And that's the simple solution to your problem:
npm install -g less
And then the library is globally visible to all Node.js code on your computer, rather than only being visible to code in the current working directory.
Alternatively, if you only want shift to see it, then do something like this:
npm install shift
cd node_modules/shift
npm install less

Resources