[英]Cannot use import statement outside a module when importing a custom NPM library
I have a custom module that I wrote and uploaded to NPM which exports a class我有一个自定义模块,我编写并上传到 NPM,它导出一个类
In an AWS-CDK Project, I am installing that dependency and trying to import it but I get the following error during build "cdk synth"在 AWS-CDK 项目中,我正在安装该依赖项并尝试导入它,但在构建“cdk synth”期间出现以下错误
Cannot use import statement outside a module
Here is package JSON from the NPM 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"
}
}
and then the CDK project using the library:然后是使用库的 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"
}
}
have tried adding type:module to the package JSON also have tried adding this to the NPM library for the tsconfig已尝试将 type:module 添加到包 JSON 也已尝试将其添加到 tsconfig 的 NPM 库中
"target": "ES2017",
"module": "ESNEXT",
I believe you want your "main": in the module to point to "stack.js" not the ".ts" file.我相信你希望你的“main”:在模块中指向“stack.js”而不是“.ts”文件。 Then you need to make sure to run 'tsc' before using the module.
然后你需要确保在使用模块之前运行'tsc'。 Also add "types": "stack.d.ts" too.
还要添加“类型”:“stack.d.ts”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.