A babel codemod to transform a script to a module with equivalent behaviour

Usage no npm install needed!

<script type="module">
  import shelterBabelPluginCodemodScriptToModule from 'https://cdn.skypack.dev/@shelter/babel-plugin-codemod-script-to-module';



A babel codemod to transform an ECMAScript script to a ECMAScript module while preserving behaviour. This is useful for integrating legacy code with your module bundler.


This is designed to be used alongside babel-codemod.

yarn add -D @shelter/babel-plugin-codemod-script-to-module babel-codemod

codemod --plugin @shelter/babel-plugin-codemod-script-to-module --source-type script path/to/file.js

After you have finished using the codemod, you can remove the related packages:

yarn remove @shelter/babel-plugin-codemod-script-to-module babel-codemod


Most testing is done by using the fixtures in the __fixtures__ directory. Their purpose is two-fold. Firstly, we check that input.js compiles to output.js. Secondly, these files contain assertions, and we check that the assertions pass in both the input when interpreted as a script and the output when interpreted as a module. These assertions help ensure that behaviour is preserved during transformation.