简体   繁体   English

SyntaxError: 不能在模块外使用 import 语句。 我正在使用 typescript 和 discord js

[英]SyntaxError: Cannot use import statement outside a module. I'm using typescript and discord js

I'm trying to figure out why I'm getting this pesky bug: SyntaxError: Cannot use import statement outside a module I'm using discord.js and typescript to create a discord bot.我试图弄清楚为什么会出现这个讨厌的错误: SyntaxError: Cannot use import statement outside a module I'm using discord.js and typescript to create a Z482BCDDC8FE53ED1E68C77A95AAB5F83 I'm currently creating my own command handler by dynamically importing all files in a commands dir.我目前正在通过动态导入commands目录中的所有文件来创建自己的命令处理程序。 However, whenever I import such file Code at question:但是,每当我导入有问题的此类文件代码时:

Promise.all((await getCommands("private")).map(async absPath => {
            return { name : basename(absPath), mod:  ( await import(absPath)).mod as fdjsMod  }
         })).then( modArr => {
             for ( const { name, mod } of modArr) {
                 publicMap.set(name.substring(0, name.length-3), mod)
             }
             
        })
import { Result, Err, Ok } from 'ts-results';

//^^^^^^^^^^ Cannot use import statement outside a module

import * as Handler from "../../types/index"

export default {
        alias: ["p"],
        type: Handler.CommandType.TEXT,
        delegate : () => {
            return Err("pong")
        }
} as Handler.fdjsMod

my tsconfig:我的 tsconfig:

{
  "compilerOptions": {
    "resolveJsonModule": true,
    "outDir": "dist",
    "rootDir": "src"

  },
  "extends": "@tsconfig/node16/tsconfig.json"
}

{
  "name": "cheemsbanker",
  "version": "1.0.0",
  "description": "",
  "main": "dist/index.js",
  "scripts": {
    "compile": "tsc && node dist/index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "discord.js": "^13.3.1",
    "ts-results": "^3.3.0"
  },
  "devDependencies": {
    "@tsconfig/node16": "^1.0.2",
    "@types/node": "^16.11.7",
    "ts-node": "^10.4.0",
    "typescript": "^4.3.5"
  }
}
src
    │   index.ts
    │   secrets.json
    │
    ├───commands
    │   ├───private
    │   │       ping.ts
    │   │
    │   └───public
    ├───handler
    │   │   index.ts
    │   │
    │   ├───events
    │   │       event.ts
    │   │
    │   ├───fdjs
    │   │       fdjs.ts
    │   │
    │   └───utils
    │           readFile.ts
    │
    └───types
        │   index.ts
        │
        └───handler
                handler.ts

Things I have tried:我尝试过的事情:

  1. Setting type to module in my package.json在我的 package.json 中将类型设置为模块
  • this only leads to an error saying exports is not defined in ES module scope这只会导致错误exports is not defined in ES module scope
  1. Change dist folder js output to.cjs files将 dist 文件夹 js output 更改为.cjs 文件
  • same error同样的错误
  1. Switch between cjs require() and dynamic import() -same error在 cjs require() 和动态 import() 之间切换 - 相同的错误

Any and all help would be appreciated.任何和所有的帮助将不胜感激。

Try adding these attributes to your tsconfig:尝试将这些属性添加到您的 tsconfig:

{
  "compilerOptions": {
    "resolveJsonModule": true,
    "outDir": "dist",
    "rootDir": "src",
    "module": "commonjs",
    "moduleResolution": "node",
    "esModuleInterop": true,
  },
  "extends": "@tsconfig/node16/tsconfig.json"
}

I'm not sure if this works for you, but it did for me.我不确定这是否对你有用,但它对我有用。

Solved: when i was dynamically importing files from my command folder, i was directing it to my src file instead of dist:已解决:当我从命令文件夹动态导入文件时,我将其定向到我的 src 文件而不是 dist:

export default async function getCommands(type: privOrPub) : Promise<string[]> {
    //src/commands/public
    const publicCommands = join(process.cwd(), "src/commands/public")
    const privateCommands = join(process.cwd(), "src/commands/private")

    return type == 'private' ? readPath(privateCommands) : readPath(publicCommands)
}


export default async function getCommands(type: privOrPub) : Promise<string[]> {
    //src/commands/public -> dist/commands/public
    const publicCommands = join(process.cwd(), "dist/commands/public")
    const privateCommands = join(process.cwd(), "dist/commands/private")

    return type == 'private' ? readPath(privateCommands) : readPath(publicCommands)
}


Thanks for the help, everyone.谢谢大家的帮助。 I didnt expect the error to be so trivial没想到错误如此微不足道

暂无
暂无

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

相关问题 未捕获的语法错误:无法在模块外使用 import 语句。 将 typescript 文件导入 JavaScript 文件 - Uncaught SyntaxError: Cannot use import statement outside a module. import typescript file into JavaScript file 语法错误:不能在模块 discord.js 之外使用导入语句 - SyntaxError: Cannot use import statement outside a module discord.js JS 导入导出语法错误:不能在模块外使用导入语句 - JS import export SyntaxError: Cannot use import statement outside a module SyntaxError:无法在 node.js 上的模块外部使用 import 语句 - SyntaxError: Cannot use import statement outside a module on node.js 未捕获的语法错误:无法在节点 js 中的模块外使用导入语句 - Uncaught SyntaxError: Cannot use import statement outside a module in node js 纯 JS - SyntaxError:不能在模块外使用 import 语句 - Pure JS - SyntaxError: Cannot use import statement outside a module Node.js:语法错误:无法在模块外使用导入语句 - Node.js: SyntaxError: Cannot use import statement outside a module SyntaxError: 不能在模块外使用 import 语句 - SyntaxError: Cannot use import statement outside a module 为什么在使用 cloudinary 模块时出现“语法错误:无法在模块外使用导入语句”错误? - Why am I getting the 'SyntaxError: Cannot use import statement outside a module' error while using the cloudinary module? 如何修复 SyntaxError:无法在使用 Jest 的模块外部使用 import 语句? - How do I fix SyntaxError: Cannot use import statement outside a module using Jest?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM