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

SyntaxError: Cannot use import statement outside a module #528

Closed
GustavoEklund opened this issue Jan 20, 2022 · 1 comment
Closed

SyntaxError: Cannot use import statement outside a module #528

GustavoEklund opened this issue Jan 20, 2022 · 1 comment

Comments

@GustavoEklund
Copy link

Importing fileTypeFromBuffer using ES6 module with Node + TypeScript + jest throws an error, am I doing something wrong? (I also tried importing fileTypeFromFile and give the same error)

(error throws only while importing modules from 'file-type' package, other packages haven't given any trouble)

● The 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.

    Details:

    /urs/src/app/node_modules/file-type/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import * as strtok3 from 'strtok3';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | import { EmailAdapter, EmailAddress } from '@/any/directory'
      2 |
    > 3 | import { fileTypeFromBuffer } from 'file-type'
        | ^
      4 |

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
      at Object.<anonymous> (any/directory/file.ts:3:1)

My npm script used for run tests:

    "test": "jest --passWithNoTests --no-cache --runInBand",
    "test:watch": "npm test -- --watch",

My jest.config.js:

module.exports = {
  collectCoverageFrom: [
    '<rootDir>/src/**/*.ts',
    '!<rootDir>/src/main/**',
    '!<rootDir>/src/**/index.ts',
  ],
  coverageDirectory: 'coverage',
  coverageProvider: 'babel',
  moduleNameMapper: {
    '@/tests/(.+)': '<rootDir>/tests/$1',
    '@/(.+)': '<rootDir>/src/$1',
  },
  testMatch: ['**/*.spec.ts'],
  roots: ['<rootDir>/src', '<rootDir>/tests'],
  transform: {
    '\\.ts': 'ts-jest',
  },
  clearMocks: true,
}

My tsconfig.json:

{
  "compilerOptions": {
    "outDir": "dist",
    "rootDirs": ["src", "tests"],
    "target": "ES2021",
    "sourceMap": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "baseUrl": "src",
    "paths": {
      "@/*": ["*"],
      "@/tests/*": ["../tests/*"]
    },
    "strict": true,
    "noImplicitOverride": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  },
  "include": ["src", "tests", "jest.config.js", "jest.integration.config.js"],
}

Package versions:

    "@types/jest": "^27.0.3",
    "@types/node": "^17.0.2",
    "jest": "^27.4.5",
    "ts-jest": "^27.1.2",
    "typescript": "^4.5.4",
    "file-type": "^17.1.1",

Node version: v16.13.2

@sindresorhus
Copy link
Owner

This is a problem with Jest not properly supporting native ESM.

https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

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

2 participants