lihaibh / ditto

NPM library that helps managing MongoDB snapshots easily and efficiently
https://www.npmjs.com/package/mongodb-snapshot
MIT License
48 stars 6 forks source link

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module #13

Closed aptiway-dev-app closed 3 years ago

aptiway-dev-app commented 3 years ago

Following is my code to take backup of users collection;

import { MongoTransferer, MongoDBDuplexConnector, LocalFileSystemDuplexConnector } from 'mongodb-snapshot';

async function main() {
  const mongo_connector = new MongoDBDuplexConnector({
    connection: {
      uri: 'mongodb://localhost:27017',
      dbname: 'test',
    },
    assource: {
      collections: ['users'],
    },
  });
  const localfile_connector = new LocalFileSystemDuplexConnector({
    connection: {
      path: './backup.tar',
    },
  });

  const transferer = new MongoTransferer({
    source: mongo_connector,
    targets: [localfile_connector],
  });

  for await (const { total, write } of transferer) {
    console.log(`remaining bytes to write: ${total - write}`);
  }
}

main();

For above code, it generates following error:

internal/modules/cjs/loader.js:1149 throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath); ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: D:\src\backup2.js       
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1149:13)
    at Module.load (internal/modules/cjs/loader.js:977:32)
    at Function.Module._load (internal/modules/cjs/loader.js:877:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47 {
  code: 'ERR_REQUIRE_ESM'
}

node version - v12.16.3

Kindly help resolving this issue.

Once this runs, my final objective is to take backup from Mongodb hosted on atlas.

lihaibh commented 3 years ago

@aptiway-dev-app how do you run this application? with typescript (node --require ts-node/register) or are you compiling it? If you are compiling your application, what kind of module are you using? commonjs, amd, umd?

If you don't use typescript, you cant use the "import" statement, try to add to your package.json file: "type": "module",.

lihaibh commented 3 years ago

@aptiway-dev-app please let me know if you have any more issues, i close this issue for now