What is this error that comes up in node.js? - javascript

I was trying to follow this tutorial.
Then when I got to this part
node jsctags/bin/jsctags --sort=yes --locals tst.js
I got the following warning message.
The "sys" module is now called "util". It should have a similar
interface.
I'm doing this in OSX. First I tried the package and then I tried installing from source. I still get the same message.
What does this mean? What can I do about it?

In your jsctags file you probably have a line that looks like this:
sys = require ('sys');
As a first step, try using this line:
sys = require ('util');
This will still refer to the package by the name sys in your script, so the rest should work; but in the future, util = require ('util'); might be better, to make it more clear that you're using the newer package and API.

The message is just Node's way of telling your sys module is deprecated and everyone should migrate to util.
The reason you're getting it may not be your fault. If any of the libraries you're using not migrated to the new modules, it'll also show that message.

Related

Node js - Write data to CSV File

I am trying to write incoming data to a csv file.
I tried to use http://csv.adaltas.com/generate/examples/
npm install csv
But I don't get the documentation. I just want to save some data into a csv file. But even the examples are not working.
E.g. the example in the link above (Using the stream API) throws the following error message:
TypeError: Cannot read property 'eql' of undefined
(First I had to change the require path, after installing the csv full package I had to require it with:
var generate = require('csv/node_modules/csv-generate');
Obviously this guy uses the same node module:
https://masteringmean.com/lessons/18-Writing-to-a-CSV-file-in-Nodejs
But I can't even get his code to work. I think it was made with an older (and better documented?!) version of the csv node module.
Can anyone help me? Maybe with some code, explaining the same basic things like the one seen in the link of masteringmean.com but for the newest version?
Thanks for any hints.
Cheers
should.eql() statement that triggers the error is an assertion made with Should. You have install it at first:
npm install should --save
And require it:
var should = require('should');
The error should disappear.
better yet, just remove these lines:
output.should.eql('OMH,ONKCHhJmjadoA\nD,GeACHiN');
and/or
data.should.eql([ [ 'OMH', 'ONKCHhJmjadoA' ],[ 'D', 'GeACHiN' ] ]);
you really don't want the overhead of a library you're not using.

Better coffeescript workaround to load this node.js module

I would like to use this node.js module https://github.com/mlin/node-assert-type
Based on documentation, to declare the module;
var ty = require("assert-type"); //https://github.com/mlin/node-assert-type
var T = ty.Assert;
In actual practice, this does not work. Some coffeescript error appears.
I have to make the following workaround;
var cs = require("coffee-script/register");//this line needed to require("assert-type")
var ty = require("assert-type"); //https://github.com/mlin/node-assert-type
var T = ty.Assert;
To use this module, I am forced to install coffeescript with npm install -g coffeescript.
Is there some way to omit the line var cs = require("coffee-script/register");? After all, the module itself is using coffeescript locally. Am I doing it the right way?
Is it a normal practice to add a line to load coffeescript for node.js modules which use coffee-script?
It is not normal practice. I mean, it would be inevitable that coffeescript gets installed since it is a dependency but the user of the module should not worry about it.
I just took a quick look at the source code of the assert-type and this is what I found:
the project is 3 years old. That's a lot!
the package.json is listing coffeescript as a dependency BUT it is using latest instead of locking a version of coffeescript which is a terrible practice.
My guess is that what it changed was coffeescript module, that instead of needing require('coffeescript') you now need require('coffeescript/register'). (Take a look at the index.js in the repo)
Based on that I'd say it is fine that you write that line. A better option would be to make the changes in the node-assert-type repo and submit a PR with the fixes for #2 and #3.
Hope that helps.

RethinkDB: Loading A NodeJS package inside r.js method

I am not sure is it possible or not.
I need to use a t-distribution in my ReQL from a nodeJS Package. The code is something like this:
Query.map(function(doc){
return {
'DesScore': r.do(doc('MXt'),
doc('MXdf'),
r.js('(function(v,df){
var distributions = require("distributions.js");
var studentt = distributions.Studentt(df);
return studentt.inv(-1.*Math.abs(v))*2.*100.;}) '))
};
})
But I get
Unhandled rejection ReqlQueryLogicError: ReferenceError: require is not defined in:
How can I load a package in ReQL js interpreter?
Thanks in advance for any help.
You cannot use require in r.js because it's JavaScript v8, not NodeJS. require is come from NodeJS API, it isn't in standard JavaScript. On JavaScript client side, people use browserify (or friend) for that purpose. So to use require, you have to implement a shim for require inside r.js, which may also pull it other dependencies...
More than that, you also have to somehow distribute your dependencies to RethinkDB server itself for require to work.
All of those are a mess. So probably try to find another solution.
Think of r.js as the last way to do something, and try to use native ReQL whenever possible, as the document say:
https://www.rethinkdb.com/api/javascript/js/
With control structure like forEeach, do, branch I think you can do pretty much stuff with it.

How to import strophe using requirejs?

I'm trying to use strophe.js with relay-starter-kit. I checked out relay-starter-kit, added "strophe": "^1.2.2" to package.json and ran npm install.
I can't find how to import strophe without getting errors. If I just try import Strophe from 'strophe'; I get errors that webpack can't resolve strophe-polyfill. I added a resolve alias for that pointing to the main strophe.js file, but that hasn't helped (I just get a console message Uncaught ReferenceError: Strophe is not defined).
It looks like strophe has some weird module system (it mentions AMD in github but I thought that meant I could just require it, but apparently not). All the examples I've seen import it in an HTML file and clutter the global namespace. I need to use it from within react so I don't think that will work.
How can I import strophe to use in my es6 files?
Since I wasted hours of my life on this, here's the answer:
Include strophe in the HTML file with:
Edit server.js to make webpack expose strophe as a static path with:
app.use('/node_modules/strophe', express.static('node_modules/strophe'));
In the react component, there's no need to import strophe since it's now globally available. Instead just connect with, e.g.:
var connection = new Strophe.Connection("ws://" + server + ":5280/websocket/");
try to use import * as XMPP from strophe.js then call
var connection = new XMPP.Strophe.Connection(BOSH_SERVICE) to connect server

Python and the Spidermonkey Javascript engine on Linux

I have successfully installed Spidermonkey JS engine on my Linux machine ( Ubuntu ).
Basically my goal is to make it execute Ajax (js) scripts and return the result back to my Python script. I'm basically trying to build a good O.O. web scraper. But it's pretty hard for me to get all of this working.
I'm now at the point where when I type JS in my terminal I can start executing Javascript.
I've been Googling and found this little snipet on Stackoverflow :
import urllib2
import spidermonkey
js = spidermonkey.Runtime()
js_ctx = js.new_context()
script = urllib2.urlopen('http://etherhack.co.uk/hashing/whirlpool/js/whirlpool.js').read()
js_ctx.eval_script(script)
js_ctx.eval_script('var s="abc"')
js_ctx.eval_script('print(HexWhirpool(s))')
but it failed to run with the error that module Spidermonkey can not be found.
I'm a bit lost now. Anyone able to help?
I also tried easy_install python-spidermonkey with no luck, for libnspr-dev package is absent.
So, I've built package from source. Instructions from project page (Debian Stretch):
Building
Check out the Python-Spidermonkey module from the SVN repository ( I downloaded it as source archive, direct link )
Unpack, and cd to ./python-spidermonkey/trunk
CPPFLAGS="-Wno-format-security" python setup.py build (these flags for Debian)
Error jsemit.h:508:32: error: expected ‘(’ before ‘)’ token uintN decltype); means that decltype cannot be used as variable (maybe it's a macro or something else), fix it this way:
sed -e 's/decltype/dectyp/' -i.ORIG ./js/src/jsemit.h
sed -e 's/decltype/dectyp/' -i.ORIG ./js/src/jsemit.cpp
Error jsemit.cpp:6490:1: error: narrowing conversion of ‘-1’ from ‘int’ to ‘uint8 {aka unsigned char}’ inside { } [-Wnarrowing] means illegal variable conversion, recompile it manually:
cd js/src
g++ -o Linux_All_DBG.OBJ/jsemit.o -c -Wall -Wno-narrowing -Wno-format -MMD -g3 -DXP_UNIX -DSVR4 -DSYSV -D_BSD_SOURCE -DPOSIX_SOURCE -DHAVE_LOCALTIME_R -DHAVE_VA_COPY -DVA_COPY=va_copy -DPIC -fPIC -DDEBUG -DDEBUG_user -DEDITLINE -ILinux_All_DBG.OBJ jsemit.cpp
Error spidermonkey.c:1:2: error: #error Do not use this file, it is the result of a failed Pyrex compilation. - some trouble with pyrex. There is a patch. Do it this way:
wget -O - https://storage.googleapis.com/google-code-attachments/python-spidermonkey/issue-14/comment-4/cinit.patch | patch -p1 ./spidermonkey.pyx
Installation
su, and python setup.py install as root.
Running
By default, setup script installs libjs.so to /usr/local/lib/, so I did ln -s /usr/local/lib/libjs.so /usr/lib/libjs.so (but you'd better use solution from Seagal82)
Without this step, python keeps complaining about import ImportError: libjs.so: cannot open shared object file: No such file or directory
I also had an error ImportError: cannot import name Runtime after from spidermonkey import Runtime. The reason possibly was in old easy_install data in ~/.local/lib/python2.7/site-packages/spidermonkey/. After removing it, all runs smooth
Recently i got a task need to do something like Web scraping,
and for the javascript part, currently want to try using python-spidermonkey to resolve it and see if this might work for me ...
and i seem to meet situation might alike, after i think i finished install python-spidermonkey, i execute the script above, i got this error:
Traceback (most recent call last):
File "spidermonkeytest.py", line 2, in <module>
import spidermonkey
ImportError: libjs.so: cannot open shared object file: No such file or directory
then after some searching by google...i found the solution probably in the end of here:
http://hi.baidu.com/peizhongyou/item/ec1575c3f0e00e31e80f2e48
i setup these things:
$sudo vi /etc/ld.so.conf.d/libjs.so.conf
fill in this line:
/usr/local/lib/
save & exit, execute ldconfig:
$sudo ldconfig
then i can run the script provided above by #Synbitz Prowduczions
don't know if this is the answer you need, or this still helps?
You need to try libnspr4. If that doesn't work, you can always download it from Mozilla and build the code yourself.
It is not difficult to type ./config && make && make install to build the library yourself after untarring the source. If you build yourself, files will likely be in
/usr/local/{include,lib}
Also just try Googling for "YOUR_OS_NAME install nspr4".
I believe someone wrote a C/C++ header file translator for Python ctypes. Although I can't say much else because I don't use Python.
SpiderMonkey also has its own implementation of ctypes modeled after Python. So technically if you know javascript you could forego using Python altogether since you want to do some ajax with it. You will need to brush up on the NSPR or C runtime sockets to meet the requirements for your projects using only Spidermonkey.
OR a web search for Python +AJAX might turn up exactly what you need.

Categories

Resources