簡體   English   中英

TypeScript 中可重用 pulumi 組件的問題(不能在模塊外使用 import 語句)

[英]Issue with reusable pulumi component in TypeScript (Cannot use import statement outside a module)

我在開始使用可重用組件時遇到了問題。 我正在嘗試使用 TypeScript 編寫一個簡單的可重用組件資源,我可以使用它在 AWS 上部署 S3 存儲桶。

為了演示我遇到的問題,我創建了兩個公共 GitHub 存儲庫:

我用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這個包:

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

誰能幫助指導我如何使這個基本示例起作用?

我的問題基本上是這個問題的重復: How to have npm install a typescript dependency from a GitHub url? . 該答案中有一些很好的建議。 我使用的一個選項是將bin目錄提交到 GitHub。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM