'Syntax error: SyntaxError: Cannot use import statement outside a module line: 1 file: Code.gs
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
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
