-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SyntaxError: Cannot use import statement outside a module #31
Comments
I had a simmilar issue while trying to import Good news is that I managed to make it work. I did the following additional steps: rollup.config.mjs
Code.ts:
package.json:
Unfortunately If someone would be able to help and find a way to move the dependency modules to a separate file, that would be amazing. |
Here's something I managed to get to work: Create two outputs with Rollup, one for your Code.ts and one for an "external" jsonata lib. Since AppsScript doesn't know modules, we'll have to use some trickery. Step 1 - Create a jsonata-lib.ts filejsonata-lib.ts import 'jsonata'; Step 2 - Treat jsonata as an external library in Code.tsCode.ts (example) // import the jsonata type
import type jsonataNS from 'jsonata';
// declare jsonata as a global variable
declare global {
const jsonata: typeof jsonataNS;
}
async function run() {
const data = {
example: [{ value: 4 }, { value: 7 }, { value: 13 }],
};
// use jsonata
const expression = jsonata('$sum(example.value)');
const result = await expression.evaluate(data); // returns 24
console.log(result);
} Step 3 - Configure Rollup to produce two outputsrollup.config.mjs import cleanup from 'rollup-plugin-cleanup';
import license from 'rollup-plugin-license';
import prettier from 'rollup-plugin-prettier';
import typescript from 'rollup-plugin-typescript2';
import nodeResolve from '@rollup/plugin-node-resolve';
import { fileURLToPath } from 'url';
export default [
{
input: 'src/jsonata-lib.ts',
output: {
dir: 'dist',
format: 'es',
},
plugins: [
cleanup({ comments: 'none', extensions: ['.ts'] }),
nodeResolve(),
typescript(),
],
},
{
input: 'src/Code.ts',
output: {
dir: 'dist',
format: 'es',
},
plugins: [
cleanup({ comments: 'none', extensions: ['.ts'] }),
license({
banner: {
content: {
file: fileURLToPath(new URL('license-header.txt', import.meta.url)),
},
},
}),
typescript(),
prettier({ parser: 'typescript' }),
],
context: 'this',
},
]; |
@k-fujishiro |
I am new to make scripts in Node.js environment. I started to develope GAS application in the environment created by Apps Script in IDE (ASIDE). As I need to use the library Dayjs, I installed that and make scripts according to the official documentation. Then I got the error message "SyntaxError: Cannot use import statement outside a module".
If anyone has any information that can help solve this please let me know.
More information is below:
index.ts:
appsscript.json:
package.json:
tsconfig.json:
The text was updated successfully, but these errors were encountered: