deepl-node icon indicating copy to clipboard operation
deepl-node copied to clipboard

oe.URLSearchParams is not a constructor Error

Open dcoales opened this issue 3 years ago • 12 comments

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):

  • OS: Windows 10
  • Browser Electron/19.1.8
  • Version [e.g. 22]
  • Node 16.15.0

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);

dcoales avatar Jan 07 '23 21:01 dcoales

Same Problem.

tomsour1s avatar Jan 09 '23 12:01 tomsour1s

same problem

blandinf avatar Jan 13 '23 13:01 blandinf

Same problem

teh0 avatar Jan 13 '23 15:01 teh0

Same problem

usbme avatar Jan 15 '23 18:01 usbme

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.

daniel-jones-deepl avatar Jan 16 '23 15:01 daniel-jones-deepl

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

glumb avatar Jan 19 '23 14:01 glumb

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.

daniel-jones-deepl avatar Jan 19 '23 15:01 daniel-jones-deepl

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

dsokopp avatar Jan 24 '23 15:01 dsokopp

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

dsokopp avatar Mar 01 '23 08:03 dsokopp

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

MariusGoth avatar Mar 29 '23 13:03 MariusGoth

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

seekuehe avatar Apr 17 '23 10:04 seekuehe

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.

JanEbbing avatar Jun 09 '23 22:06 JanEbbing