question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SyntaxError: Cannot use import statement outside a module

See original GitHub issue

Jest doesnt work anymore with ngx-toaster after updating to Version 14.x.x, caused by using “mjs”.

There is currently no angular jest preset, that works with ngx-toaster *.mjs

Jest Error:

● 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:

C:\Users\dmitry.prudnikov\IdeaProjects\biskin-kit\node_modules\ngx-toastr\fesm2020\ngx-toastr.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

   5 | import { BsUserManagementDeleteModalComponent } from './delete-modal/user-management-delete-modal.component';
   6 | import { BsUserManagementPasswordResetModalComponent } from './password-reset-modal/user-management-password-reset-modal.component';
>  7 | import { ToastrService } from 'ngx-toastr';
     | ^
   8 | import { BS_USER_SERVICE_TOKEN } from '../../services/user-management-tokens';` 

Jest Config:

“use strict”; module.exports = { testMatch: [‘**/+(*.)+(spec|test).+(ts|js)?(x)’], resolver: ‘@nrwl/jest/plugins/resolver’, moduleFileExtensions: [‘ts’, ‘js’,‘mjs’, ‘html’], coverageReporters: [‘html’], transform: { ‘^.+\.(ts|js|mjs|html)$’: ‘ts-jest’, }, testEnvironment: ‘jsdom’, };

module.exports = { name: ‘user-management’, preset: ‘…/…/…/jest.preset.js’, setupFilesAfterEnv: [‘<rootDir>/src/test-setup.ts’], globals: { ‘ts-jest’: { tsconfig: ‘<rootDir>/tsconfig.spec.json’, stringifyContentPathRegex: ‘\.(html|svg)$’, diagnostics: { warnOnly: true }, astTransformers: [ ‘jest-preset-angular/build/InlineFilesTransformer’, ‘jest-preset-angular/build/StripStylesTransformer’, ], isolatedModules: true, }, }, coverageDirectory: ‘…/…/…/coverage/libs/biskin-kit/user-management’, transform: { ‘^.+\.(ts|js|mjs|html)$’: ‘jest-preset-angular’, ‘^.+\.m?js$’: ‘ts-jest’, }, transformIgnorePatterns: [‘<rootDir>/node_modules/(?!@ngx-toastr)’], snapshotSerializers: [ ‘jest-preset-angular/build/serializers/no-ng-attributes’, ‘jest-preset-angular/build/serializers/ng-snapshot’, ‘jest-preset-angular/build/serializers/html-comment’, ], moduleFileExtensions: [‘ts’, ‘tsx’, ‘js’, ‘mjs’, ‘jsx’, ‘json’, ‘html’], modulePathIgnorePatterns: [‘<rootDir>/dist/’], moduleNameMapper: { ‘.+\.(mdx?)$’: ‘identity-obj-proxy’, }, clearMocks: true, };

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:14
  • Comments:7

github_iconTop GitHub Comments

1reaction
MKlimentowiczcommented, Dec 10, 2021

The error disappears when I downgrade the dependency to 14.1.3

package.json:

"ngx-toastr": "14.1.3"

0reactions
nschimekcommented, Jan 20, 2022

The error disappears when I downgrade the dependency to 14.1.3

package.json:

"ngx-toastr": "14.1.3"

Thank you, this solved the issue for me for now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Uncaught SyntaxError: Cannot use import statement outside ...
This means that you're using the native source code in an unaltered/unbundled state, leading to the following error: Uncaught SyntaxError: ...
Read more >
How to fix "cannot use import statement outside a module"
I stumbled on this error: Uncaught SyntaxError: cannot use import statement outside a module while importing a function from a JavaScript file.
Read more >
Cannot use import statement outside a module [React ...
When building a web application, you may encounter the SyntaxError: Cannot use import statement outside a module error.
Read more >
How to solve: cannot use import statement outside a module
When you see the error message Uncaught SyntaxError: cannot use import statement outside a module, it means you're using an import statement ......
Read more >
Cannot use import statement outside a module - Future Studio
A common error with modules is the “Uncaught SyntaxError: Cannot use import statement outside a module”. This error means you must ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found