简体   繁体   English

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

[英]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.

相关问题 “SyntaxError: Cannot use import statement outside a module” 导入模块时导入 Jest 中的副作用模块 - "SyntaxError: Cannot use import statement outside a module" when importing a module which imports a module for side effects in Jest 导入 React = 'SyntaxError: 不能在模块外使用导入语句' - Importing React = 'SyntaxError: Cannot use import statement outside a module' JS npm 包,不能在模块外使用 import 语句 - JS npm package, Cannot use import statement outside a module 导入类时“不能在模块外使用导入语句”和“未捕获的语法错误:意外的标识符” - "Cannot use import statement outside a module" and "Uncaught SyntaxError: Unexpected identifier" when importing a class 在 Next.js 中导入 react-hook-mousetrap 时出现“无法在模块外使用导入语句”错误 - "Cannot use import statement outside a module" error when importing react-hook-mousetrap in Next.js 将已编译的 Typescript 文件相互导入时出现“未捕获的语法错误:无法在模块外使用 import 语句” - “Uncaught SyntaxError: Cannot use import statement outside a module” when importing compiled Typescript files to each other 导入自制 class 时“不能在模块外使用导入语句” - "Cannot use import statement outside a module" when importing a self made class “未捕获的语法错误:无法在模块外使用导入语句”导入时 ReactJS - "Uncaught SyntaxError: Cannot use import statement outside a module" When Importing ReactJS 未捕获的语法错误:导入 React 时无法在模块外使用导入语句 - Uncaught SyntaxError: Cannot use import statement outside a module" When Importing React 导入 ECMAScript 6 时出现“Uncaught SyntaxError: Cannot use import statement outside a module” - "Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM