5

I wanted to create a script, which would send data from the google form to the discord webhook in google apps script. But I receive the error in the first line:

import { client as _client } from 'discord.js';
const client = new _client();

Here is the error:

Syntax error: SyntaxError: Cannot use import statement outside a module line: 1 file: Code.gs
6
  • 1
    your error stems from a flawed import statement. It looks like you are using the common.js type which uses require instead of import. You can read up on it here Commented May 4, 2021 at 19:41
  • im using google apps script, if i will use require, ill receive ReferenceError: require is not defined Commented May 4, 2021 at 21:25
  • Related: stackoverflow.com/q/58211880/1595451 Commented May 4, 2021 at 21:27
  • It doesn't help me. I don't know where I should type "type": "module". Commented May 5, 2021 at 15:25
  • ? stackoverflow.com/questions/47639463/… Commented May 5, 2021 at 17:46

2 Answers 2

1

discord.js depends on Node.js. It cannot run on Google Apps Script.

If you want to contact a discord webhook, then do so directly using the UrlFetchApp API as per the External APIs guide.

Sign up to request clarification or add additional context in comments.

Comments

0

Currently, Google Apps Script does not support ES modules. Hence the typical export/import pattern cannot be used and will fail.

One way of handling this is to use rollup.js to bundle your project into one single JavaScript file.

The trick here is to make sure not to export any functions in your entry point code, e.g. index.ts, and to prevent any generation of export statement in the final bundle (see the custom rollup plugin in the rollup.config.js below).

See code: https://github.com/google/clasp/blob/master/docs/esmodules.md

See also: The ULTIMATE Guide to NPM Modules in Google Apps Script

Or even: Using npm modules inside of Apps Script

1 Comment

This won't help. discord.js depends on Node.js APIs that rollup cannot bundle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.