【问题标题】:Can't import @google/pubsub and other @google npm module in Node.js 13.6无法在 Node.js 13.6 中导入 @google/pubsub 和其他 @google npm 模块
【发布时间】:2020-05-06 11:04:04
【问题描述】:

我正在开发物联网项目,我需要在我的 app.js 文件中使用 @google-cloud/pubsub npm 模块!我想使用 Node.js 13.6.0 作为带有 ECMAScript 模块的 Node.js 版本!

// TypeError: PubSub is not a constructor 
import PubSub from '@google-cloud/pubsub';

// SyntaxError: module does not provide an export name 'PubSub'
import { PubSub } from '@google-cloud/pubsub';

// ReferenceError: require is not defined 
const PubSub = require('@google-cloud/pubsub');

【问题讨论】:

    标签: node.js ecmascript-6 node-modules google-cloud-pubsub


    【解决方案1】:

    其实我用的是:

    'use strict';
    // Google Cloud PubSub  
    const {PubSub} = require('@google-cloud/pubsub');
    // Creates a client; cache this for further use
    const pubSubClient = new PubSub();
    

    【讨论】:

      【解决方案2】:

      我能够使用第一种方法导入 PubSub,使用 Node.js 13.6.0:

      // app.js
      import PubSub from '@google-cloud/pubsub';
      console.log(PubSub);
      
      // Output
      $ node app.js 
      (node:17508) ExperimentalWarning: The ESM module loader is experimental.
      {
        v1: {
          PublisherClient: [Function: PublisherClient],
          SubscriberClient: [Function: SubscriberClient]
        },
        IAM: [Function: IAM],
        PubSub: [Function: PubSub],
        Snapshot: [Function: Snapshot],
        Message: [Function: Message],
        Subscription: [Function: Subscription],
        Topic: [Function: Topic]
      }
      

      您是否安装了 Node.js Cloud Pub/Sub 客户端库?

      npm install --save @google-cloud/pubsub
      

      【讨论】:

      猜你喜欢
      • 2019-02-28
      • 2013-04-12
      • 2018-12-11
      • 2017-02-09
      • 2017-10-31
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多