{"id":2709,"date":"2024-03-12T12:19:30","date_gmt":"2024-03-12T19:19:30","guid":{"rendered":"https:\/\/cknotes.com\/?p=2709"},"modified":"2024-03-12T12:19:30","modified_gmt":"2024-03-12T19:19:30","slug":"solving-the-commonjs-common-javascript-and-ecmascript-modules-esm-problem","status":"publish","type":"post","link":"https:\/\/cknotes.com\/solving-the-commonjs-common-javascript-and-ecmascript-modules-esm-problem\/","title":{"rendered":"Solving the CommonJS (Common JavaScript) and ECMAScript Modules (ESM) Problem"},"content":{"rendered":"<p>The following is a solution graciously provided by a Chilkat user trying to convert a node16 application using Common JS (require) to node20 using ESM (import).<\/p>\n<p>CommonJS (Common JavaScript) and ECMAScript Modules (ESM) are two different module systems used in JavaScript environments. CommonJS is the module system used in Node.js and is characterized by `require()` and `module.exports` syntax, while ECMAScript Modules are the standard module system introduced in ECMAScript 6 (ES6) and are characterized by `import` and `export` statements.<\/p>\n<pre style=\"background-color: black; color: white; font-family: 'Courier New', monospace;\">'use strict'\r\nimport fp from 'fastify-plugin';\r\nimport os from 'os'\r\nconst drive = process.env.drive;\r\n\r\n\/\/ The following fixes common libs when you cant import\r\nimport { createRequire } from \"module\";\r\nconst require = createRequire(import.meta.url);\r\n\/\/ End The following fixes common libs when you cant import\r\n\r\nconsole.log(\" chilkat================== \")\r\n if (os.platform() == 'win32') {\r\n  if (os.arch() == 'ia32') {\r\n    var chilkat = require('@chilkat\/ck-node20-win-ia32');\r\n  } else {\r\n    var chilkat = require('@chilkat\/ck-node20-win64');\r\n  }\r\n}\r\n\r\nfunction chilkat_decrypt(str) {\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The following is a solution graciously provided by a Chilkat user trying to convert a node16 application using Common JS (require) to node20 using ESM (import). CommonJS (Common JavaScript) and ECMAScript Modules (ESM) are two different module systems used in JavaScript environments. CommonJS is the module system used in Node.js and is characterized by `require()` [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[818],"tags":[566],"class_list":["post-2709","post","type-post","status-publish","format-standard","hentry","category-node-js","tag-node-js"],"_links":{"self":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/2709","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/comments?post=2709"}],"version-history":[{"count":3,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/2709\/revisions"}],"predecessor-version":[{"id":2712,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/2709\/revisions\/2712"}],"wp:attachment":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/media?parent=2709"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/categories?post=2709"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/tags?post=2709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}