#TIL - Today I Learned

Execute ES6 module with node.js

When executing a script designed as an ES6 module (f.e. node my-module.js), it will fail with the error Cannot use import statement outside a module.

Add "type": "module" to package.json and it will work.

Async Delay using Timeout

A little helper function to delay the execution of a script asynchonously

1
2
3
4
5
const delay = ms => new Promise((resolve, reject) => {
setTimeout(_ => resolve(), ms)
});

await delay(1000);

Detect Touch Input Devices

As of the W3C draft of Media Queries Level 5, touch devices can be detected by using the hover and the pointer media feature:

1
2
3
4
5
6
7
8
9
/* smartphones, touchscreens */
@media (hover: none) and (pointer: coarse) {
/* ... */
}

/* mouse, touch pad */
@media (hover: hover) and (pointer: fine) {
/* ... */
}

Module Script Access

When using <script type="module">, which allows to dynamically include ES6 modules via import, and bootstrapping the app in the script via var app = new App();, there is no access to app in developer tools or links with javascript: app.myFunc(). It has to be made visible by window.app = New App();.