繁体   English   中英

获取 Node 项目上的 `tsc` 导入依赖

[英]Get `tsc` import dependencies on Node project

我正在使用 Node 和 Typescript 创建一个小型不和谐机器人。

当前的代码非常简单:

import * as Discord from "discord.js";
const config = require("./config.json");

const client = new Discord.Client();

client.on("ready", () => {
  if (client.user) {
    console.log(`Logged in as ${client.user.tag}!`);
  } else {
    console.log(`Ready without client.user`);
  }
});

client.on("message", (msg: any) => {
  if (msg.content === "ping") {
    msg.reply("Pong!");
  }
});

client.login(config.BOT_TOKEN);

bot.js ,我试图让它构建一个要上传到主机的bot.js文件。

到目前为止,它一直失败,因为结果只是:

var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
define(["require", "exports", "discord.js"], function (require, exports, Discord) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    Discord = __importStar(Discord);
    var config = require("./config.json");
    var client = new Discord.Client();
    client.on("ready", function () {
        if (client.user) {
            console.log("Logged in as " + client.user.tag + "!");
        }
        else {
            console.log("Ready without client.user");
        }
    });
    client.on("message", function (msg) {
        if (msg.content === "ping") {
            msg.reply("Pong!");
        }
    });
    client.login(config.BOT_TOKEN);
});
//# sourceMappingURL=index.js.map

我一直在调整设置,试图让它按预期构建,但到目前为止没有结果。

我的tsconfig.json如下:

{
    "compilerOptions": {
      /* Basic Options */
      "target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */,
      "module": "amd" /* Specify module code generation: 'none', commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
      // "lib": [],                             /* Specify library files to be included in the compilation:  */
      "allowJs": true /* Allow javascript files to be compiled. */,
      // "checkJs": true,                       /* Report errors in .js files. */
      // "jsx": "preserve",                     /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
      // "declaration": true,                   /* Generates corresponding '.d.ts' file. */
      "sourceMap": true /* Generates corresponding '.map' file. */,
      // "outFile": "./dist/index.js" /* Concatenate and emit output to single file. */,
      "outDir": "./dist" /* Redirect output structure to the directory. */,
      // "rootDir": "./",                       /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
      // "resolveJsonModule": true /* Do not emit comments to output. */,
      "esModuleInterop": true /* Do not emit comments to output. */,
      "removeComments": true /* Do not emit comments to output. */,
      // "noEmit": true,                        /* Do not emit outputs. */
      // "importHelpers": true,                 /* Import emit helpers from 'tslib'. */
      // "downlevelIteration": true,            /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
      // "isolatedModules": true,               /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
  
      /* Strict Type-Checking Options */
      "strict": true /* Enable all strict type-checking options. */,
      "noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
      "strictNullChecks": true /* Enable strict null checks. */,
      "noImplicitThis": true /* Raise error on 'this' expressions with an implied 'any' type. */,
      // "alwaysStrict": true,                  /* Parse in strict mode and emit "use strict" for each source file. */
  
      /* Additional Checks */
      // "noUnusedLocals": true,                /* Report errors on unused locals. */
      // "noUnusedParameters": true,            /* Report errors on unused parameters. */
      // "noImplicitReturns": true,             /* Report error when not all code paths in function return a value. */
      // "noFallthroughCasesInSwitch": true,    /* Report errors for fallthrough cases in switch statement. */
  
      /* Module Resolution Options */
      "moduleResolution": "node",            /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
      // "baseUrl": "./",                       /* Base directory to resolve non-absolute module names. */
      // "paths": {},                           /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
      // "rootDirs": [],                        /* List of root folders whose combined content represents the structure of the project at runtime. */
      // "typeRoots": [],                       /* List of folders to include type definitions from. */
      // "types": [],                           /* Type declaration files to be included in compilation. */
      // "allowSyntheticDefaultImports": true,  /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
      // "preserveSymlinks": true,              /* Do not resolve the real path of symlinks. */
  
      /* Source Map Options */
      // "sourceRoot": "./",                    /* Specify the location where debugger should locate TypeScript files instead of source locations. */
      // "mapRoot": "./",                       /* Specify the location where debugger should locate map files instead of generated locations. */
      // "inlineSourceMap": true /* Emit a single file with source maps instead of having a separate file. */,
      // "inlineSources": true,                 /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
  
      /* Experimental Options */
      "experimentalDecorators": true /* Enables experimental support for ES7 decorators. */
      // "emitDecoratorMetadata": true,         /* Enables experimental support for emitting type metadata for decorators. */
    },
    "include": ["src/**/*.ts"],
    "exclude": ["node_modules"]
  }

在您的编译器选项中,您有"module": "amd" 这是错误的。

Discord 使用了一个使用普通 js 的节点 api,因此将其更改为: "module": "commonjs"

参考

https://discordjs.guide/creating-your-bot/#logging-in-to-discord

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM