frida-compile icon indicating copy to clipboard operation
frida-compile copied to clipboard

ParseError: 'import' and 'export' may appear only with 'sourceType: module'

Open ychen96 opened this issue 3 years ago • 2 comments

frida-compile version:

10.2.4

trace.js

import { JNIInterceptor } from "jnitrace-engine";

// Attach to the JNI FindClass method
JNIInterceptor.attach("FindClass", {
    onEnter(args) {
        // called whenever the FindClass is about to be called
        console.log("FindClass method called");
        this.className = Memory.readCString(args[1]);
    },
    onLeave(retval) {
        // called whenever the FindClass method has finished executing
        console.log("\tLoading Class:", this.className);
        console.log("\tClass ID:", retval.get());
    }
});

command

frida-compile trace.js -o agent.js

log

[SyntaxError: 'import' and 'export' may appear only with 'sourceType: module'] {
  line: 1,
  column: 1,
  annotated: '\n' +
    '/Users/cyc/Projects/jnitrace/trace.js:1\n' +
    'import { JNIInterceptor } from "jnitrace-engine";\n' +
    '^\n' +
    "ParseError: 'import' and 'export' may appear only with 'sourceType: module'",
  stream: Labeled {
    _readableState: ReadableState {
      objectMode: true,
      highWaterMark: 16,
      buffer: BufferList { length: 0 },
      length: 0,
      pipes: [Labeled],
      pipesCount: 1,
      flowing: true,
      ended: false,
      endEmitted: false,
      reading: true,
      sync: false,
      needReadable: true,
      emittedReadable: false,
      readableListening: false,
      resumeScheduled: false,
      destroyed: false,
      defaultEncoding: 'utf8',
      awaitDrain: 0,
      readingMore: false,
      decoder: null,
      encoding: null
    },
    readable: true,
    _events: [Object: null prototype] {
      end: [Array],
      finish: [Function],
      error: [Function (anonymous)],
      data: [Function: ondata],
      _mutate: [Function]
    },
    _eventsCount: 5,
    _maxListeners: undefined,
    _writableState: WritableState {
      objectMode: true,
      highWaterMark: 16,
      finalCalled: false,
      needDrain: false,
      ending: false,
      ended: false,
      finished: false,
      destroyed: false,
      decodeStrings: true,
      defaultEncoding: 'utf8',
      length: 1,
      writing: true,
      corked: 0,
      sync: false,
      bufferProcessing: false,
      onwrite: [Function (anonymous)],
      writecb: [Function: nop],
      writelen: 1,
      bufferedRequest: null,
      lastBufferedRequest: null,
      pendingcb: 1,
      prefinished: false,
      errorEmitted: false,
      bufferedRequestCount: 0,
      corkedRequestsFree: [CorkedRequest]
    },
    writable: true,
    allowHalfOpen: true,
    _options: { objectMode: true },
    _wrapOptions: { objectMode: true },
    _streams: [ [DestroyableTransform] ],
    length: 1,
    label: 'syntax',
    [Symbol(kCapture)]: false
  },
  inputs: Set(7) {
    '/Users/cyc/Projects/jnitrace/trace.js',
    '/usr/local/lib/node_modules/frida-compile/node_modules/typescript/lib/lib.d.ts',
    '/usr/local/lib/node_modules/frida-compile/node_modules/typescript/lib/lib.es5.d.ts',
    '/usr/local/lib/node_modules/frida-compile/node_modules/typescript/lib/lib.dom.d.ts',
    '/usr/local/lib/node_modules/frida-compile/node_modules/typescript/lib/lib.webworker.importscripts.d.ts',
    '/usr/local/lib/node_modules/frida-compile/node_modules/typescript/lib/lib.scripthost.d.ts',
    '/Users/cyc/Projects/jnitrace/package.json'
  }
}

Anyone help? Thanks in advance.

ychen96 avatar Aug 27 '21 09:08 ychen96

Try creating a file called tsconfig.json in the project root directory and add this content:

{
  "compilerOptions": {
    "skipLibCheck": true
  }
}

NicolaiSoeborg avatar Sep 29 '21 20:09 NicolaiSoeborg

Hi @ychen96,

you can fix the issue by changing the import/export syntax in your NodeJS code from ES6 to ES5.

You can find many examples here: https://marketplace.visualstudio.com/items?itemName=tlevesque.import-to-require

In your situation probably it is sufficient to change:

import { JNIInterceptor } from "jnitrace-engine";

into:

const JNIInterceptor = require("jnitrace-engine").JNIInterceptor

Federico

federicodotta avatar Jan 14 '22 14:01 federicodotta