GithubHelp home page GithubHelp logo

Comments (14)

YunYouJun avatar YunYouJun commented on June 15, 2024

import Mirai from "mirai-ts" 可以啊,我自己正用着呢。

版本是?当前最新版本是 v0.4.5

from mirai-ts.

YunYouJun avatar YunYouJun commented on June 15, 2024

2ce9e92 这里的修改是为了适配 nodejs,以便使用 require("mirai-ts") 而非 require("mirai-ts").default

应该不会对 import Mirai from 'mirai-ts' 的语法产生影响。

from mirai-ts.

CL-Jeremy avatar CL-Jeremy commented on June 15, 2024

肯定是最新版本的,但是我这里报错

            this.bot = new mirai_ts_1.default({
                       ^

TypeError: mirai_ts_1.default is not a constructor

from mirai-ts.

YunYouJun avatar YunYouJun commented on June 15, 2024

能否提供一下原项目此处的代码链接?

from mirai-ts.

CL-Jeremy avatar CL-Jeremy commented on June 15, 2024

2ce9e92 这里的修改是为了适配 nodejs,以便使用 require("mirai-ts") 而非 require("mirai-ts").default

应该不会对 import Mirai from 'mirai-ts' 的语法产生影响。

我知道,但是我不是很明白:

-   module.exports = Mirai;
-   module.exports.default = Mirai;
-   module.exports.Mirai = Mirai;
+   exports = module.exports = Mirai;

这两个等价吗?

from mirai-ts.

CL-Jeremy avatar CL-Jeremy commented on June 15, 2024

能否提供一下原项目此处的代码链接?

https://github.com/CL-Jeremy/mirai-twitter-bot/blob/master/src/mirai.ts

from mirai-ts.

YunYouJun avatar YunYouJun commented on June 15, 2024

这两个等价吗?

exports 是对 module.exports 的引用,只在原生的 nodejs 中我们需要用到 module.exports。因为 require() 返回的是 module.exports,而 TypeScript 可以使用 ES6+ 的 import 语法,直接调用 exports 即可。

本质上是:

module.exports = Mirai
exports = module.exports

https://github.com/CL-Jeremy/mirai-twitter-bot/blob/master/src/mirai.ts

我尝试 clone 了您的项目,构建时并没有发现报错提示。重新安装下 node_modules ?

from mirai-ts.

YunYouJun avatar YunYouJun commented on June 15, 2024

也可能与 node 版本有关,我当前使用的是 nodejs LTS 版本(12.18.3)。

from mirai-ts.

CL-Jeremy avatar CL-Jeremy commented on June 15, 2024

这种诸如 import * as A from 'a';import A from 'a'; 的差异在构建时不会体现,可以尝试执行一下 node dist/main.js config.json.example

from mirai-ts.

CL-Jeremy avatar CL-Jeremy commented on June 15, 2024

也可能与 node 版本有关,我当前使用的是 nodejs LTS 版本(12.18.3)。

服务器上同为 12.18.3(本机上是 14.x)

from mirai-ts.

YunYouJun avatar YunYouJun commented on June 15, 2024

5d93693 已同时导出 default 以修复该问题。
已 release v0.4.6。我测试已可以正常运行。
如果没问题的话,可以关闭该 ISSUE。

from mirai-ts.

CL-Jeremy avatar CL-Jeremy commented on June 15, 2024

是不是因为我 tsconfig.json 的设置呢?因为如上面我报错的信息所示,我生成的 JS 在创建时肯定还是在调用 require ('mirai-ts').default,是否这种情况下必须定义 module.exports.default 呢?

from mirai-ts.

CL-Jeremy avatar CL-Jeremy commented on June 15, 2024

好的,多谢!

from mirai-ts.

YunYouJun avatar YunYouJun commented on June 15, 2024

是不是因为我 tsconfig.json 的设置呢?因为如上面我报错的信息所示,我生成的 JS 在创建时肯定还是在调用 require ('mirai-ts').default,是否这种情况下必须定义 module.exports.default 呢?

发现 TypeScript 构建后总是使用 .default,而我之所以可以正常使用,是因为我开发的 el-bot 更接近于一个库,导出时会自动加上 __importDefault() 的函数,来适配是否使用 default。

而本质上,你也可以考虑使用导出一个主入口文件的方式来使用,而非直接执行编译后的部分 js 文件。当然这也并非必要的。

主动适配 module.exports.default 显然是更好的方式。

from mirai-ts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.