Skip to content
This repository has been archived by the owner on May 24, 2021. It is now read-only.

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

Open
wchen-oxy opened this issue Jul 20, 2020 · 0 comments
Open

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

wchen-oxy opened this issue Jul 20, 2020 · 0 comments

Comments

@wchen-oxy
Copy link

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 #223 (comment)

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant