0

How do i make a ESModule and CommonJS module?

I want to make a package, this package can be used with import, and also with require, something like this: import { function } from 'package' and: const { function } = require('package'); It is posible?, How?

31st Mar 2021, 12:09 AM
<Camilo/>
<Camilo/> - avatar
2 Réponses
+ 3
The import and export statements have been standardized in ES2015. They are supported in most of the browsers at this moment. Some browsers that don't recognize the ES module syntax. We can use JavaScript module bundler like webpack, webpack actually "transpiles" the code so that older browsers can also run it. Since webpack is a node package, it supports CommonJS modules by default. To learn webpack, please check out https://webpack.js.org/guides/getting-started/ https://webpack.js.org/concepts/modules/ Webpack allows mix and match import and require in the same file. https://pencilflip.medium.com/using-es-modules-with-commonjs-modules-with-webpack-2cb6821a8b99
31st Mar 2021, 11:16 PM
Calviղ
Calviղ - avatar
- 1
here a example: var myModule = { multi: function(val1){ console.log( val1 * 3); }, div: function(val1){ console.log( val1 / 3); } } module.exports = myModule;
31st Mar 2021, 2:09 AM
Alfonso Farías
Alfonso Farías - avatar