打包基础设施时的 Pulumi 问题:无法在模块外使用导入语句

Pulumi issue when packaging infrastructure: Cannot use import statement outside a module

我正在测试一个简单的 Pulumi 概念:打包基础设施。我为此使用了一个非常简单的示例,我正在尝试通过导入 npm 包来部署 S3 存储桶。 这是我的 npm bucket npm 包:

import * as aws from "@pulumi/aws";
import { ComponentResource, ComponentResourceOptions, Input, Output } from "@pulumi/pulumi";

export interface S3Args {
    description: string;
    baseTags: aws.Tags;
}

export class S3 extends ComponentResource {
    s3: aws.s3.Bucket;

    private readonly name: string;
    private readonly description: string;
    private readonly baseTags: { [k: string]: Input<string> };

    constructor(name: string, args: S3Args, opts?: ComponentResourceOptions) {
        super("oli:s3-test", name, {}, opts);

        // Make base info available to other methods.
        this.name = name;
        this.description = args.description;
        this.baseTags = args.baseTags;

        // VPC
        this.s3 = new aws.s3.Bucket(`${name}-test`, {
            tags: this.resourceTags({
                Name: `${args.description} test`,
            }),
        }, {parent: this});
    }
    private resourceTags(additionalTags: { [k: string]: Input<string> }) {
        return Object.assign(additionalTags, this.baseTags);
    }
}
{
    "compilerOptions": {
        "strict": true,
        "outDir": "bin",
        "target": "es2016",
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "skipLibCheck": true,
        "experimentalDecorators": true,
        "pretty": true,
        "noFallthroughCasesInSwitch": true,
        "noImplicitReturns": true,
        "forceConsistentCasingInFileNames": true
    },
    "files": [
        "index.ts"
    ]
}
{
  "name": "@obutterbach/bucket",
  "version": "1.0.0",
  "description": "Pulumi package for S3",
  "main": "index.ts",
  "type": "module",
  "scripts": {
    "build": "tsc",
    "lint": "tslint --project ."
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/obutterbach/pulumi-infra-package.git"
  },
  "publishConfig": {
    "name": "@obutterbach/bucket",
    "registry": "https://npm.pkg.github.com"
  },
  "devDependencies": {
    "@types/node": "14.6.2",
    "ts-node": "^8.0.2",
    "typescript": "^2.9.1",
    "tslint": "^5.10.0"
  },
  "dependencies": {
    "@pulumi/aws": "^4.0.0",
    "@pulumi/pulumi": "^3.0.0"
  },
  "author": "Oli",
  "license": "ISC"
}

到目前为止一切顺利,我可以执行以下命令: npm inpm run buildnpm run lintnpm publish

现在我有一个用于部署 Pulumi 的不同存储库,我正在使用:

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

import  { S3 } from "@obutterbach/bucket";

const website  = new S3 ("testing", {
    description: "blabla",
    baseTags: {"bla": "bla"}
});

使用 Pulumi preview 不断向我发送此错误,我找不到背后的原因:

$ pulumi preview
Please choose a stack, or create a new one: deploy-dev
Previewing update (deploy-dev):
     Type                 Name                      Plan       Info
 +   pulumi:pulumi:Stack  pulumi-deploy-deploy-dev  create     1 error

Diagnostics:
  pulumi:pulumi:Stack (pulumi-deploy-deploy-dev):
    error: Running program '/Users/oli/Documents/pulumi-deploy' failed with an unhandled exception:
    /Users/oli/Documents/pulumi-deploy/node_modules/@obutterbach/bucket/index.ts:1
    import * as aws from "@pulumi/aws";
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module
        at wrapSafe (internal/modules/cjs/loader.js:1001:16)
        at Module._compile (internal/modules/cjs/loader.js:1049:27)
        at Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
        at Object.require.extensions.<computed> [as .ts] (/Users/oli/Documents/pulumi-deploy/node_modules/ts-node/src/index.ts:431:14)
        at Module.load (internal/modules/cjs/loader.js:950:32)
        at Function.Module._load (internal/modules/cjs/loader.js:790:12)
        at Module.require (internal/modules/cjs/loader.js:974:19)
        at require (internal/modules/cjs/helpers.js:93:18)
        at Object.<anonymous> (/Users/oli/Documents/pulumi-deploy/index.ts:4:1)
        at Module._compile (internal/modules/cjs/loader.js:1085:14)

再次感谢您的帮助!

看起来您的 @obutterbach/bucket 包在导入时 运行 导入了错误的文件 index.ts。 Node.js 无法 运行 Typescript 文件。

我认为您应该将 package.json 中的 main 更改为 @obutterbach/bucket 包中的 index.js

{
  "name": "@obutterbach/bucket",
  "version": "1.0.0",
  "description": "Pulumi package for S3",
  "main": "./bin/index.js",
  "types": "./bin/index.d.ts",
   ...
}

来源: https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html