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?
2 Antworten
+ 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
- 1
here a example:
var myModule = {
multi: function(val1){
console.log( val1 * 3);
},
div: function(val1){
console.log( val1 / 3);
}
}
module.exports = myModule;