Tuesday, 3 January 2017

How do I include a JavaScript file in another JavaScript file?



Is there something in JavaScript similar to @import in CSS that allows you to include a JavaScript file inside another JavaScript file?


Answer



The old versions of JavaScript had no import, include, or require, so many different approaches to this problem have been developed.



But since 2015 (ES6), JavaScript has had the ES6 modules standard to import modules in Node.js, which is also supported by most modern browsers.




For compatibility with older browsers, build and/or transpilation tools can be used.





ECMAScript (ES6) modules have been supported in Node.js since v8.5, with the --experimental-modules flag. All files involved must have the .mjs extension.



// module.mjs
export function hello() {
return "Hello";
}



// main.mjs
import { hello } from 'module'; // or './module'
let val = hello(); // val is "Hello";


ECMAScript modules in browsers



Browsers have had support for loading ECMAScript modules directly (no tools like Webpack required) since Safari 10.1, Chrome 61, Firefox 60, and Edge 16. Check the current support at caniuse.







// hello.mjs
export function hello(text) {
const div = document.createElement('div');

div.textContent = `Hello ${text}`;
document.body.appendChild(div);
}


Read more at https://jakearchibald.com/2017/es-modules-in-browsers/



Dynamic imports in browsers



Dynamic imports let the script load other scripts as needed:







Read more at https://developers.google.com/web/updates/2017/11/dynamic-import






The old style of importing modules, still widely used in Node.js, is the module.exports/require system.



// mymodule.js
module.exports = {
hello: function() {
return "Hello";
}
}



// server.js
const myModule = require('./mymodule');
let val = myModule.hello(); // val is "Hello"


There are other ways for JavaScript to include external JavaScript contents in browsers that do not require preprocessing.






You could load an additional script with an AJAX call and then use eval to run it. This is the most straightforward way, but it is limited to your domain because of the JavaScript sandbox security model. Using eval also opens the door to bugs, hacks and security issues.





Like Dynamic Imports you can load one or many scripts with a fetch call using promises to control order of execution for script dependencies using the Fetch Inject library:



fetchInject([
'https://cdn.jsdelivr.net/momentjs/2.17.1/moment.min.js'
]).then(() => {

console.log(`Finish in less than ${moment().endOf('year').fromNow(true)}`)
})




The jQuery library provides loading functionality in one line:



$.getScript("my_lovely_script.js", function() {
alert("Script loaded but not necessarily executed.");

});




You could add a script tag with the script URL into the HTML. To avoid the overhead of jQuery, this is an ideal solution.



The script can even reside on a different server. Furthermore, the browser evaluates the code. The

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...