无法在带有 @pusher/push-notifications-web nodejs 的模块外部使用 import 语句 - beams

发布于 2025-01-15 01:10:40 字数 609 浏览 2 评论 0原文

我正在尝试使用nodejs和express来遵循本教程: https://pusher .com/docs/beams/reference/web/#npm-yarn

首先,我做了:在添加代码之前 npm install @pusher/push-notifications-web 。

但是当我在 index.js 文件中添加此代码时:

import * as PusherPushNotifications from "@pusher/push-notifications-web";

const beamsClient = new PusherPushNotifications.Client({
  instanceId: "<YOUR_INSTANCE_ID_HERE>",
});

beamsClient.start().then(() => {
  // Build something beatiful 
              

I am trying to follow this tutorial using nodejs and express: https://pusher.com/docs/beams/reference/web/#npm-yarn

First I did: npm install @pusher/push-notifications-web before adding the code.

But when I add this code in the index.js file:

import * as PusherPushNotifications from "@pusher/push-notifications-web";

const beamsClient = new PusherPushNotifications.Client({
  instanceId: "<YOUR_INSTANCE_ID_HERE>",
});

beamsClient.start().then(() => {
  // Build something beatiful ????
});

I get this error:
SyntaxError: Cannot use import statement outside a module

It's also not very clear to me from the tutorial if the code has to be in the frontend or the backend. I tried both but got the same result.

How can I fix this problem?

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

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

发布评论

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

评论(2

埋情葬爱 2025-01-22 01:10:40

该错误是由于您尝试在常规 CommonJS 文件中使用 ES 模块特定功能(Node.js 中的默认行为)而引起的。但是,您看到的是 Pusher 的 Web SDK,它不会帮助您实现目标。

您需要 Node.js 的服务器 SDK - https://pusher.com /docs/beams/reference/server-sdk-node/

The error is caused by the fact that you're trying to use ES module specific features in a regular CommonJS file (the default behavior in Node.js). However, what you're looking at is the Web SDK for Pusher which won't help you achieve your goals.

You need the server SDK for Node.js - https://pusher.com/docs/beams/reference/server-sdk-node/.

淡水深流 2025-01-22 01:10:40

验证您是否安装了最新版本的 Node.js,并且有两种方法可以修复

  1. package.json 中将“type”字段设置为“module”的值。这将确保所有 .js 和 .mjs 文件都被解释为 ES 模块。
// package.json
{
  "type": "module"
}
  1. 使用 .mjs 作为文件扩展名,而不是 .js

Verify that you have the latest version of Node.js installed and you have 2 ways of fixing that

  1. Set "type" field with a value of "module" in package.json. This will ensure that all .js and .mjs files are interpreted as ES modules.
// package.json
{
  "type": "module"
}
  1. Use .mjs as file extension instead of .js.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文