What is module option in tsconfig used for?

TLDR; module in tsconfig.json tells the compiler what syntax to use for the modules in the emitted .js files. Frequently used values are “commonjs” (require/module.exports) or “ES2015” (import/export keywords), but there are other module systems. module affects the module syntax of emitted code while target affects the rest. What does Specify module code generation mean? … Read more

Reset single module with Jest

As explained in the question, jest.resetModules() resets the module cache, which is useful your module keeps some local state and you want to get rid of that state between tests. The problem with resetModules is that it resets everything but sometimes you want only some of the modules to be reset. Since Jest 24 you … Read more

supporting both CommonJS and AMD

Yes, and I owe this answer to ded and his awesome modules: (function(name, definition) { if (typeof module != ‘undefined’) module.exports = definition(); else if (typeof define == ‘function’ && typeof define.amd == ‘object’) define(definition); else this[name] = definition(); }(‘mod’, function() { //This is the code you would normally have inside define() or add to … Read more

How are UMD and CommonJS (CJS) package folders different, and which should I use?

JavaScript was originally for interactive browsers only. With NodeJS, it is used in non-browser contexts. Because of this and other factors, there are incompatible formats for modules: The “CommonJS” specification describes the use of an exports object which is the API to declare and discover what names are exported from a module. No allowance is … Read more

How to use UMD in browser without any additional dependencies

Ok, so you are running in an environment without RequireJS, CommonJS, SystemJS, etc. The key line is factory((global.mymodule = global.mymodule || {})) this does a few things: If global.mymodule truthy, then it is equivalent to global.mymodule = global.mymodule // A noop. factory(global.mymodule) Otherwise it is equivalent to: global.mymodule = {} factory(global.mymodule) Inside the factory: Your … Read more

Why is CommonJS only said to be suitable for non-browser apps?

CommonJS is definitely suitable for the browser, with some caveats. The CommonJS module pattern is quite nice (in my biased opinion), and is also a good stepping stone to the module system proposed for ECMAScript Harmony (the planned next release of the JavaScript language). Specifically, Harmony modules won’t have access to the global (“window”) object. … Read more

Asynchronous JavaScript – Callbacks vs Deferred/Promise [duplicate]

Promises also rely on callbacks behind the scene, so it’s not really one vs. the other. The benefit of callbacks is that they are easy to implement with plain JavaScript (for example in ajax calls). Promises require an additional abstraction layer, which usually means that you’ll rely on a library (not an issue in your … Read more

In the `import` syntax of ES6, how is a module evaluated exactly?

When the D module is executed, the console will print this message: A evaluated A constructor Which means that the A module was evaluated only once, even if it was imported multiple times by other modules. The evaluation rules for ES6 modules is the same as for commonjs format: A module is a piece of … Read more

Webpack and external libraries

According to the Webpack documentation, you can use the externals property on the config object “to specify dependencies for your library that are not resolved by webpack, but become dependencies of the output. This means they are imported from the enviroment while runtime [sic].” The example on that page illustrates it really well, using jQuery. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)