Unexpected token '*' error occurs when trying to use the "import" keyword on a module

huangapple go评论75阅读模式
英文:

Unexpected token '*' error occurs when trying to use the "import" keyword on a module

问题

我在尝试制作OBS扩展时遇到了导入模块的问题。我正在使用ES6,并采用Kruiz Control的方法,让OBS加载一个连接到本地WebSocket的网页,目前我只要求它显示切换到的场景。

我验证了我已经安装了obs-websocket-js,然后在网上搜索了有关导入的细节,但是建议的更改 - 将*更换为OBSWebSocket,尝试使用import 'obs-websocket-js',在脚本标签中添加/删除type="module",以及使用require而不是import - 都产生了类似的结果。

import * as OBSWebSocket from 'obs-websocket-js' // 问题所在

const obs = new OBSWebSocket();
await obs.connect(IP_REMOVED, PASSWORD_REMOVED)
obs.on('CurrentProgramSceneChanged', scene => {
    console.log(scene)
})

如果有任何替代方法或修复方法,将不胜感激。

已编辑以包括脚本加载方式。

英文:

I was trying to make an OBS extension when I ran into a problem with importing the module in the first place. I'm working in ES6, and using the Kruiz Control approach of making OBS load a webpage that connects to a local Websocket, and currently all I'm asking it to do is display the scene it switched to.

I verified that I had obs-websocket-js installed and then searched online about the nuances of import, however the suggested changes- switching out the * for OBSWebSocket, trying to use import 'obs-websocket-js', adding/removing type="module" from the script tag, and using require instead of import- yielded similar results.

import * as OBSWebSocket from 'obs-websocket-js'	// The problem line

const obs = new OBSWebSocket();
await obs.connect(IP_REMOVED, PASSWORD_REMOVED)
obs.on('CurrentProgramSceneChanged', scene => {
	console.log(scene)
})

If there's any alternative, or a fix to this, that would be greatly appreciated.

Edited to include the way the script is loaded.

答案1

得分: 0

我只使用了Node。这是最终的代码片段。

var { default: OBSWebSocket } = require('obs-websocket-js');

const obs = new OBSWebSocket();
obs.connect('ws://192.168.1.143:4455', "4gPDLLGJhuUoCvOH")
obs.on('CurrentProgramSceneChanged', scene => {
	console.log(scene.sceneName)
})
英文:

I resorted to just using Node. This is the finalized code snippet.

    var { default: OBSWebSocket } = require('obs-websocket-js');

const obs = new OBSWebSocket();
obs.connect('ws://192.168.1.143:4455', "4gPDLLGJhuUoCvOH")
obs.on('CurrentProgramSceneChanged', scene => {
	console.log(scene.sceneName)
})

答案2

得分: -2

尝试在包名称后面加上分号,就像这样:
import * as OBSWebSocket from 'obs-websocket-js';

英文:

Try put semicolon after the package name, like this:
import * as OBSWebSocket from 'obs-websocket-js';

huangapple
  • 本文由 发表于 2023年6月15日 14:50:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定