lokalise/node-lokalise-api

Unable to use `9.0.0` Error [ERR_REQUIRE_ESM]: require() of ES Module not supported

atifsaddique211f opened this issue · 4 comments

Describe the bug
Unable to use latest version 9.0.01, getting following error.
Error [ERR_REQUIRE_ESM]: require() of ES Module node_modules/@lokalise/node-api/dist/main.js from lokalise.service.js not supported. Instead change the require of main.js in lokalise.service.js to a dynamic import() which is available in all CommonJS modules.

To Reproduce
Steps to reproduce the behavior.

  • Install "@lokalise/node-api": "^9.0.0"
import { LokaliseApi } from '@lokalise/node-api';
import { config } from 'somewhere';

export class LokaliseService {
  private lokaliseApi: LokaliseApi;

  constructor() {
    this.lokaliseApi = new LokaliseApi({ apiKey: config.apiKey });
  }

  async fetchLokaliseKeys(page: number, keys?: string[]) {
    try {
      const requestBody: any = { page, project_id: config.projectId, limit: 20 };

      if (keys?.length) {
        requestBody.include_translations = 1;
        requestBody.filter_keys = keys?.join();
      }

      return await this.lokaliseApi.keys().list(requestBody);
    } catch (error) {
      this.logger.error(error);
    }
  }
}

Expected behavior
Should work as mentioned in docs here.
https://lokalise.github.io/node-lokalise-api/api/getting-started#installation-and-requirements

Your environment:

  • Node version: v16.17.0

Additional context
Add any other context about the problem here.

Are you using version 9 in an ESM module? If not, it probably will not work. https://lokalise.github.io/node-lokalise-api/additional_info/changelog as explained here, the latest version is a pure ESM module so it can only be used in other ESM modules. So, the choices are: convert your project to ESM, use dynamic import, or stay on version 8 which will receive major updates in the future.

And here's a sample repo with a ESM project that utilizes v9 https://github.com/bodrovis-learning/LokaliseForDevelopersNode/tree/master/API_Demo So, my guess is that your project is CommonJS therefore the best course of actions is to stay on v8 for now as it's fully compatible with the API

So, I'm going to close this one as this is an expected behavior: v9 will not work with CommonJS.