alternative for module.exports - node.js

I've got a question that might sound strange.
We are using node.js, but the way we are writing javascript code is in a functional style. We have a lint tool that reports illegal use of the assignment expression. We can eleminate them all except for the 'module.exports = xxx' statement.
Is there an other way to load modules in node.js?
for example; the following statement would be fine as a solution as it is not an assignment expression but a variable declaration:
var exports = xxx;
if there is a way that nodejs would pick up this 'exports' variable, then we're done.
gr,
Coen

the following did the trick for me:
vm.runInThisContext

Looks like changing lint tool is the best way. :)
However you can use different module loader (there are some AMD implementations for node.js out there) that's pure functional.

Related

compile-time constants in mustache template

Suppose I have the following mustache template:
This is a {{#BIG_HOUSE}}really big{{\BIG_HOUSE}} house with {{NUM_WINDOWS}} windows.
and I happen to know at compile time whether BIG_HOUSE is truthy or not. How do I pass this compile-time-constant-iness through to the template compiler?
Obviously in this case it's not very interesting, but in reality the template will be a lot larger and will include several such blocks.
I am currently using mustache-loader with webpack, so any solution specific to that would be great! I also have the UglifyJSPlugin for webpack, so perhaps there is some way of using that here?
The mustache-loader returns a function which evaluates the template when given an object of parameters. This function can be executed using the apply-loader:
require('apply-loader?{obj: {BIG_HOUSE: false}}!mustache-loader!./template.html')

Import a javascript module as dynamic using typescript

I want to import a normal javascript module (e.g. vhost) into my node.js typescript file using CommonJS.
I can do this with the following line:
import vhost = require('vhost')
We assume that I can't find a .d.ts file on the internet, but I also don't want to write it by myself, so I just use the vhost variable without intellisense.
The compiler complains and complains:
How can I tell that I just want it to be 'dynamic' (like the C# dynamic keyword or 'var' in normal javascript) and use all of the things in the picture above?
I could create a vhost.d.ts file, but I don't know what to write in there:
declare module 'vash' {
// what to write here?
}
I found this out while typing the question, it was so easy that it is almost embarrassing, but maybe somebody has this problem too.
Just use var instead of import:

How do you access the "special-keys" module from an Intern functional test?

How can you access the "special-keys" module from an Intern functional test?
I've tried specifying the path in the opening define statement of the test, I've also tried updating the "loader" attribute in the intern.js configuration file, but with no success so far. This must be a fairly common use case for a functional test but I've not had any luck so far and can find any examples in the source or the Git Wiki.
In Intern 1.3+ you may access that module through intern/dojo/node!wd/lib/special-keys. Alternatively you may simply use the escape codes directly in your type/keys strings.

erlang -import not working

I have an erlang program, compiled with rebar, after the new debian release, it won't compile anymore, complaining about this:
-import(erl_scan).
-import(erl_parse).
-import(io_lib).
saying:
bad import declaration
I don't know erlang, I am just trying to compile this thing.
Apparently something bad happened to -import recently http://erlang.org/pipermail/erlang-questions/2013-March/072932.html
Is there an easy way to fix this?
Well, -import(). is working but it does NOT do what you are expecting it to do. It does NOT "import" the module into your module, nor does it go out, find the module and get all the exported functions and allow you to use them without the module name. You use -import like this:
-import(lists, [map/2,foldl/3,foldr/3]).
Then you can call the explicitly imported functions without module name and the compiler syntactically transforms the call by adding the module name. So the compiler will transform:
map(MyFun, List) ===> lists:map(MyFun, List)
Note that this is ALL it does. There are no checks for whether the module exists or if the function is exported, it is a pure naive syntactic transformation. All it gives you is slightly shorter code. For this reason it is seldom used most people advise not to use it.
Note also that the unit of code for all operations is the module so the compiler does not do any inter-module checking or optimisation at all. Everything between modules like checking a modules existence or which functions it exports is done at run-time when you call a function in the other module.
No, there is no easy way to fix this. The source code has to be updated, and every reference to imported functions prefixed with the module in question. For example, every call to format should be replaced with io_lib:format, though you'd have to know which function was imported from which module.
You could start by removing the -import directives. The compilation should then fail, complaining about undefined functions. That is where you need to provide the correct module name. Look at the documentation pages for io_lib, erl_scan and erl_parse to see which functions are in which module.
Your problem is that you were using the experimental -import(Mod) directive which is part of parameterized modules. These are gone in R16B and onwards.
I often advise against using import. It hurts quick searches and unique naming of foreign calls. Get an editor which can quickly expand names.
Start by looking at what is stored in the location $ERL_LIBS, typically this points to /usr/lib/erlang/lib.

How to get "should.be.false" syntax pass jslint?

I am writing JS UT for my NodeJS code. I am using Chai as the assertion library, and I prefer the should syntax. I also use jslint to check the JS file syntax, even the JS files for UT.
Now I have a problem with jslint and Chai. In Chai, you can use:
myvalue.should.be.true;
But jslint give me:
#1 Expected an assignment or function call and instead saw an expression.
I know jslint comes with many options, but I just cannot find the right one to turn off this check.
Just to make it clear to other readers:
You can add /*jshint expr: true*/ to the top of the test files, so it only allows it in the test files.
In the rare case that you use that kind of expressions in your code, you can add the expr: true option to the jshint config options.
You can also use myvalue.should.equal(true) instead.
It turns our jslint is unable to solve my problem. And due to many restrictions of jslint, I turned to jshint, and the option expr saved me :P

Resources