I'm facing an issue with emailjs-mime-builder
Code
var MimeBuilder = require('emailjs-mime-builder')
var rootNode = new MimeBuilder("multipart/mixed"),
childNodeTxt = rootNode.createChild("text/plain").setContent("Text");
childNodeHtml = rootNode.createChild("text/html").setContent("<h1>HTML</h1>");
rootNode.build()
Error
MimeBuilder is not a constructor
The emailjs-mime-builder package is an ES6 Module. Meaning that if you can't use import like for example within a node.js enviornment, you have to require it like this instead:
const { default: MimeBuilder } = require( 'emailjs-mime-builder' );
Related
function requireFromString(src, filename) {
var Module = module.constructor;
var m = new Module();
m._compile(src, filename);
return m.exports;
}
console.log(requireFromString(`
const a = require('./a');
const fs = require('fs');
module.exports = { test: a}
`));
We can require node module by this.
Can we require TypeScript module in memory?
I think what you are looking this: How to compile TypeScript code in the browser?
Or use only typescriptServices.js:
<script src="https://rawgit.com/Microsoft/TypeScript/master/lib/typescriptServices.js"></script>
And add js code:
var hello = "test";
var js = ts.transpile("let a = `<div>${hello}</div>`");
console.log(js);
eval(js);
console.log(a);
Where ts.transpile translate ts to js string.
Examle on next.plnkr.co.
I think what you are looking for is called dynamic imports. Check out https://blog.mariusschulz.com/2018/01/14/typescript-2-4-dynamic-import-expressions
I have problem using all 3 of the packages together. I define them like this:
var moment = require('moment-timezone');
var momentRange = require('moment-range');
And when I want to use the moment-range functions, I'm trying to call it like this:
var range1 = momentRange.range(moment("string1"), moment("string2"));
And I'm getting error: TypeError: momentRange.range is not a function
What am I doing wrong?
According to the documentation, you are supposed to use the moment-range library to first extend the core moment library itself, then use moment.range because the moment-range package adds additional functions to the moment object:
var momentRange = require('moment-range');
momentRange.extendMoment(moment);
moment.range(moment(…), moment(…)); // Now usable
Specifically, in their documentation:
CommonJS:
const Moment = require('moment');
const MomentRange = require('moment-range');
const moment = MomentRange.extendMoment(Moment);
I am learning to use ECMAScript6 -styled classes in NodeJS (7.7.3). I have used this kind of programming style:
//app.js
var forecastHandler = require('./forecastHandler.js');
//forecastHandler.js
class ForecastHandler {
constructor() {}
}
module.exports = new ForecastHandler()
It has worked well until now, because I have to pass parameters to module.
//app.js
var forecastHandler = require('./forecastHandler.js')(3600);
//forecastHandler.js
class ForecastHandler {
constructor(cacheUpdateDelay) {}
}
module.exports = new ForecastHandler(cacheUpdateDelay)
I got this error: ReferenceError: cacheUpdateDelay is not defined.
Can I pass the parameter to ForecastHandler-module using ES6 styled classes and creating an object at module.exports? If I only export the class and create the object in app.js, code works, but it's syntax is ugly.
//app.js
var forecastHandlerClass = require('./forecastHandler.js');
var forecastHandler = new forecastHandlerClass(3600);
//forecastHandler.js
module.exports = ForecastHandler
EDIT: better examples
module.exports = new ForecastHandler(cacheUpdateDelay)
The trouble with this code is that it initialises the object when the code is first run.
require('./forecastHandler.js') means "execute all the code in forecastHandler.js and give me the exports object. This means that the JS engine tries to run new ForecastHandler(cacheUpdateDelay) when there is no cacheUpdateDelay created.
The simple way to do this is the one you provide. Load the class, then try to make a new instance of it. If you really want to one-line it, you can do this in app.js:
var forecastHandler = new (require('./forecastHandler.js'))(3600);
There are various other ways you could do this. The simplest involve not exporting a class but a function.
For instance, you could do this in your module file:
module.exports = cacheUpdateDelay => new ForecastHandler(cacheUpdateDelay);
// OR
module.exports = function(cacheUpdateDelay) {
return new ForecastHandler(cacheUpdateDelay);
};
I'm requiring a library in NodeJS which has a self-invoking function, that results an error because it looks for an object which is not initialized at that moment .
I want to dynamically require this library when that object is initialized.
Is there any way to dynamically require/ load a library ?
This is the part of library required :
https://github.com/sakren/node-google-maps/blob/develop/lib/Google.js#L5
Actually I want to require when the window object is present (client-side rendering).
So something like this :
'use strict';
var React = require('react');
var Map = require('./map.jsx');
var Common = require('../common/common');
var MapStatic = require('./map-static.jsx');
exports.type = function() {
return 'map';
};
exports.jsx = function(data) {
if (Common.isServerSide()) {
return (<MapStatic data={data}/>);
} else {
return (
<Map data={data}/>
);
}
};
exports.transform = require('./map-transform.js');
The reason the code looks weired is that I'm using react.
In nodeJS require can be used anywhere at anytime whithout much limitations AFAIK.
Which error is thrown once you require at runtime ?
In your else branch.
Try the following.
requires = {}
function getter(key) {
if(!requires[key]){
requires[key] = require(key)
}
return requires[key]
}
I'm trying to get the code below under test when occurred to me that I already included express at the top of this file. Can you some how monkey patch the express object after it's already loaded?
var express = require('express')
Helper = (function() {
var HelperObject = function(params) {
this.directories = params.directories;
};
HelperObject.prototype.addStaticPath = function(app) {
for(i = 0; i < this.directories.length; i++) {
var static = express.static('/public');
app.use(static);
}
};
return HelperObject;
})();
The problem is that when you create a node module the required modul is bound in the closure of the module and you can't start spying on it cause it isn't visible in your test.
There is Gently where you can override require but it will sprinkle your code with boilerplate test related code.
From the docs:
Returns a new require functions that catches a reference to all
required modules into gently.hijacked.
To use this function, include a line like this in your 'my-module.js'.
if (global.GENTLY) require = GENTLY.hijack(require);
var sys = require('sys');
exports.hello = function() {
sys.log('world');
};
Now you can write a test for the module above:
var gently = global.GENTLY = new (require('gently'))
, myModule = require('./my-module');
gently.expect(gently.hijacked.sys, 'log', function(str) {
assert.equal(str, 'world');
});
myModule.hello();