Typescript import not resolved from apps script

760 views
Skip to first unread message

Yannick MOHL-CLAUZADE

unread,
Apr 3, 2023, 6:02:58 PM4/3/23
to Google Apps Script Community
Hello, i work on a app script project written in Typescript.
I have a file FfvlMailReader.ts file with an exported function:
```
export function getMembers(body: string): Array<Member> {
...
}
```
I have an other file 0_code.ts (with the main function):
```
import 'google-apps-script';
import { Member } from './Models';
import { getMembers } from './FfvlMailReader';

const mailSubjectLicence = "[FFVL] Récapitulatif des licences et/ou adhésions du";
const mailSubjectNotice = "[FFVL] Debut de prise de licence en ligne";
const mailLabelName = "new_memberships";

function run(): void {
const mailLabel: GoogleAppsScript.Gmail.GmailLabel = GmailApp.getUserLabelByName(mailLabelName)
const threads: GoogleAppsScript.Gmail.GmailThread[] = mailLabel.getThreads()

for (var i = 0; i < threads.length; i++) {
if (!threads[i].isUnread()) {
// skip message already read
continue ;
}
var subject: string = threads[i].getFirstMessageSubject()
if (subject.includes(mailSubjectLicence)) {
// ffvl licence validation
var body: string = threads[i].getMessages()[0].getPlainBody()
var members: Array<Member> = getMembers(body);

console.log(body)
console.log(members)

threads[i].markRead();
} else if (subject.includes(mailSubjectNotice)) {
// ffvl notice
threads[i].markRead();
}
}
}
```
In local all is good, project compile, but when i push with clasp push, in Google Apps script, the resolution of getMembers import is bad:

Google app script compiled version of 0_code.ts:
```
/ Compiled using undefined undefined (TypeScript 4.9.5)
var exports = exports || {};
var module = module || { exports: exports };
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//import 'google-apps-script';
//import { Member } from './Models';
//import { getMembers } from './FfvlMailReader';
const mailSubjectLicence = "[FFVL] Récapitulatif des licences et/ou adhésions du";
const mailSubjectNotice = "[FFVL] Debut de prise de licence en ligne";
const mailLabelName = "new_memberships";
function run() {
const mailLabel = GmailApp.getUserLabelByName(mailLabelName);
const threads = mailLabel.getThreads();
for (var i = 0; i < threads.length; i++) {
if (!threads[i].isUnread()) {
// skip message already read
continue;
}
var subject = threads[i].getFirstMessageSubject();
if (subject.includes(mailSubjectLicence)) {
// ffvl licence validation
var body = threads[i].getMessages()[0].getPlainBody();
var members = (0, FfvlMailReader_1.getMembers)(body);
console.log(body);
console.log(members);
threads[i].markRead();
}
else if (subject.includes(mailSubjectNotice)) {
// ffvl notice
threads[i].markRead();
}
}
}
```
When i start this function i have this error:

23:51:02
Erreur
ReferenceError: FfvlMailReader_1 is not defined

Nerio Villalobos

unread,
Apr 11, 2023, 4:20:19 AM4/11/23
to google-apps-sc...@googlegroups.com
It looks like the issue is with how your TypeScript code is compiled when you push it to the Apps Script environment. The ReferenceError: FfvlMailReader_1 is not defined error is indicating that the compiled code is unable to find the FfvlMailReader module, which is likely because the import statement is not being properly resolved.

One way to fix this is to use a tool like clasp-typescript to compile your TypeScript code before pushing it to the Apps Script environment. This tool will compile your TypeScript code into JavaScript, which can then be pushed to the Apps Script environment using clasp.

Here are the steps to use clasp-typescript to compile your code:

Install clasp-typescript by running npm install -g clasp-typescript.

Create a tsconfig.json file in the root directory of your project with the following contents:

{
  "compilerOptions": {
    "module": "CommonJS",
    "target": "ES6",
    "lib": ["es6", "dom"],
    "strict": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "skipLibCheck": true,
    "outDir": "./dist"
  },
  "exclude": ["node_modules"]
}

This configuration will tell the TypeScript compiler to output the compiled JavaScript files to a dist/ directory.

Run clasp-typescript in your project directory. This will compile your TypeScript code and output the compiled JavaScript files to the dist/ directory.

Push the compiled JavaScript files to the Apps Script environment using clasp push.

clasp push --force

The --force flag is used to overwrite any existing files in the Apps Script environment.

Once you've followed these steps, your TypeScript code should be properly compiled and ready to run in the Apps Script environment.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/de193c29-fe6c-47cd-95b2-d44273187197n%40googlegroups.com.


--
__________________________
Nerio Enrique Villalobos Morillo
Buenos Aires, Argentina

Andrew Mallin

unread,
Apr 11, 2023, 4:23:10 AM4/11/23
to google-apps-sc...@googlegroups.com
i really dont understand what you are saying?

--
Reply all
Reply to author
Forward
0 new messages