Casl: rulesToQuery uses ES6 export

Created on 2 Jul 2018  路  9Comments  路  Source: stalniy/casl

I get this error (I am trying to use rulesToQuery for sequelize etc.):

/home/myuser/myproject/node_modules/@casl/ability/extra.js:134
export { rulesToQuery, permittedFieldsOf, packRules, unpackRules };
^^^^^^

SyntaxError: Unexpected token export

Can I somehow have a ES5 version of rulesToQuery as so far I did not compile with Babel and prefer things to stay that way (most recent node 10.5.0 still does not support export).

Most helpful comment

Yeah, with version 2.4.1 it is good now!

All 9 comments

Could you please show how you import extra package?

There is ES5 version for extra package as well but it looks like you try to do this:

require('@casl/ability/extra.js') // <-- with `.js` extension

But instead you should do this

require('@casl/ability/extra') // <-- WITHOUT `.js` extension!

Using this:

const {rulesToQuery} = require('@casl/ability/extra');

function ruleToQuery (rule) {
  if (JSON.stringify(rule.conditions).includes('"$all":')) {
    throw new Error('Sequelize does not support "$all" operator');
  }

  return rule.inverted ? {$not: rule.conditions} : rule.conditions;
}

module.exports = function toSequelizeQuery (ability, subject, action = 'read') {
  return rulesToQuery(ability, action, subject, ruleToQuery);
};

And in package.json:
"@casl/ability": "^2.3.0",

This is very strange :) On node 8+ it works as expected but in node 10+ it uses different logic resolution for node_modules. I will try to investigate this

Thanks for the issue!

Meanwhile the workaround is this:

require('@casl/ability/extra/') // <-- add slash at the end ;)

Thanks for the excellent & quick help!

Fyi, this is full error:

/home/myuser/myproject/node_modules/@casl/ability/extra.js:134
export { rulesToQuery, permittedFieldsOf, packRules, unpackRules };
^^^^^^

SyntaxError: Unexpected token export
    at new Script (vm.js:74:7)
    at createScript (vm.js:246:10)
    at Object.runInThisContext (vm.js:298:10)
    at Module._compile (internal/modules/cjs/loader.js:670:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
    at Module.require (internal/modules/cjs/loader.js:650:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/home/myuser/myproject/src/hooks/hooks_utils/toSequelizeQuery.js:1:86)
    at Module._compile (internal/modules/cjs/loader.js:702:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
    at Module.require (internal/modules/cjs/loader.js:650:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/home/myuser/myproject/src/hooks/abilities.js:2:26)
    at Module._compile (internal/modules/cjs/loader.js:702:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)

Could you please try @casl/[email protected] and let me know if all good?

Yeah, with version 2.4.1 it is good now!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SebUndefined picture SebUndefined  路  4Comments

nidkil picture nidkil  路  5Comments

arekucr picture arekucr  路  4Comments

rahulpsd18 picture rahulpsd18  路  7Comments

cerino-ligutom picture cerino-ligutom  路  6Comments