ember-codemods / ember-native-class-codemod

A codemod-cli project for converting Ember objects to es6 native classes
68 stars 38 forks source link

Error: `define is not defined` #542

Closed machty closed 6 months ago

machty commented 8 months ago

I'm trying to test this codemod out on a single component via this command:

npx ember-native-class-codemod http://localhost:4200/default-index.html --type=components app/components/dynamic-style.js

But it gives me define is not defined with the stack trace below. I added some console.log for a clue and it seems like it's barfing on trying to load /Users/machty/.npm/_npx/4e4aff0f1510c9e0/node_modules/@babel/plugin-syntax-dynamic-import/lib/index.js after wrapping it in some define() ceremony.

I'd appreciate any guidance.

Ember 3.16

/Users/machty/.npm/_npx/4e4aff0f1510c9e0/node_modules/ember-native-class-codemod/transforms/ember-object/index.js:1
define([], function () {
^

ReferenceError: define is not defined
    at Object.<anonymous> (/Users/machty/.npm/_npx/4e4aff0f1510c9e0/node_modules/ember-native-class-codemod/transforms/ember-object/index.js:1:1)
    at Module._compile (node:internal/modules/cjs/loader:1198:14)
    at Module._compile (/Users/machty/.npm/_npx/4e4aff0f1510c9e0/node_modules/pirates/lib/index.js:118:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1252:10)
    at Object.newLoader [as .js] (/Users/machty/.npm/_npx/4e4aff0f1510c9e0/node_modules/pirates/lib/index.js:124:7)
    at Module.load (node:internal/modules/cjs/loader:1076:32)
    at Function.Module._load (node:internal/modules/cjs/loader:911:12)
    at Module.require (node:internal/modules/cjs/loader:1100:19)
    at require (node:internal/modules/cjs/helpers:119:18)
    at setup (/Users/machty/.npm/_npx/4e4aff0f1510c9e0/node_modules/jscodeshift/src/Worker.js:91:18)
    at Object.<anonymous> (/Users/machty/.npm/_npx/4e4aff0f1510c9e0/node_modules/jscodeshift/src/Worker.js:45:3)
    at Module._compile (node:internal/modules/cjs/loader:1198:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1252:10)
    at Module.load (node:internal/modules/cjs/loader:1076:32)
    at Function.Module._load (node:internal/modules/cjs/loader:911:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:22:47
All done. 
Results: 
0 errors
0 unmodified
0 skipped
0 ok
Time elapsed: 1.150seconds 
machty commented 6 months ago

I traced the issue down to jscodeshift, which uses babel/register to hook into require() and transpile, on demand, any required file. For some reason, when the jscodeshift worker requires the transform file located at

'/Users/machty/.volta/tools/image/packages/ember-native-class-codemod/lib/node_modules/ember-native-class-codemod/transforms/ember-object/index.js'

it thinks it needs to wrap it in a define(). I don't know why.

The easiest way I was able to move on from this issue was to open my local copy of jscodeshift's Worker.js and add an ignore regex for /ember-native-class-codemod/.

Wow that sucked to debug.

machty commented 6 months ago

The reason I was running into errors is because I still had a babel.config.js that was getting picked up by the babel/register inside JSCodeshift. When I got rid of it, I stopped running into define is not defined.