selfrefactor/rambda

Regression in rambda 7.4

Phygon opened this issue ยท 14 comments

Wenn upgrading from earlier version to 7.4, the following error is thrown in a project using rambda:

import { pathOr } from 'rambda';
         ^^^^^^
SyntaxError: Named export 'pathOr' not found. The requested module 'rambda' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'rambda';
const { pathOr } = pkg;
// [...]

Nodejs version: v16.18.1. package.json includes "type": "module".

thanks, I haven't run the test on the released version. Can you share info about your package.json and tsconfig.json?

I am using plain JavaScript. I created a minimal example:

  • package.json:
{
  "main": "main.js",
  "type": "module",
  "dependencies": {
    "rambda": "7.4"
  }
}

  • main.js:
import { pathOr } from 'rambda';

let res = pathOr('or', [1], {});
console.log('res:', res);

It works with rambda@7.3 or earlier; with 7.4, the error message in the OP is printed.

I made a change to fix tree-shaking issue, so that seems to be the side effect - Rambda doesn't work when type:module. I will keep the issue open until I figure out how to solve it. Getting back is easy, but I also want to fix tree shaking problem as well. Anyway, good issue and I created several new scripts in rambda-scripts to help out in testing after release.

cjk commented

I'm having a similar issue after upgrading to v9.x:

(node:11123) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/home/cjk/proj/product-groups-mapper/node_modules/rambdax/rambdax.js:1
export * from './src/allFalse'
^^^^^^

Probably all projects that migrated to type=module are affected, which I guess will be more and more projects on more recent Node-versions.

Note: downgrading Rambda / Rambdax to v8.x resolved this.

thanks for sharing this @cjk. I will try to fix that as it is a bad issue to keep open.

I did some work on that and I see that both type: module and webpack are using package.json's export.import property. If I fix one, the other is broken. It seems that I will have to rethink the build process and take from Ramda as it is working for both correctly. If I cannot fix it, I will prefer to keep the tree shaking and leave a note that it doesn't work with type = module.

I will post further updates soon.

cjk commented

Great, thanks for looking into this, seems to be a tricky issue.
Perhaps there will be changes on the Webpack-side too regarding this, as popularity of the ESM-way rises, making this easier to handle for Rambda.

I will most likely copy the build process of Ramda if that is not too much hustle as it might depend on folder structure. Thanks for understanding that tree-shaking is more important, because is the main reason this library exists on the first place.

I think I found half-solution, I will post updates as soon as I have something.

I think I found half-solution, I will post updates as soon as I have something.

I removed export property in package.json and it should work as before. No half solution was needed and tree shaking seems to work from my test runs. Still, I will wait for 7.5.0 release to confirm it before closing this issue.

7.5.0 seems to have fixed the issue for me.

Hi I know this is a closed issue right now, but just to let you know for a possible solution that also includes the "exports" statement:

According to the documentation:

Within the "exports" object, key order is significant. During condition matching, earlier entries have higher priority and take precedence over later entries. The general rule is that conditions should be from most specific to least specific in object order.

So, if you want webpack to pick up a different export than node you should, add a "webpack" condition as the first item the exports map.

Txs for the note. I will added check once again exports options to Rambda's TODO list as this information is new to me and can help solve some of the problems.