In Node.js a module system called CommonJS was created, this is used with: const module = require('module'); In ES2015/6 a native module system was added to the language using the syntax of: import module from 'module'; Although they basically seem to do the same thing, they actually work differently at a lower level.
CommonJS when you load a module brings you a copy of the module so you can use it, while ES2015 brings a reference to that module. This means that if your module has a variable and you export a function that modifies that variable, any other part of your application that makes use of that variable will see the change reflected, whereas with CommonJS everyone would have their own copy of the variable. .
You can read a better and more complete explanation here
The difference between import and require is huge.
On the one hand require is a global function introduced by the execution environment while import is a new syntax added to the language. Since require is a function like any other, it can be called anywhere in the code. Thus allowing the dynamic loading of modules. This can be good or bad depending on how you use it, but doing it right can make applications start faster. On the other hand, import is just syntax and therefore cannot be assigned to a variable, so module loading is statically defined. This second can also be bad or good depending on how you look at it. On the one hand, since the relationship between modules is static, its analysis can be carried out without executing code, which allows for better development tools and, on the other hand, it has the disadvantage of being somewhat rigid.
To illustrate the difference, let's take the sequelize library as a case study. This library offers the possibility to import a javascript module as a model. This is possible because as require is a function like any other, its argument can be any value. This cannot be done in the same way using import.
The difference that Daniel Hernández points out above has more to do with how the module is used and the particularities of Javascript as a language than with the module system that is used itself. Also the statementCommonJS cuando cargas un módulo te trae una copia del módulo para que lo puedas usar, mientras que ES2015 trae una referencia a ese módulo.it is totally false. See the official Nodejs documentation for more info about it. require only returns a reference to the module that is loaded in the Node module cache. import supports the existence of local symbols pointing to objects in other modules while require only supports pointing to the module as a whole. Hence, creating a local symbol that references a variable in another module has to go through a process of assigning a new local variable. Given the nature of Javascript, if this local variable is re-assigned, it is only changing where it points to and therefore the variable in the module whose initial value was required is not modified.
Example:
// aquí x no es una variable local sino un símbolo, no puede ser reasignado por definición debido a la nueva sintaxis
import {x} from 'x';
// aquí y es una variable definida localmente que puede sufrir modificaciones
var y = require('x').x;
// aquí la variable local y se ha reasignado para que referencie el valor 35.
y = 35;
The example above does not imply that it is not possible to modify the variable x exported from the module x in local code. But when using a local variable, it is this variable that we are modifying. The following example modifies the variable x in the module x using the CommonJS module system.
var x = require('x');
x.x = 35;
As can be seen, since a local variable is not used, it is possible to directly modify the value of the attribute x of the module x. This new value is now accessible from any other module that requires the 'x' module.
In Node.js a module system called CommonJS was created, this is used with: const module = require('module'); In ES2015/6 a native module system was added to the language using the syntax of: import module from 'module'; Although they basically seem to do the same thing, they actually work differently at a lower level.
CommonJS when you load a module brings you a copy of the module so you can use it, while ES2015 brings a reference to that module. This means that if your module has a variable and you export a function that modifies that variable, any other part of your application that makes use of that variable will see the change reflected, whereas with CommonJS everyone would have their own copy of the variable. .
You can read a better and more complete explanation here
The difference between import and require is huge.
On the one hand require is a global function introduced by the execution environment while import is a new syntax added to the language. Since require is a function like any other, it can be called anywhere in the code. Thus allowing the dynamic loading of modules. This can be good or bad depending on how you use it, but doing it right can make applications start faster. On the other hand, import is just syntax and therefore cannot be assigned to a variable, so module loading is statically defined. This second can also be bad or good depending on how you look at it. On the one hand, since the relationship between modules is static, its analysis can be carried out without executing code, which allows for better development tools and, on the other hand, it has the disadvantage of being somewhat rigid.
To illustrate the difference, let's take the sequelize library as a case study. This library offers the possibility to import a javascript module as a model. This is possible because as require is a function like any other, its argument can be any value. This cannot be done in the same way using import.
The difference that Daniel Hernández points out above has more to do with how the module is used and the particularities of Javascript as a language than with the module system that is used itself. Also the statement
CommonJS cuando cargas un módulo te trae una copia del módulo para que lo puedas usar, mientras que ES2015 trae una referencia a ese módulo.
it is totally false. See the official Nodejs documentation for more info about it. require only returns a reference to the module that is loaded in the Node module cache. import supports the existence of local symbols pointing to objects in other modules while require only supports pointing to the module as a whole. Hence, creating a local symbol that references a variable in another module has to go through a process of assigning a new local variable. Given the nature of Javascript, if this local variable is re-assigned, it is only changing where it points to and therefore the variable in the module whose initial value was required is not modified.Example:
The example above does not imply that it is not possible to modify the variable x exported from the module x in local code. But when using a local variable, it is this variable that we are modifying. The following example modifies the variable x in the module x using the CommonJS module system.
As can be seen, since a local variable is not used, it is possible to directly modify the value of the attribute x of the module x. This new value is now accessible from any other module that requires the 'x' module.