gpt4 book ai didi

javascript - 导入自定义 NPM 库时无法在模块外使用导入语句

转载 作者:行者123 更新时间:2023-12-05 06:57:47 25 4
gpt4 key购买 nike

我有一个我编写并上传到导出类的 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",

最佳答案

我相信您希望模块中的“main”: 指向“stack.js”而不是“.ts”文件。然后您需要确保在使用该模块之前运行“tsc”。还要添加 "types": "stack.d.ts"。

关于javascript - 导入自定义 NPM 库时无法在模块外使用导入语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64822309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com