lokalise / node-lokalise-api

Lokalise API v2 Node.js client.

Home Page:https://lokalise.github.io/node-lokalise-api

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Example lokalise-api-hello-world-app-with-node does not work

ridestoredev opened this issue · comments

Describe the bug
Example from https://developers.lokalise.com/docs/lokalise-api-hello-world-app-with-node does not work with node v18

If i use import word, I see:

node --experimental-modules translations/upload.js
(node:16975) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
translations/upload.js:1
import { LokaliseApi } from "@lokalise/node-api";
^^^^^^

SyntaxError: Cannot use import statement outside a module

with require, as docs say:

const { LokaliseApi } = require('@lokalise/node-api');
                        ^

Error [ERR_REQUIRE_ESM]: require() of ES Module node_modules/@lokalise/node-api/dist/main.js from translations/upload.js not supported.
Instead change the require of main.js in translations/upload.js to a dynamic import() which is available in all CommonJS modules.

To Reproduce
Steps to reproduce the behavior.

Expected behavior
A clear and concise description of what you expected to happen.

Your environment:

  • Node version 18

Additional context
Add any other context about the problem here.

To make it working, the upload.js should look like this:

require('dotenv').config({ path: "./.env.local" }); // if next.js is using

const { LOKALISE_API_TOKEN } = process.env;
const lokaliseProjectId = 'some-project-id';

const englishI18nFile = require('./en.json');
const filename = 'en.json';
const lang_iso = 'en';
(async function () {
  const LokaliseApi = await (import('@lokalise/node-api').then(m => m.LokaliseApi)); // js module
  const lokaliseApi = new LokaliseApi({ apiKey: LOKALISE_API_TOKEN});
  try {
    const data_base64 = Buffer.from(JSON.stringify(englishI18nFile)).toString("base64");
    process = await lokaliseApi.files().upload(lokaliseProjectId,
        { data: data_base64, filename, lang_iso }
    );
    console.log('upload process --->', process.status);
  } catch (error) {
    console.log('ERROR --->', error);
  }
})();

Well, the first error actually explains what's going on: set "type": "module" in the package.json. This is because the latest version of this SDK is an ESM module as mentioned in the README. So, one should either use it with ESM project, or use a dynamic import. If that's not feasible, you can stay on v8 which is supported and has all the features of v9. So, this behavior is pretty much expected. I'll add a note to the DevHub article. Thank you for reporting this!

Here's that part from the README for reference:

Please note that starting from version 9 this SDK is a pure ESM module. It does not provide a CommonJS export (require) anymore. Therefore you should either convert your project to ESM, use dynamic import, or stay on version 8 which we are fully supporting.