Skip to content

oe.URLSearchParams is not a constructor Error #17

Open

Description

@dcoales

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

Activity

  1. tomsour1s commented on Jan 9, 2023

    @tomsour1s

    Same Problem.

  2. blandinf commented on Jan 13, 2023

    @blandinf

    same problem

  3. teh0 commented on Jan 13, 2023

    @teh0

    Same problem

  4. usbme commented on Jan 15, 2023

    @usbme

    Same problem

  5. ghost added
    kind/featurerelated to a new feature
    on Jan 16, 2023
  6. glumb commented on Jan 19, 2023

    @glumb

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

  7. ghost removed
    kind/featurerelated to a new feature
    on Jan 19, 2023
  8. ghost added
    kind/bugrelated to a bug
    on Jan 19, 2023
  9. dsokopp commented on Jan 24, 2023

    @dsokopp

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

  10. dsokopp commented on Mar 1, 2023

    @dsokopp

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

  11. MariusGoth commented on Mar 29, 2023

    @MariusGoth

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

  12. seekuehe commented on Apr 17, 2023

    @seekuehe
    Contributor

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

  13. JanEbbing commented on Jun 9, 2023

    @JanEbbing
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugrelated to a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions