iamkun / dayjs

⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API
https://day.js.org
MIT License
46.6k stars 2.28k forks source link

SyntaxError: Cannot use import statement outside a module #2667

Open k-fujishiro opened 3 months ago

k-fujishiro commented 3 months ago

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:

import dayjs from 'dayjs';

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

console.log(date_str);

appsscript.json:

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

package.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:

{
  "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"]
}
lukeselden commented 2 months ago

That error indicates that you're compiling into commonjs (require) instead of esm syntax (import) at some point in your compile chain. You may be able to change this in your rollup config, maybe by changing your target or by bundling dayjs into your distribution (Rollup's external vs bundled behavior). This readme may help