[英]Using JavaScript NPM package with import
I am using the kahoot-api NPM module ( GitHub , NPM ) and it requires using JavaScript import.我正在使用 kahoot-api NPM 模块( GitHub , NPM ),它需要使用 JavaScript 导入。 (edit, this is a Node.js package. I didn't know the difference between JS and Node.js at the time of writing this, this is why this question was created).
(编辑,这是一个 Node.js 包。在写这篇文章的时候我不知道 JS 和 Node.js 之间的区别,这就是创建这个问题的原因)。
The documentation says to use the following JavaScript import script to import Session
and Adapters
:该文档说使用以下 JavaScript 导入脚本来导入
Session
和Adapters
:
import { Session, Adapters } from 'kahoot-api';
This throws an error saying that I need to use a relative reference.这会引发一个错误,指出我需要使用相对引用。
Failed to resolve module specifier "kahoot-api".
无法解析模块说明符“kahoot-api”。 Relative references must start with either "/", "./", or "../".
相对引用必须以“/”、“./”或“../”开头。
My files and folders are structured as so:我的文件和文件夹的结构如下:
- index.php
索引.php
- node_modules
节点模块
- @omegaboot
@omegaboot
- kahoot-api
kahoot-api
All of the NPM files for kahoot-api
are inside of the kahoot-api
folder. kahoot-api
所有 NPM 文件都在kahoot-api
文件夹中。
I have modified my JavaScript code to use a relative reference, as shown below.我修改了我的 JavaScript 代码以使用相对引用,如下所示。
import { Session, Adapters } from './node_modules/@omegaboot/kahoot-api/';
No errors are thrown, however the two imported statements Session
and Adapters
are not available, and still undefined.不会抛出任何错误,但是导入的两个语句
Session
和Adapters
不可用,并且仍然未定义。
import { Session, Adapters } from './node_modules/@omegaboot/kahoot-api/';
const session = new Session('000000');
session.openSocket() //Connect
.then(socket => {
const player = new Adapters.Player(socket); //Create player class
player.join('test') //Join with name
.then(() => {
console.log('Success!');
});
});
There are multiple JavaScript files inside of the kahoot-api
folder, and I have tried including each JavaScript file with the JavaScript import, but to no avail. kahoot-api
文件夹中有多个 JavaScript 文件,我尝试在 JavaScript 导入中包含每个 JavaScript 文件,但无济于事。
Also, I am using the code in index.php
with a <script>
tag with the attribute type="module"
as it is needed, or else the following error is thrown:另外,我在
index.php
使用带有<script>
标签的代码,根据需要,它的属性为type="module"
,否则会抛出以下错误:
Cannot use import statement outside a module
不能在模块外使用 import 语句
Looks like that documentation is wrong because it is based on an old package.看起来该文档是错误的,因为它基于旧包。
You should install it like:你应该像这样安装它:
yarn add @omegaboot/kahoot-api
or或者
npm i @omegaboot/kahoot-api
if you have webpack (I see that you are using this in php) then use import如果你有 webpack(我看到你在 php 中使用它)然后使用 import
import { Session, Adapters } from '@omegaboot/kahoot-api';
otherwise use require否则使用需要
const { Session, Adapters } = require("@omegaboot/kahoot-api")
This worked for me when I tried the module @omegaboot/kahoot-api
.当我尝试模块
@omegaboot/kahoot-api
时,这对我@omegaboot/kahoot-api
。
As nodejs does not support import statement and you must use require
method if you are not using any bundlers.由于 nodejs 不支持 import 语句,如果您不使用任何捆绑程序,则必须使用
require
方法。
const {
Session,
Adapters
} = require('@omegaboot/kahoot-api');
const session = new Session('000000');
session.openSocket() //Connect
.then(socket => {
const player = new Adapters.Player(socket); //Create player class
player.join('test') //Join with name
.then(() => {
console.log('Success!');
});
});
idiidk here (the author of the package) idiidk 在这里(包的作者)
The package @omegaboot/kahoot-api
is not the version that people should be using. @omegaboot/kahoot-api
包不是人们应该使用的版本。 This package uses some hard coded defaults for easier integration with our build process for omegaboot.com.这个包使用了一些硬编码的默认值,以便更容易地与我们的 omegaboot.com 构建过程集成。
The "normal" kahoot-api package https://www.npmjs.com/package/kahoot-api is to be used by third parties, although it hasn't been updated in a bit. “正常”的 kahoot-api 包https://www.npmjs.com/package/kahoot-api将被第三方使用,尽管它没有一点更新。 Please open an issue on the github page if anything is broken and I'll be sure to take a look.
如果有任何问题,请在 github 页面上打开一个问题,我一定会看看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.