3

I am creating a minecraft bot using mineflayer library. After a bit of work I decided to make code readable and reusable (image of file organisation) and also start using typescript. I have read a lot of stack threads and other articles as this problem is quite popular. However, after trying all of it the problem still persists.

Edit, important change:

I have tried compiling it with tsc bot.ts --resolveJsonModule and then node bot.js. It turns out it works just fine, so now the problem narrows down to configuring WebStorm running options.

Here is what I have already done

  • package.json "type": "module -> TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for path
  • tsconfig.json has "esModuleInterop": true
  • use const util = require('./utils/util') and use util.function() -> same error as in 1st step

Running whole code

As I am using WebStorm, this is what I have in running options: image (just to clarify that I don't run code from terminal)

Recreating problem in simplified environment

bot.ts

import {util} from "./utils/util" // error is thrown right away, so other lines are irrelevant I guess

const mineflayer = require('mineflayer')
const bot = mineflayer.createBot()

util.ts

import * as config from "../config/config_main.json"
export module util {
    export function sleep (time) {
        return new Promise((resolve) => setTimeout(resolve, time));
    }

    export function random(list) {
        if (list[0] === list[1]) return list[0];
        return Math.floor(Math.random() * (list[1] - list[0]))  + list[0];
    }
}

config_main.json

{
  "bot": {
    "username": "username",
    "password": "password"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es2016",
    "module": "commonjs",
    "resolveJsonModule": true,
    "esModuleInterop": true,                             
    "forceConsistentCasingInFileNames": true,
    "strict": false,                                      
    "skipLibCheck": true                           
  }
}

package.json

{
  "type": "module",
  "name": "mcbot",
  "main": "bot.js",
  "scripts": {
    "test": "None"
  }
}

Related threads

import { parse } from 'node-html-parser';
parse = require('node-html-parser');

but the IDE gives me TS2632: Cannot assign to 'util' because it is an import. error.

2 Answers 2

3

Fixed by changing:

import {util} from "./utils/util"

To:

const util = require('./utils/util')
// or
const { sleep, random, eatAny, clickItem, lookAtEntity} = require('./utils/util')

Afterall I don't know why compiling with tsc had been working well while webstorm was throwing out error. I just changed the import ES6 syntax to require() CommonJS.

Sign up to request clarification or add additional context in comments.

Comments

2

First, remove type:module from package.json. Then remove module:commonjs from tsconfig.json. Use export default function util () {} syntax. Named exports will also work for local files if you've done the first two steps.

Cause

You are mixing es6 and commonjs. module:commonjs is forcing it to be commonjs whereas esModuleInterop is forcing it to be es6. And type:moduleshows error for es6 and forces to write file extension, remove it first

Warning

Named imports will not work for npm package. Like you can't use

import { something } from "some-package";

Instead, import the default one and then access the named one.

import defaultExport from "some-package";

const something = defaultExport.something

6 Comments

Okay so I did what you told me to do: | change to module util{} ... export default util and export default function something() {} | using import util from "./utils/util" | "type": "module" was already deleted because it wasn't warning //package.json | deleted "module": "commonjs" //tsconfig.ts now pops up: Error:(5, 5) TS5070: Option '--resolveJsonModule' cannot be specified without 'node' module resolution strategy. -> So I tested with "moduleResolution": "node" and similar to 1st error comes out: SyntaxError: Cannot use import statement outside a module
I.E. I did exactly what you told me to do and the SyntaxError still persists. If it would be helpful, here's my whole mcbot directory: bot.ts | util.ts | attack.ts | movement.ts | config_main.json | tsconfig.json | package.json Every pastebin file is protected by password stackoverflow
Important note: when compiling in cmd terminal with tsc bot.ts --resolveJsonModule in mcbot directory and then running it with node bot.js, everything works just fine.
@jancor When you are using tsc bot.ts this command ignores tsconfig file. You should rather run only tsc. Here I've uploaded the zipped file. Unzip it, go to unzipped directory do a npm i and then run tsc
Thank you @KasRoudra for your help. I've unzipped the dir you sent, ran npm i & tsc and after that, node bot.js to test if it works, and well, it does. However, this issue jumps back to WebStorm, because although I can run it with those commands, I can't run it from WebStorm, which is essential for me as I find it very convenient. I opened that directory as a project in WS, created configuration file for running: added config for node.js -> assigned bot.ts to JavaScript file -> added before-launch param Compile TypeScript and used the tsconfig.json file for it -> saved and ran:
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.