GithubHelp home page GithubHelp logo

homer0 / cjs2esm Goto Github PK

View Code? Open in Web Editor NEW
14.0 2.0 2.0 1.83 MB

Transforms a project that uses CommonJS to ES Modules.

Home Page: https://homer0.github.io/cjs2esm

License: MIT License

JavaScript 99.57% Shell 0.43%
commonjs nodejs esmodules hacktoberfest

cjs2esm's Introduction

await freeTimeRequest();

cjs2esm's People

Contributors

homer0 avatar semantic-release-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

cjs2esm's Issues

TypeError: Cannot read properties of undefined (reading 'to')

Running npx cjs2esm in root file with no config shows:

[cjs2esm] Loading configuration...
[cjs2esm] No configuration was found, using defaults...
[cjs2esm] Output directory successfully cleaned
[cjs2esm] TypeError: Cannot read properties of undefined (reading 'to')
[cjs2esm] at transformOutput (/home/o/.npm/_npx/b6a0d7ead30377a5/node_modules/cjs2esm/src/index.js:338:25)
[cjs2esm] at /home/o/.npm/_npx/b6a0d7ead30377a5/node_modules/cjs2esm/src/bin.js:25:9
[cjs2esm]
[cjs2esm] If the issue persist, create a ticket and I may be able to help you: https://github.com/homer0/cjs2esm/issues/new :D

Doesn't work with nested node_modules

I use pnpm, and it seems like pnpx cjs2esm tries to import 5to6-codemod directly from node_modules but fails.

Once I added 5to6-codemod to the project it worked.

Named Export Generation Fails on NodeJS Project

I'm converting a NodeJS project from CJS to ESM, and pretty much everything works with this CLI which is lovely in comparison to figuring out how jscodeshift and 5to6-codemod works! Our actual goal is to migrate everything to TypeScript, but in order to get import statements working properly, I need to do some conversions into ESM before running ts-migrate to mitigate errors and warnings.

Most of the codemods work, but named-export-generation always fails for me. This occurs when I run it using this CLI, but also when I run the codemods directly with jscodeshift and a locally installed version of 5to6-codemod. I'm running the same versions as this project specifies in package.json.

Before:

const { GCLOUD_IMAGE_STORAGE_BUCKET_NAME } = process.env
const { DateTime } = require('luxon')
const { logger } = require('../../../../logger')
const { imageStorage } = require('./client')

const getSignedUploadUrl = async (filename, mimeType) => {
  // ...
}

module.exports = {
  getSignedUploadUrl,
}

After:

const { GCLOUD_IMAGE_STORAGE_BUCKET_NAME } = process.env
import { DateTime } from 'luxon';
import { logger } from '../../../../logger';
import { imageStorage } from './client.js';

const getSignedUploadUrl = async (filename, mimeType) => {
  // ...
}

export default {
  getSignedUploadUrl,
};

What I want here is for getSignedUploadUrl() to become a named export (export const), and for the default export to be deleted, because that's how my project typically imports functions like this. When you use Node with ESM, though, it doesn't recognize this older style of exporting, so I get errors all over the place unless manually go through and change named imports and exports myself, which kinda defeats the purpose of a codemod.

Not sure if this is the right place to put this, because I know the issue still exists in 5to6-codemod, but it didn't seem like that project was still getting any updates...

Creates empty exports

Happens when rewriting a file that is already es module (but transpiled via webpack):

export default {foo: true}

becomes

const exported = {foo: true}

export default exported
export const {} = exported
export const {foo} = exported

lodash-es => lodash-es-es

with

	"modules": [{"name": "lodash", "path": "lodash-es"}],

running it on import ... from 'lodash-es' results in import ... from 'lodash-es-es'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.