DeepLcom / deepl-node

Official Node.js library for the DeepL language translation API.
MIT License
358 stars 23 forks source link

oe.URLSearchParams is not a constructor Error #17

Open dcoales opened 1 year ago

dcoales commented 1 year ago

I have tried to use this library but when I run my code I get the following error in the browser console:

index.3240d547.js:10 Uncaught (in promise) TypeError: oe.URLSearchParams is not a constructor
    at je (index.3240d547.js:10:69597)
    at a.Translator.translateText (index.3240d547.js:10:73014)
    at Le (index.3240d547.js:10:71558)

Desktop (please complete the following information):

Additional context I'm using typescript and have no errors when I build my app - only when I run it. However, I'm not a professional programmer so it's possible I've installed the wrong version of something. My tsconfig.json file is below:

{
  "compilerOptions": {
    "lib": [
      "es2021",
      "dom"
    ],
    "module": "commonjs",
    "target": "es2021",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "node"
  }
}

and my package.json

{
  "name": "logseq-deepl-integration",
  "version": "0.0.1",
  "description": "Translate selected blocks via deepl",
  "main": "dist/index.html",
  "targets": {
    "main": false
  },
  "scripts": {
    "build": "parcel build --no-source-maps index.html --public-url ./",
    "dev": "parcel build --no-optimize index.html --public-url ./"
  },
  "author": "dcoales",
  "license": "MIT",
  "dependencies": {
    "@logseq/libs": "^0.0.10",
    "deepl-node": "^1.7.3"
  },
  "logseq": {
    "id": "logseq-deepl",
    "title": "Logseq Deepl Integration",
    "author": "dcoales",
    "description": "Translate selected blocks via deepl"
  },
  "devDependencies": {
    "@types/jest": "^29.0.3",
    "@types/node": "^18.11.9",
    "buffer": "^5.5.0",
    "events": "^3.3.0",
    "parcel": "^2.8.0",
    "path-browserify": "^1.0.1",
    "punycode": "^1.4.1",
    "querystring-es3": "^0.2.1",
    "stream-http": "^3.2.0",
    "ts-node": "^10.9.1",
    "typescript": "^4.8.3",
    "url": "^0.11.0",
    "util": "^0.12.5"
  }
}

I'm running this as a plugin to a tool called LogSeq. The plugin code is below. Any help would be much appreciated. Thanks.

import '@logseq/libs';
import {BlockEntity} from "@logseq/libs/dist/LSPlugin";
import { SettingSchemaDesc } from '@logseq/libs/dist/LSPlugin.user';
import * as deepl from 'deepl-node';
import {TextResult} from "deepl-node";

/**
 * entry
 */

function main() {

    logseq.useSettingsSchema(settings);

    logseq.Editor.registerSlashCommand('Get Jira Details for Selection', (_) => {
        return translate();
    })

    logseq.App.registerCommand('deeplTranslate', {
        key: 'deeplTranslate',
        label: 'Translate the selected lines',
        desc: 'Get translations for each of the currently selected lines',
        keybinding: {binding: 'mod+alt+t'}
    }, (e) => {
        return translate();
    })
}

async function translate() {
    let selection: BlockEntity[] | null = await logseq.Editor.getSelectedBlocks();
   //console.log(selection);
    if (!selection || selection.length === 0) {
        const block: BlockEntity | null = await logseq.Editor.getCurrentBlock();
        if (block){
            selection = [block];
        }
    }
    const lines:string[] = [];
    if (selection){
        for (let b of selection){
            lines.push(b.content);
        }
    }

    const authKey = logseq.settings?.APIToken;
    const translator = new deepl.Translator(authKey);
    const results:TextResult[] = await translator.translateText(lines,null,logseq.settings?.language);
    results.map((result: deepl.TextResult) => {
        console.log(result.text);
    });
}

const settings: SettingSchemaDesc[] = [
    {
        key: "language",
        description: "The language for the translated text",
        type: "string",
        default: "pl",
        title: "Translated text language",
    },
    {
        key: "APIToken",
        description: "Deepl API token",
        type: "string",
        default: "",
        title: "Deepl API token",
    }
];

logseq.ready(main).catch(console.error);
tomsour1s commented 1 year ago

Same Problem.

blandinf commented 1 year ago

same problem

teh0 commented 1 year ago

Same problem

usbme commented 1 year ago

Same problem

daniel-jones-deepl commented 1 year ago

Hi @dcoales, thanks for creating this issue. Thank you to the others for saying you're also affected.

I was able to reproduce the issue; it appears that URLSearchParams is not supported in the Node environment that logseq uses. By quickly removing URLSearchParams from deepl-node I was able to get it working in logseq in a limited way. Unfortunately to make this change without breaking other parts of the library is more difficult. I'll update when I have more information.

glumb commented 1 year ago

How is this labelled as enhancement when it is clearly a bug? 😀 We are running into the same error with node 14.18.1

daniel-jones-deepl commented 1 year ago

Hi @glumb, this problem doesn't occur in any "native" node version we support, rather with logseq that we haven't specifically tested in yet. One could argue that changing the lib to extend support for logseq would be an enhancement 😉

However, you're right, it's clearer if we label this a bug.

dsokopp commented 1 year ago

same issue here. Running into the error with node 16.x and 18.13.0

dsokopp commented 1 year ago

Any updates on this @daniel-jones-deepl? Cant't use this library with the bug and our configuration..

MariusGoth commented 1 year ago

@daniel-jones-deepl Are there any updates on this issue?

seekuehe commented 1 year ago

Daniel is out at the moment, but our team will look into this

JanEbbing commented 1 year ago

A workaround is to put

import * as url from "url";
url.URLSearchParams = URLSearchParams;

at the top of the file (e.g. at the end of the imports, outside the main function). That allows me to translate with the plugin. The issue is webpack injecting a different version of url, I am investigating a clean way to fix this.