1

Lowdb@3.0 does not seem to work with jest@27.5.1, I end up with the following error:

Unexpected token 'export'
({"Object.":function(module,exports,require,__dirname,__filename,jest){export * from './adapters/JSONFile.js';

Does anyone know how to fix it?

My sample index.test.ts code:

import { Low, JSONFile } from 'lowdb'
it('test file upload', async () => { 
  const adapter = new JSONFile("db.json")  
  const db = new Low(adapter)  
  console.log("Test:", db)  
})

I have tried the following babel.config.js:

module.exports = {
    presets: [
      ['@babel/preset-env', {targets: {node: 'current'}}],
      '@babel/preset-typescript',
    ],
  };
Frank
  • 317
  • 2
  • 11
  • jest runs as commonjs, and lowdb is ESM only. it does not support any commonjs module. so to fix this, you either need to enable jest to run as ESM, or you need to use a babel plugin to transform all esm code to commonjs. i usually do the latter, but you the former may be appealing if you are on node > 14 and jest > 26. this tutorial probably contains everything you need to know: https://bl.ocks.org/rstacruz/511f43265de4939f6ca729a3df7b001c – r3wt Apr 06 '22 at 13:26
  • I have tried all the solutions from the link provided and none of them work. :( – Frank Apr 07 '22 at 14:28
  • I ended up using db code from this from: https://stackoverflow.com/questions/67649849/using-lowdb-results-in-err-require-esm/71768640#71768640 instead. – Frank Apr 07 '22 at 17:45

0 Answers0