Including NodeJS library manually not working - javascript

I need to include a NodeJS library manually for our config, and I downloaded this library: https://github.com/squaremo/amqp.node/
and I tried to include the library with
var amqp = require("amqp.node-master");
I ended up getting exceptions for
Cannot find module './defs'
I look in connection.js and it has this at the beginning:
'use strict';
var defs = require('./defs');
var constants = defs.constants;
var frame = require('./frame');
var HEARTBEAT = frame.HEARTBEAT;
var Mux = require('./mux').Mux;
Looking at it, I don't see any defs folder or def.js in the library. Am I missing something?

If you look at their source, you'll see that def.js is generated during build.
That means you can't install it directly from GitHub; you need to install a version that ran make.

Related

Using RequireJS with node to optimize creating single output file does not include all the required files

I use the FayeJS and the latest version has been modified to use RequireJS, so there is no longer a single file to link into the browser. Instead the structure is as follows:
/adapters
/engines
/mixins
/protocol
/transport
/util
faye_browser.js
I am using the following nodejs build script to try and end up with all the above minified into a single file:
var fs = require('fs-extra'),
requirejs = require('requirejs');
var config = {
baseUrl: 'htdocs/js/dev/faye/'
,name: 'faye_browser'
, out: 'htdocs/js/dev/faye/dist/faye.min.js'
, paths: {
dist: "empty:"
}
,findNestedDependencies: true
};
requirejs.optimize(config, function (buildResponse) {
//buildResponse is just a text output of the modules
//included. Load the built file for the contents.
//Use config.out to get the optimized file contents.
var contents = fs.readFileSync(config.out, 'utf8');
}, function (err) {
//optimization err callback
console.log(err);
});
The content of faye_browser.js is:
'use strict';
var constants = require('./util/constants'),
Logging = require('./mixins/logging');
var Faye = {
VERSION: constants.VERSION,
Client: require('./protocol/client'),
Scheduler: require('./protocol/scheduler')
};
Logging.wrapper = Faye;
module.exports = Faye;
As I under stand it the optimizer should pull in the required files, and then if those files have required files, it should pull in those etc..., and and output a single minified faye.min.js that contains the whole lot, refactored so no additional serverside calls are necessary.
What happens is faye.min.js gets created, but it only contains the content of faye_browser.js, none of the other required files are included.
I have searched all over the web, and looked at a heap of different examples and none of them work for me.
What am I doing wrong here?
For anyone else trying to do this, I mist that on the download page it says:
The Node.js version is available through npm. This package contains a
copy of the browser client, which is served up by the Faye server when
running.
So to get it you have to pull down the code via NPM and then go into the NPM install dir and it is in the "client" dir...

export node module to local error

i am a newby on node.js. i want to parse a xml file into json .so i am trying to use bulebutton library from https://github.com/blue-button/bluebutton.js .
first i have installed the module by command npm install bluebutton and its created a node_modules folder with bluebutton module.
now i created a test.js file with following code
var bb = require('bluebutton');
var myRecord = bb.BlueButton('./asd.xml');
console.log(myRecord);
but its gave me an error that bluebutton is not define .please help me to figureout this problem thanks
REVISED ANSWER
From bluebuttonjs.com/docs, the require statement you use would return the BlueButton object, so bb represents said object, and it would be called like so
var myRecord = bb('someFile.xml');
However you might also note that they use fs to read the file before passing it. http://www.bluebuttonjs.com/docs/#parsing-node
PREVIOUS ANSWER (for wrong module)
According to their npm docs, you need to do
var bb = require('blue-button');
https://www.npmjs.com/package/blue-button

How to start with WebTorrent?

I have some idea for WebTorrent. I have some experience with JavaScript and jQuery, but I never use Node.js or Browserify. Could someone explain me how to use the following simple code?
var client = new WebTorrent()
var torrentId = 'magnet:?xt=urn:btih:6a9759bffd5c0af65319979fb7832189f4f3c35d'
client.add(torrentId, function (torrent) {
// Torrents can contain many files. Let's use the first.
var file = torrent.files[0]
// Display the file by adding it to the DOM. Supports video, audio, image, etc. files
file.appendTo('body')
})
Should I attach a <script src="webtorrent.min.js"></script> and convert the code from above by Browserify (browserify app.js > bundle.js)? Or maybe I need to use npm install webtorrent and attach some Browserify code?
After using the code npm install webtorrent and just doing Ctrl + c + Ctrl + v, this example from above in <script> tag showed up player. Magic :) But I do something in my VM and now Chrome say that the require('webtorrent') is not definied.
It would be great if I could use WebTorrent as simple as jQuery.
With the code you posted, you don't need to require. If you use the standalone minified version, you can use use var client = WebTorrent()and start using it.
Here's an old example: http://jsfiddle.net/fr090taa/ when I answered this: How to use webtorrent.io
Don't use node/npm/browserify. Just use the minified version.
Check https://github.com/DiegoRBaquero/BTorrent (there's also a clean compiled version to view in JS instead of Coffee)
I'm a WebTorrent project contributor.

Pulling in sinon submodules in browser with AMD and webpack

I'm running into the same problem described in this question. Basically sinon only pulls in all required submodules if you're using node. If you try to use it in the browser with AMD (I'm using webpack) apparently you have to manually include lib/spy.js.
I have a webpack module that currently exports sinon:
exports.sinon = require('sinon');
How would I modify that to roll in spy.js?
I think this is more of a CommonJS syntax question than anything.
The following seems to work:
exports.sinon = require("sinon");
exports.sinon.spy = require("sinon/lib/sinon/spy");
exports.sinon.spyCall = require("sinon/lib/sinon/call");
exports.sinon.behavior = require("sinon/lib/sinon/behavior");
exports.sinon.stub = require("sinon/lib/sinon/stub");
exports.sinon.mock = require("sinon/lib/sinon/mock");
exports.sinon.collection = require("sinon/lib/sinon/collection");
exports.sinon.assert = require("sinon/lib/sinon/assert");
exports.sinon.sandbox = require("sinon/lib/sinon/sandbox");
exports.sinon.test = require("sinon/lib/sinon/test");
exports.sinon.testCase = require("sinon/lib/sinon/test_case");
exports.sinon.match = require("sinon/lib/sinon/match");
This is basically mimicking the behavior in sinon.js. If anyone has a better/cleaner solution please post it and I'll accept. And now that I look at it I wonder why there're

How do you compile CoffeeScript in a Jakefile?

I would like to create a Jakefile which compiles some CoffeeScripts to install a NodeJS application.
How do you do that?
I tried with:
https://gist.github.com/1241827
but it's a weak approach, definitely not classy.
Any hints?
Approx snippet I have used:
var fs = require('fs')
var coffee = require('coffee-script')
// If you'd like to see compiled code..
// console.log(coffee.compile(fs.readFileSync('coffee.coffee')))
// ..otherwise
fs.writeFileSync('output.js', coffee.compile(fs.readFileSync('input.coffee')))
..assumes you have the coffee-script node module installed, of course.
Translated from this Cakefile of mine.

Categories