michelson / dante2

A complete rewrite of dante editor in draft-js
https://michelson.github.io/dante2/
Other
912 stars 121 forks source link

@babel/node cannot require() ES module during "npm build" #233

Open wchen-oxy opened 4 years ago

wchen-oxy commented 4 years ago

Version

Expected: I'm trying to expand upon the project by modifying the code for links. I noticed that my npm build of the cloned project is missing the es and library folder that can be found in the official npm install of dante2. I need those files for my project to import blocks like '"Dante2/package/es/components/blocks/image.js"'. Running npm build in my cloned repo should build an npm and es module and result in the following console output.

➜  dante2 git:(master) ✗ yarn prepublish
yarn run v1.22.4
$ yarn package
$ NODE_ENV=production ./node_modules/.bin/babel-node tools/build-cli.js; yarn build
/Users/garmjs/Documents/htdocs/labs/dante2/package.json
Building: npm module
Building: es module
Built: npm module
Built: es module
$ NODE_ENV=production rollup -c

Actual: Despite the "presets": ["@babel/preset-env", { }] in the .babelrc file, when the @babel/node/bin/babel-node.js file is executed it encounters an error in line 54 of babel-node.js when it reaches

const kexec = require("kexec");

and then throws this error.

internal/modules/cjs/loader.js:1172
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/Work/Documents/Coding/Ongoing/CustomLib/dante2/node_modules/@babel/runtime/helpers/esm/slicedToArray.js
require() of ES modules is not supported.
require() of /Users/Work/Documents/Coding/Ongoing/CustomLib/dante2/node_modules/@babel/runtime/helpers/esm/slicedToArray.js from /Users/Work/Documents/Coding/Ongoing/CustomLib/dante2/tools/amd/build.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename slicedToArray.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/Work/Documents/Coding/Ongoing/CustomLib/dante2/node_modules/@babel/runtime/helpers/esm/package.json.

    at Module._extensions..js (internal/modules/cjs/loader.js:1172:13)
    at Object.newLoader [as .js] (/Users/Work/Documents/Coding/Ongoing/CustomLib/dante2/node_modules/pirates/lib/index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14)
    at Module.require (internal/modules/cjs/loader.js:1042:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.<anonymous> (/Users/Work/Documents/Coding/Ongoing/CustomLib/dante2/tools/amd/build.js:10:46)
    at Module._compile (internal/modules/cjs/loader.js:1156:30)
    at Module._compile (/Users/Work/Documents/Coding/Ongoing/CustomLib/dante2/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:1176:10) {
  code: 'ERR_REQUIRE_ESM'
}

Thus, I hit the same problem mentioned in #223 where only Dante.cjs.js and Dante.es.js files are generated.

What I've Tried I've tried reverting to older versions of node like v10.20.1, changing @babel modules to version 7.0.0, changing the .babelrc file to babel.config.json and fiddlin with the presets as well. I've also followed the error message suggestion and added/removed type:module from the nearest package.json. I've also deleted node_module and reinstalled, cloned the repo for a fresh install as well. Lastly, I've also tried publishing it to Github and npm installing from there as mentioned in https://github.com/michelson/dante2/issues/223#issuecomment-624932589

Apologies if this is a dumb question but I'm not sure how to proceed next. Thank you and I'd appreciate any help I can get.