iamkun / iamkun/dayjs

SyntaxError: Cannot use import statement outside a module

Open
#2,667 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
48.7k
Forks
2.8k
PR merge metrics
No merged PRs in 30d

Description

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)](https://github.com/google/aside). As I need to use the library Dayjs, I installed that and make scripts according to [the official documentation](https://day.js.org/docs/en/installation/typescript). 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:

``` typescript
import dayjs from 'dayjs';

const now = dayjs();
const date_str = now.format('YYYY/MM/DD HH:mm:ss');

console.log(date_str);
```

appsscript.json:

``` json
{
"timeZone": "Asia/Tokyo",
"dependencies": {
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"webapp": {
"executeAs": "USER_DEPLOYING",
"access": "ANYONE_ANONYMOUS"
}
}
```

package.json:

``` json
{
"name": "gas-sample-2",
"version": "0.0.0",
"description": "",
"main": "build/index.js",
"license": "Apache-2.0",
"keywords": [],
"type": "module",
"scripts": {
"clean": "rimraf build dist",
"lint": "npm run license && eslint --fix --no-error-on-unmatched-pattern src/ test/",
"bundle": "rollup --no-treeshake -c rollup.config.mjs",
"build": "npm run clean && npm run bundle && ncp appsscript.json dist/appsscript.json",
"license": "license-check-and-add add -f license-config.json",
"test": "jest test/ --passWithNoTests --detectOpenHandles",
"deploy": "npm run lint && npm run test && npm run build && ncp .clasp-dev.json .clasp.json && clasp push -f && source .env && clasp deploy $DEV_DEPLOYMENT_ID",
"deploy:prod": "npm run lint && npm run test && npm run build && ncp .clasp-prod.json .clasp.json && clasp push && source .env && clasp deploy $PROD_DEPLOYMENT_ID"
},
"engines": {
"node": ">=12"
},
"dependencies": {
"@google/clasp": "^2.4.2",
"@types/google-apps-script": "^1.0.83",
"@types/jest": "^29.5.12",
"@typescript-eslint/eslint-plugin": "^7.11.0",
"dayjs": "^1.11.11",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"gts": "^5.3.0",
"jest": "^29.7.0",
"license-check-and-add": "^4.0.5",
"ncp": "^2.0.0",
"prettier": "^3.2.5",
"rimraf": "^5.0.7",
"rollup": "^4.18.0",
"rollup-plugin-cleanup": "^3.2.1",
"rollup-plugin-license": "^3.4.0",
"rollup-plugin-prettier": "^4.1.1",
"rollup-plugin-typescript2": "^0.36.0",
"ts-jest": "^29.1.4",
"typescript": "^5.4.5"
}
}
```

tsconfig.json:

``` json
{
"compilerOptions": {
"target": "ES2022",
"module": "ES2022",
"lib": ["ES2022"],
"sourceMap": true,
"moduleResolution": "node",
"outDir": "dist",
"rootDir": ".",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"allowSyntheticDefaultImports": true,
},
"include": ["src/**/*", "test/**/*", "rollup.config.mjs"]
}
```

Contributor guide

Open the contributing guide

Research direction

Start by reviewing index.ts, package.json, tsconfig.json, and the Rollup entry configuration, then reproduce the build and deployment flow described in the scripts. Trace how the TypeScript import is emitted and loaded by the Apps Script V8 runtime. Done means the deployed Day.js example runs without the reported SyntaxError.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
backend, build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.