Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't import all operators in NodeJS #144

Closed
stefanholzapfel opened this issue Jun 11, 2020 · 9 comments
Closed

Can't import all operators in NodeJS #144

stefanholzapfel opened this issue Jun 11, 2020 · 9 comments

Comments

@stefanholzapfel
Copy link
Contributor

Hi!

I'm on
NodeJS version 14,
Mingo version 3.0.2

I bundle the app with Webpack version 5 (also tried with 4, same effect) and run it via nodemon (2.0.4).

I try to import all operators like that (bundle size is no problem for me):

import { OperatorType, useOperators } from 'mingo/core';
import * as pipelineOperators from 'mingo/operators/pipeline';
import * as projectionOperators from 'mingo/operators/projection';
import * as queryOperators from 'mingo/operators/query';
import * as expressionOperators from 'mingo/operators/expression';
import * as accumulatorOperators from 'mingo/operators/accumulator';

useOperators(OperatorType.PIPELINE, pipelineOperators);
useOperators(OperatorType.PROJECTION, projectionOperators);
useOperators(OperatorType.QUERY, queryOperators);
useOperators(OperatorType.EXPRESSION, expressionOperators);
useOperators(OperatorType.ACCUMULATOR, accumulatorOperators);    

Some of the imports work, some (like in this example arithmetic) throw:

(function (exports, require, module, __filename, __dirname) { export * from './arithmetic';
                                                              ^^^^^^
SyntaxError: Unexpected token 'export'
    at new Script (vm.js:99:7)
    at createScript (vm.js:247:10)
    at Object.runInThisContext (vm.js:295:10)
    at wrapSafe (internal/modules/cjs/loader.js:1104:15)
    at Module._compile (internal/modules/cjs/loader.js:1164:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)
    at Module.require (internal/modules/cjs/loader.js:1089:19)
    at require (internal/modules/cjs/helpers.js:73:18)
[nodemon] app crashed - waiting for file changes before starting...

Other es6 imports work fine in my app. It also seem to have somehting to do with import ordering (which is weird)?

Any ideas?

Thanks!

@stefanholzapfel stefanholzapfel changed the title Can't import operators in NodeJS Can't import all operators in NodeJS Jun 11, 2020
@j
Copy link

j commented Jun 18, 2020

Not sure if this is unrelated, but I couldn't get tests to start b/c this 3.x branch of mingo. I would get a memory leak somehow. Reverting back to the older version of mingo fixes my issue.

@stefanholzapfel
Copy link
Contributor Author

To be more precise, I get errors with:
useOperators(OperatorType.EXPRESSION, expressionOperators);
and
useOperators(OperatorType.ACCUMULATOR, accumulatorOperators);

Other operators can be imported.

@kofrasa
Copy link
Owner

kofrasa commented Jun 19, 2020

@stefanholzapfel thanks for reporting the issues. The error seems related to module loading. Starting from 3.0.0 there is a dependency on the esm which allows using the ES6 import syntax, which is the target of the Typescript build.

If you are getting a syntax error, my guess is that esm is not installed in your environment. Assuming you got that working correctly, then something else must be causing the import of expression and accumulator operators to fail.

Can you provide the exception for those cases and how are you bundling your project?

@kofrasa
Copy link
Owner

kofrasa commented Jun 19, 2020

Not sure if this is unrelated, but I couldn't get tests to start b/c this 3.x branch of mingo. I would get a memory leak somehow. Reverting back to the older version of mingo fixes my issue.

@j please open a new issue with the details of the memory leak.

@j
Copy link

j commented Jun 19, 2020

@kofrasa #145 might be related.

@kofrasa
Copy link
Owner

kofrasa commented Jun 19, 2020

I am unable to repro the import errors. Below is my example using esm from my terminal.

  ~ node -r esm
Welcome to Node.js v14.4.0.
Type ".help" for more information.
> import 'mingo/init/system'
undefined
> import * as pkg from 'mingo/package.json'
undefined
> pkg.version
'3.0.2'
> import { Query } from 'mingo/query'
undefined
> let q = new Query({a: {$gt: 5}})
undefined
> q
Query {
  __criteria: { a: { '$gt': 5 } },
  __config: { idKey: '_id' },
  __compiled: [ [Function (anonymous)] ]
}
> q.test({a:10})
true
> q.test({a:1})
false
>
> import * as expressionOperators from 'mingo/operators/expression'
undefined
> useOperators(OperatorType.EXPRESSION, expressionOperators)
undefined
>

@stefanholzapfel
Copy link
Contributor Author

stefanholzapfel commented Jun 26, 2020

Hi Kofrasa!

Yes you are right, I don't have esm installed, and its a build from Typescript via Webpack.

I don't know the different module builders very well, but I don't unterstand why the ES export syntax works with pipeline, projection and query operators and not with accumulators and expressions. I also use other packages with ES syntax...

The exact error for expression operators is:

C:\xxxx\node_modules\mingo\operators\accumulator\index.js:4
export * from './addToSet';
^^^^^^

SyntaxError: Unexpected token 'export'
    at new Script (vm.js:99:7)
    at createScript (vm.js:247:10)
    at Object.runInThisContext (vm.js:295:10)
    at wrapSafe (internal/modules/cjs/loader.js:1104:15)
    at Module._compile (internal/modules/cjs/loader.js:1164:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)
    at Module.require (internal/modules/cjs/loader.js:1089:19)
    at require (internal/modules/cjs/helpers.js:73:18)
[nodemon] app crashed - waiting for file changes before starting...

Thank you!

@kofrasa
Copy link
Owner

kofrasa commented Jul 16, 2020

I am unable to repro this but the latest version 3.0.3 removes the reliance on the esm module which is likely the root cause.

Closing this issue. Please reopen if the issue still persists after upgrade.

@kofrasa kofrasa closed this as completed Jul 16, 2020
@stefanholzapfel
Copy link
Contributor Author

Thanks, fixed with new version.

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

No branches or pull requests

3 participants