3

In my Create React App when importing the lightweight-chars like so everything's working:

import { createChart } from 'lightweight-charts';

However, when running tests with:

yarn test --all

this error is shown:

● Test suite failed to run

.../node_modules/lightweight-charts/dist/lightweight-charts.esm.development.js:7
import { bindToDevicePixelRatio } from 'fancy-canvas/coordinate-space';
^^^^^^

SyntaxError: Cannot use import statement outside a module

  at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
  ...

Please suggest if looks familiar.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
valk
  • 9,363
  • 12
  • 59
  • 79

3 Answers3

2

Currently it's impossible to use/import lightweight-charts in nodejs context. You can subscribe on https://github.com/tradingview/lightweight-charts/issues/488 to be notified when it'll be solved.

timocov
  • 1,088
  • 6
  • 16
2

It might not be the perfect solution to fix the root cause, but I was able to fix the errors via jest option in package.json.

...
"transformIgnorePatterns": [
      "node_modules/(?!(lightweight-charts|fancy-canvas)/)"
    ]

Till Kolter
  • 602
  • 1
  • 9
  • 13
0

This is the only thing that worked for me:

npm test -- --transformIgnorePatterns "node_modules/(?!(lightweight-charts|fancy-canvas)/)"

This worked for me because I have an unejected create react app project and therefore transformIgnorePatterns is not available via jest.config.js or the package.json.

Naruto Sempai
  • 6,233
  • 8
  • 35
  • 51