我有一個自定義模塊,我編寫并上傳到 NPM,它導出一個類在 AWS-CDK 項目中,我正在安裝該依賴項并嘗試導入它,但在構建“cdk Synth”期間出現以下錯誤Cannot use import statement outside a module這是來自 NPM 模塊的 JSON 包 "name": "@organization/cdk-organization-fe", "version": "1.0.2", "description": "Frontend construct for apps", "main": "stack.ts", "publishConfig": { "access": "restricted" }, "type":"module", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "tsc": "tsc" }, "author": "me, "license": "ISC", "dependencies": { "@aws-cdk/aws-cloudfront": "^1.73.0", "@aws-cdk/aws-s3": "^1.73.0", "@aws-cdk/aws-s3-deployment": "^1.73.0", "@aws-cdk/aws-certificatemanager": "^1.73.0", "@aws-cdk/aws-logs": "^1.73.0", "@aws-cdk/aws-lambda": "^1.73.0", "@aws-cdk/aws-iam": "^1.73.0", "aws-cdk": "^1.73.0", "path": "^0.12.7", "typescript": "^4.0.5" }}然后是使用該庫的 CDK 項目: "name": "showtix_fe", "version": "0.1.0", "bin": { "showtix_fe": "bin/showtix_fe.js" }, "type": "module", "scripts": { "build": "tsc", "watch": "tsc -w", "test": "jest", "cdk": "cdk" }, "devDependencies": { "@aws-cdk/assert": "1.73.0", "@types/jest": "^24.0.22", "@types/node": "10.17.5", "jest": "^24.9.0", "ts-jest": "^24.1.0", "aws-cdk": "^1.73.0", "ts-node": "^8.1.0", "typescript": "~3.7.2" }, "dependencies": { "@aws-cdk/core": "^1.73.0", "@organization/cdk-organization-fe": "^1.0.2", "source-map-support": "^0.5.16" }}已嘗試將 type:module 添加到包 JSON 也嘗試將其添加到 tsconfig 的 NPM 庫 "target": "ES2017", "module": "ESNEXT",
導入自定義 NPM 庫時無法在模塊外部使用 import 語句
料青山看我應如是
2023-08-05 11:07:22