Tampermonkey / tampermonkey

Tampermonkey is the most popular userscript manager, with over 10 million users. It's available for Chrome, Microsoft Edge, Safari, Opera Next, and Firefox.
GNU General Public License v3.0
4.24k stars 419 forks source link

`TypeError` when importing an external JS #1434

Closed sz3lbi closed 2 years ago

sz3lbi commented 2 years ago

Expected Behavior

Working code of the required script.

Actual Behavior

TypeError in the browser console. It occurs exactly in the line of the required script code. I don't even have to execute anything from the script. Actually, the error is not dependent of the requested script. It occurs in the attached case (typescript-json-serializer) as well as in the case of importing reflect-metadata and then class-transformer. Then it shows the line of the class-transformer script code as the faulty one.

In the Node.js with TypeScript there are no errors at all. Webpack compilation also goes smoothly and that's the step when the UserScript header is generated. Issues appear in the Tampermonkey and they are not dependent of the script code as the UserScript header is enough for them to occur.

Maybe I am doing something wrong? Thanks in advance for your tips.

Firefox: Uncaught (in promise) TypeError: undefined is not a non-null object

Chromium: Uncaught (in promise) TypeError: Object.defineProperty called on non-object

Specifications

Script

// ==UserScript==
// @name test
// @version 1.0.0
// @author szelbi
// @match *://*/*
// @require https://cdn.jsdelivr.net/npm/typescript-json-serializer@4.0.0
// ==/UserScript==
7nik commented 2 years ago

exports require(...)

Unfortunately, userscripts do not support modules.

sz3lbi commented 2 years ago

Okay, thanks.
Fortunately, I already use the Webpack, so after removing the module from externals in the Webpack config it just gets bundled with the script itself and I don't need to require any externals. Then the script code gets a lot longer, but everything works fine.