TypeScript中可重用的pulumi组件存在问题(无法在模块外部使用import语句)

cgyqldqp  于 2022-12-24  发布在  TypeScript
关注(0)|答案(1)|浏览(169)

我在开始使用可重用组件时遇到了麻烦,我试图使用TypeScript来创建一个简单的可重用组件资源,我可以使用它在AWS上部署S3 bucket。
为了说明我遇到的问题,我创建了两个公开的GitHub repos:

我用pulumi new aws-typescript创建了这两个函数。
对于pulumi-beta,我使用以下代码来定义我的组件资源:

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

export interface S3BucketArgs {
  name: string;
}

export class S3Bucket extends pulumi.ComponentResource {
  public readonly bucket: aws.s3.Bucket;

  constructor(name: string, args: S3BucketArgs, opts?: pulumi.ComponentResourceOptions) {
    super("my:modules:S3Bucket", name, {}, opts);

    this.bucket = new aws.s3.Bucket(name, {
      bucket: args.name,
    }, { parent: this });
  }
}

pulumi-alpha中,我使用以下命令安装了pulumi-beta this包:

npm i git+https://github.com/briancaffey/pulumi-beta.git

然后我在index.ts中这样使用它:

import * as pulumi from "@pulumi/pulumi";
import { S3Bucket } from "pulumi-beta";

const bucket = new S3Bucket("my-bucket", {
  name: "my-example-pulumi-bucket",
});

export const bucketName = bucket.bucket.id;

然后运行tsc,可以看到它在bin目录中构建了应用程序:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.bucketName = void 0;
const pulumi_beta_1 = require("pulumi-beta");
const bucket = new pulumi_beta_1.S3Bucket("my-bucket", {
    name: "my-example-pulumi-bucket",
});
exports.bucketName = bucket.bucket.id;
//# sourceMappingURL=index.js.map

现在,我想运行pulumi preview,看看结果如何,但是我得到了这个错误:

~/git/github/pulumi-alpha$ pulumi preview
Previewing update (dev)

View Live: https://app.pulumi.com/briancaffey/pulumi-alpha/dev/previews/f8469d80-a9d5-4e95-9d9f-8a5830220f5c

     Type                 Name              Plan       Info
 +   pulumi:pulumi:Stack  pulumi-alpha-dev  create     1 error

Diagnostics:
  pulumi:pulumi:Stack (pulumi-alpha-dev):
    error: Running program '/Users/brian/git/github/pulumi-alpha' failed with an unhandled exception:
    /Users/brian/git/github/pulumi-alpha/node_modules/pulumi-beta/index.ts:1
    import * as pulumi from "@pulumi/pulumi";
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

回到pulumi-beta存储库,我将"type": "module",添加到package.json,推送到GitHub,然后重新安装pulumi-alpha中的所有内容:

rm -rf bin
rm -rf node_modules
rm package-lock.json
npm i
tsc

再次运行pulumi preview,我得到了与之前相同的错误:

Diagnostics:
  pulumi:pulumi:Stack (pulumi-alpha-dev):
    error: Running program '/Users/brian/git/github/pulumi-alpha' failed with an unhandled exception:
    /Users/brian/git/github/pulumi-alpha/node_modules/pulumi-beta/index.ts:1
    import * as pulumi from "@pulumi/pulumi";
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

有谁能帮助指导我如何让这个基本示例工作吗?

xfb7svmp

xfb7svmp1#

我的问题基本上是这个问题的翻版:这个答案中有一些很好的建议,我使用的一个选项是将bin目录提交到GitHub。

相关问题