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

Posted

技术标签:

【中文标题】打包基础设施时的Pulumi问题:无法在模块外使用import语句【英文标题】:Pulumi issue when packaging infrastructure: Cannot use import statement outside a module 【发布时间】:2022-01-06 16:58:02 【问题描述】:

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

index.ts
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);
    

tsconfig.json

    "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"
    ]

package.json

  "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 的不同存储库,我正在使用:

安装 npm 包:npm install @obutterbach/bucket

index.ts

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)

再次感谢您的帮助!

【问题讨论】:

我猜这不是 Pulumi 特有的...尝试按照***.com/questions/58384179/… 中的建议添加"type": "module" 感谢@MikhailShilkov 的建议和快速评论,如果你检查package.json,我确实试过这个,你会看到type: modulemodule: commonJS 等。从线程来看,它似乎也该方法不再有效。我正在使用节点14.18.0,我开始怀疑是否应该降级节点以克服这些问题 【参考方案1】:

您的@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

【讨论】:

感谢@Sebastian 的解释,这确实是我面临的问题:) 我应用了修复程序,它立即起作用了!

以上是关于打包基础设施时的Pulumi问题:无法在模块外使用import语句的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Pulumi 在存储帐户上启用 Azure Defender?

Pulumi kubernetes ConfigGroup 需要字符串,我只有输出

使用 Pulumi 通过 C# 在 APIM 中创建后端资源时输入映射错误

maven打包时的三方包的选择顺序

如何使用 Pulumi 向 Web App 添加身份验证

Pulumi 不执行 Kubernetes Pod 的优雅关闭