last day (15 days later) » 

5:10 PM
4
A: Use existing JS "package" with Node.JS?

E_net4All Node.js modules must export its contents via module.exports. You lack the following in your file: module.exports.ping = ping; This works as well (exports is initially assigned to module.exports): exports.ping = ping;

 
This has to be done for every exposed function, then? Can't just simply expose the contents of the "main" JS as-is? I'd have to add an index.js and it would simply wrap/point to the existing JS functions in my library, is this the correct way to go about it?
 
It's the beauty of the Node.js module system. Everything you declare in a module is private unless you either export it or make it a global (which I do not recommend).
 
My library already handles what should be public/private, so I would only be exposing functions which are already public. That's why it seems redundant to me to wrap/point to functions which I've already determined to be "public", but I'm new to Node.JS so bare with me. I'll give it a go. Thanks!
 
@JoshM. I'd say it's much easier if you rely on Node's module system rather than making your own workarounds to attain encapsulation at this level.
 
Not planning on doing any workarounds, just wanted to know the proper way to do this. Thanks.
@E_net4 I created an index.js and set my package to point to it as main. I added exports.ping = ping and also tried exports.ping = module.ping but neither work. How do I expose my ping function in my-pkg.js?
 
5:10 PM
You must export things in the same file they are declared. In "index.js", simply require the remaining files to retrieve their contents ( ping = require('./someothermodule').ping ).
 
In index.js I've done: exports.ping = require("./my-pkg.js").ping;
Then...
node
> var x = require("my-pkg");
undefined
> x
{ ping: undefined }
 
I suspect you exported ping without being defined. Either do exports.ping = function(...){...} or just place exports.ping = ping where you are sure that the function is defined.
 
But ping is defined in my-pkg.js which I just required.
I appreciate your continued support. The docs weren't clear to me on this...
I get...
> var x = require("my-pkg");
ReferenceError: ping is not defined
When I do ...
exports.ping = ping;
Which makes sense since my-pkg.js hasn't been referenced at that point.
 
Well, right now it's unclear to be what exactly you have in each file. :P
 
5:30 PM
index.js
//exports.ping = require("./my-pkg.js").ping;
exports.ping = ping;
my-pkg.js
function ping() {
console.log("Hi!");
}
package.json
{
"name": "my-pkg",
"version": "1.0.0",
"description": "My test package.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Me",
"license": "ISC"
}
fin
 
5:43 PM
You see, you still haven't exported ping in "my-pkg.js".
 
I know, that's the thing I don't want to have to do. Consider that "my-pkg" is an already-completed, encapsulated JS library. I only wanted to add it to NPM, add an index.js and expose the functionality within the completed library. I don't want to (can't) add NPM-specific stuff within the standalone library.
This seems like it would be a very common use case.
Of course it works fine when I export from within the same file, but my plan was to simply wrap my existing library.
I was under the impression that I should be able to use Node.JS with an existing library but I believe my understanding is flawed. I assumed Node.JS would simply work just like JS within a browser, however I now see that things such as XMLHttpRequest do not exist in Node.JS - so I guess it's normal to write a library specifically for Node.JS instead of simply "releasing" an existing library...
 
6:06 PM
Looks, let's get some facts straight.
Node.js, unlike plain JavaScript, actually has a module system. This is great, and if you just learn how modules are made, you are well on your way to achIeve all of your needs.
What you want to do there is still plain simple. It's not a few module.exports=... in your files that will clobber up your code.
As for the other part, XMLHttpRequest is part of the browser's environment, and not available in Node. The common alternative is the built-in "http" module. You can also just use another library in the npm, such as "request".
So yes, it's JavaScript. But it's not ye olde JavaScript for the browser. No window, no document, and those other things. On the other hand, you are given many more powerful features that only a desktop/server application could have.
 
6:26 PM
The issues that you have now only sound like misconceptions about the module system and the Node.js ecosystem.
 
I agree - misconceptions and misunderstandings.
 

last day (15 days later) »