首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >npm +类型记录:无法在模块外使用导入语句

npm +类型记录:无法在模块外使用导入语句
EN

Stack Overflow用户
提问于 2022-03-02 18:51:56
回答 1查看 352关注 0票数 0

我写了一个包裹,并把它发布给了国家预防机制。这个包是从

代码语言:javascript
复制
import * as aws from "@pulumi/aws";
import * as pulumi from "@pulumi/pulumi";

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

出版和安装工作正常。当我尝试从不同的index.ts运行代码时,它会失败。

代码语言:javascript
复制
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)

我就是这样叫这个包裹的

代码语言:javascript
复制
import * as controlplane from "@bb/controlplane";

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

我不知道怎么解决这个问题。我尝试过添加"type": "module",,但仍然失败。

我也试过

代码语言:javascript
复制
const aws = require( "@hybrid-cloud/aws" )

但是,不能在接口中访问aws

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

代码语言:javascript
复制
{
  "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"
  ]
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-04 05:23:47

我将它添加到package.json中,现在它正在工作。

代码语言:javascript
复制
"scripts": {
    "build": "tsc",
    "build:typecheck": "tsc --noEmit",
    "lint:check": "eslint src/**/*.ts",
    "lint:fix": "eslint --fix src/**/*.ts"
  },
  "main": "bin/src/index.js"

我还将es6保存在我的库中。谢谢大家的帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71327794

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档