npm + Typescript:无法在模块外部使用 import 语句

发布于 2025-01-11 05:01:49 字数 2227 浏览 0 评论 0原文

我写了一个包并将其发布到 npm。该包以 开头

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

export interface ...
export class controlplaneDependencies extends pulumi.ComponentResource {
...

发布和安装工作正常。当我尝试从不同的 index.ts 运行代码时,它失败并显示错误

import * as aws from "@pulumi/aws";
    ^^^^^^
    
    SyntaxError: Cannot use import statement outside a module
        at compileFunction (<anonymous>)
        at Object.compileFunction (node:vm:353:18)
        at wrapSafe (node:internal/modules/cjs/loader:1039:15)
        at Module._compile (node:internal/modules/cjs/loader:1073:27)
        at Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
        at Object.require.extensions.<computed> [as .ts] (/Users/cinto/public-cloud-operator/tests/aws/node_modules/ts-node/src/index.ts:431:14)
        at Module.load (node:internal/modules/cjs/loader:989:32)
        at Function.Module._load (node:internal/modules/cjs/loader:829:14)
        at Module.require (node:internal/modules/cjs/loader:1013:19)
        at require (node:internal/modules/cjs/helpers:93:18)

“这就是我调用包的方式”,

import * as controlplane from "@bb/controlplane";

stackOutput=new controlplane.controlplaneDependencies("Install dependencies", {
       Provider: awsProvider,

我不确定如何解决此问题。我尝试添加 "type": "module", 但仍然失败。

我也尝试过

const aws = require( "@hybrid-cloud/aws" )

,但是无法在界面内访问 aws

任何指示都有帮助。这是 tsconfig.json


{
  "compilerOptions": {
    "strict": true,
    "strictPropertyInitialization": false,
    "target": "ES6",
    "module": "CommonJS",
    "moduleResolution": "Node",
    "sourceMap": true,
    "declaration": true,
    "experimentalDecorators": true,
    "pretty": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "noImplicitAny": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true,
    "lib": [
      "es2019",
      "es5",
      "es6",
      "dom",
      "es2015.collection"
    ],
    "outDir": "./bin"
  },
  "exclude": [
    "node_modules"
  ]
}

I wrote a package and published it to npm. The package starts with

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

export interface ...
export class controlplaneDependencies extends pulumi.ComponentResource {
...

The publishing and installation works fine. When I try to run the code from a different index.ts, it fails with the error

import * as aws from "@pulumi/aws";
    ^^^^^^
    
    SyntaxError: Cannot use import statement outside a module
        at compileFunction (<anonymous>)
        at Object.compileFunction (node:vm:353:18)
        at wrapSafe (node:internal/modules/cjs/loader:1039:15)
        at Module._compile (node:internal/modules/cjs/loader:1073:27)
        at Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
        at Object.require.extensions.<computed> [as .ts] (/Users/cinto/public-cloud-operator/tests/aws/node_modules/ts-node/src/index.ts:431:14)
        at Module.load (node:internal/modules/cjs/loader:989:32)
        at Function.Module._load (node:internal/modules/cjs/loader:829:14)
        at Module.require (node:internal/modules/cjs/loader:1013:19)
        at require (node:internal/modules/cjs/helpers:93:18)

This is how I am calling the package

import * as controlplane from "@bb/controlplane";

stackOutput=new controlplane.controlplaneDependencies("Install dependencies", {
       Provider: awsProvider,

I am not sure how to fix this. I tried adding "type": "module", but is still fails.

I also tried

const aws = require( "@hybrid-cloud/aws" )

but then aws cannot be accessed inside the interface.

Any pointers are helpful. Here is the tsconfig.json


{
  "compilerOptions": {
    "strict": true,
    "strictPropertyInitialization": false,
    "target": "ES6",
    "module": "CommonJS",
    "moduleResolution": "Node",
    "sourceMap": true,
    "declaration": true,
    "experimentalDecorators": true,
    "pretty": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "noImplicitAny": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true,
    "lib": [
      "es2019",
      "es5",
      "es6",
      "dom",
      "es2015.collection"
    ],
    "outDir": "./bin"
  },
  "exclude": [
    "node_modules"
  ]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荭秂 2025-01-18 05:01:49

我将其添加到 package.json 中,现在它可以工作了,

"scripts": {
    "build": "tsc",
    "build:typecheck": "tsc --noEmit",
    "lint:check": "eslint src/**/*.ts",
    "lint:fix": "eslint --fix src/**/*.ts"
  },
  "main": "bin/src/index.js"

我也将 es6 保留在我的库中。感谢大家的帮助

I added this to package.json and it is working now

"scripts": {
    "build": "tsc",
    "build:typecheck": "tsc --noEmit",
    "lint:check": "eslint src/**/*.ts",
    "lint:fix": "eslint --fix src/**/*.ts"
  },
  "main": "bin/src/index.js"

I also just kept es6 in my lib. Thanks everyone for the help

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文