sibiraj-s / ngx-editor

🖋️ Rich Text Editor for angular using ProseMirror
https://sibiraj-s.github.io/ngx-editor/
MIT License
423 stars 182 forks source link

[Bug]: Cannot use import statement outside a module #527

Closed alexpivtorak closed 1 month ago

alexpivtorak commented 2 months ago

What happened?

There's a problem with Jest. Testing a standalone component which uses 'ngx-editor' falls with an error: "Cannot use import statement outside a module"

Version

v17.0.1

Angular Version

v17.1.3

What browsers are you seeing the problem on?

No response

Link to reproduce

No response

Relevant log output

node_modules\ngx-editor\node_modules\nanoid\index.browser.js:1        
    ({"Object.<anonymous>":function(module,exports,require,__dirna 
me,__filename,jest){import { urlAlphabet } from './url-alphabet/index.js' 

                    ^^^^^^ 

    SyntaxError: Cannot use import statement outside a module      

      20 | import { filter, Subject } from "rxjs"; 
      21 | 
    > 22 | import * as NgxEditor from "ngx-editor";

Willing to submit a PR?

No

sibiraj-s commented 1 month ago

Hi, it seems the issue is related to a configuration issue in package.json. You may need to add type:module in package.json. The package is built with ng-packager with no configuration changes to their defaults.

PhilKoepfer commented 1 month ago

Hi, type:module in package.json not worked in my case

I needed to add (but it slowed my tests down):

  transformIgnorePatterns: [
    'node_modules/ngx-editor/node_modules/nanoid/index.js',
  ],
sibiraj-s commented 1 month ago

Oh. I can remove the nanoid package. it just provides a very minor functionality.

PhilKoepfer commented 1 month ago

Nice, thank you!

teknekale commented 1 month ago

Hi, the transformIgnorePatterns does not work for me :( Any suggestion? I cannot run the test in anyway :(

 FAIL  some/path/pages/product-list-page/product-list-page.component.spec.ts
  ● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /some/path/.../node_modules/ngx-editor/fesm2022/ngx-editor.mjs:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import * as i0 from '@angular/core';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

       7 | import { noop } from 'rxjs';
       8 |
    >  9 | import { Editor, NgxEditorModule, Toolbar } from 'ngx-editor';
         | ^
      10 |
      11 | @UntilDestroy()
      12 | @Component({

{
  ...,
  "dependencies": {
    "@angular/animations": "17.1.3",
    "@angular/cdk": "17.1.2",
    "@angular/common": "17.1.3",
    "@angular/compiler": "17.1.3",
    "@angular/core": "17.1.3",
    "@angular/forms": "17.1.3",
    "@angular/material": "17.1.2",
    "@nx/angular": "17.3.2",
    "@nx/webpack": "17.3.2",
    "ngx-editor": "^17.0.1",
    "rxjs": "~7.8.0",
    "tslib": "^2.3.0",
    "zone.js": "0.14.4",
    ...
  },
 ...
}
kiwidude68 commented 1 month ago

Also getting a bunch of similar errors here, all blame seemingly being pointed to that nanoid component. Any chance a 17.0.2 can be pushed out with that removed soonish? I can't get the transformIgnorePatterns to work as a workaround - it is still failing the test suites.

 D:\Git\my-project\node_modules\ngx-editor\node_modules\nanoid\index.js:1
  ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { webcrypto as crypto } from 'node:crypto'
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module
sibiraj-s commented 1 month ago

Removed nanoid in v17.0.2

github-actions[bot] commented 1 week ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in the thread.