{"id":19500638,"url":"https://github.com/reactioncommerce/babel-remove-es-create-require","last_synced_at":"2025-04-25T23:30:33.687Z","repository":{"id":73364138,"uuid":"215880193","full_name":"reactioncommerce/babel-remove-es-create-require","owner":"reactioncommerce","description":null,"archived":false,"fork":false,"pushed_at":"2019-10-17T22:45:33.000Z","size":44,"stargazers_count":1,"open_issues_count":0,"forks_count":2,"subscribers_count":5,"default_branch":"trunk","last_synced_at":"2025-04-19T04:14:32.040Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/reactioncommerce.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-17T20:29:58.000Z","updated_at":"2021-12-24T16:16:33.000Z","dependencies_parsed_at":null,"dependency_job_id":"7bf06c2b-0c77-45a8-b7f4-94c2847d7b72","html_url":"https://github.com/reactioncommerce/babel-remove-es-create-require","commit_stats":{"total_commits":3,"total_committers":1,"mean_commits":3.0,"dds":0.0,"last_synced_commit":"c1b85a150feb74bce178356b25f1add5f9bbe67f"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reactioncommerce%2Fbabel-remove-es-create-require","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reactioncommerce%2Fbabel-remove-es-create-require/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reactioncommerce%2Fbabel-remove-es-create-require/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reactioncommerce%2Fbabel-remove-es-create-require/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/reactioncommerce","download_url":"https://codeload.github.com/reactioncommerce/babel-remove-es-create-require/tar.gz/refs/heads/trunk","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250912660,"owners_count":21506865,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T22:09:21.186Z","updated_at":"2025-04-25T23:30:33.451Z","avatar_url":"https://github.com/reactioncommerce.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @reactioncommerce/babel-remove-es-create-require\n\nA [Babel](https://babeljs.io) plugin.\n\nIf you create a Node 12 project with ECMAScript (ES) modules enabled, you will likely be doing this in a few places to import from CommonJS modules:\n\n```js\nimport { createRequire } from \"module\";\n\nconst require = createRequire(import.meta.url);\n```\n\nThis is the official way to get the `require` function in a module file.\n\nNow let's say you want to add some Jest tests. Unfortunately Jest does not yet natively parse ES modules, and it doesn't like `import.meta`, so you'll need Babel configured for tests only.\n\nInstall `@babel/core`, `babel-jest`, `@babel/preset-env`, and two plugins that take care of transforming your ES modules:\n\n```sh\nnpm i --save-dev @babel/core @babel/preset-env babel-jest babel-plugin-transform-import-meta babel-plugin-transform-es2015-modules-commonjs\n```\n\nThe `babel-plugin-transform-es2015-modules-commonjs` plugin does most of the transformation to CommonJS and `babel-plugin-transform-import-meta` transforms the `import.meta` code into something Jest will accept. But there's still a problem...\n\n```js\nconst require = createRequire(import.meta.url);\n```\n\nThose pesky `createRequire` calls are still in there, and now they're causing confusion. We no longer need them because `require` will now work automatically based on the file being CommonJS.\n\nThis is where this package comes in. It does only one thing: removes all lines where you're calling `createRequire`. Install it the same way you installed the other Babel plugins:\n\n```sh\nnpm i --save-dev @reactioncommerce/babel-remove-es-create-require\n```\n\nThen add it to your Babel config plugins list. Here's an example `babel.config.js` file that should work for a Node 12 project with ES modules enabled.\n\n```js\nmodule.exports = function (api) {\n  api.cache(false);\n\n  return {\n    presets: [\n      [\n        \"@babel/preset-env\",\n        {\n          targets: {\n            node: \"12\"\n          }\n        }\n      ]\n    ],\n    plugins: [\n      \"babel-plugin-transform-import-meta\",\n      \"module:@reactioncommerce/babel-remove-es-create-require\",\n      \"transform-es2015-modules-commonjs\"\n    ]\n  };\n};\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freactioncommerce%2Fbabel-remove-es-create-require","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Freactioncommerce%2Fbabel-remove-es-create-require","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freactioncommerce%2Fbabel-remove-es-create-require/lists"}