bitcoinjs / tiny-secp256k1

A tiny secp256k1 native/JS wrapper
MIT License
92 stars 54 forks source link

SyntaxError: Cannot use import statement outside a module #135

Closed bucko13 closed 5 months ago

bucko13 commented 5 months ago

I wanted to re-open a version of issue #73 as I'm currently running into it as well. The original issue is a couple years old now and it also looks like jest has been upgraded since to address this same issue in other context (e.g. this resolved issue). Unfortunately I still run into it when importing the tiny-secp256k1 library.

/packages/caravan-psbt/node_modules/tiny-secp256k1/lib/index.js:1
@caravan/psbt:test:watch:     ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { compare } from "uint8array-tools";
@caravan/psbt:test:watch:                                                                                       ^^^^^^
@caravan/psbt:test:watch: 
@caravan/psbt:test:watch:     SyntaxError: Cannot use import statement outside a module
@caravan/psbt:test:watch: 
@caravan/psbt:test:watch:       10 | import { toOutputScript } from "bitcoinjs-lib/src/address";
@caravan/psbt:test:watch:       11 | import * as bitcoin from "bitcoinjs-lib";
@caravan/psbt:test:watch:     > 12 | import * as ecc from "tiny-secp256k1";
@caravan/psbt:test:watch:          | ^
@caravan/psbt:test:watch:       13 |
@caravan/psbt:test:watch:       14 | bitcoin.initEccLib(ecc);
@caravan/psbt:test:watch:       15 |

You can see in this diff where I'm trying to add the library the code that throws the error. Without the import from tiny-secp25k1, I can run the test file no problem.

Here is the package.json relevant deps:

"bitcoinjs-lib": "^6.1.5",
"tiny-secp256k1": "^2.2.3"
"jest": "^29.4.1",
"ts-jest": "^29.1.2",

Interestingly if I try import another library from the bitcoinjs-lib "suite": import * as bip174 from 'bip174' that seems to work no problem.

As best I can tell an extra compiler like babel shouldn't be necessary with newer versions of jest and ts-jest as implied here. I could be wrong about that but just in case I also did try adding babel with a babel.config.js, but that didn't work either (and I'd rather avoid the extra config complexity as it is if possible).

Hopefully that provides enough details and perhaps you've seen this elsewhere since that last issue and might be able to point out something else I might've missed.

bucko13 commented 5 months ago

Ok, I was finally able to get jest to accept the import. But I am now running into issues with intEccLib. Will post that in another issue, but for this, here's how I got it working:

NODE_OPTIONS=--experimental-vm-modules npx jest --watch sr
// jest.config.ts
import type { JestConfigWithTsJest } from "ts-jest";

const config: JestConfigWithTsJest = {
  extensionsToTreatAsEsm: [".ts"],
  verbose: true,
  preset: "ts-jest/presets/default-esm",
  testEnvironment: "node",
  transform: {
    "^.+\\.tsx?$": ["ts-jest", { useESM: true }],
  },
  testPathIgnorePatterns: ["./dist"],
  setupFiles: ["<rootDir>/jest.setup.ts"],
};

export default config;
// jest.setup.ts
import "@inrupt/jest-jsdom-polyfills";
...
"type": "module",
...