admin管理员组文章数量:1289877
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 piling 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 useutil.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
{
"pilerOptions": {
"target": "es2016",
"module": "monjs",
"resolveJsonModule": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": false,
"skipLibCheck": true
}
}
package.json
{
"type": "module",
"name": "mcbot",
"main": "bot.js",
"scripts": {
"test": "None"
}
}
Related threads
- SyntaxError: Cannot use import statement outside a module -
"type": "module"
doesn't work as well as changing extensions to.mjs
isn't viable as I am using typescript - "Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6 - from here tried
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.
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 piling 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 useutil.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
{
"pilerOptions": {
"target": "es2016",
"module": "monjs",
"resolveJsonModule": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": false,
"skipLibCheck": true
}
}
package.json
{
"type": "module",
"name": "mcbot",
"main": "bot.js",
"scripts": {
"test": "None"
}
}
Related threads
- SyntaxError: Cannot use import statement outside a module -
"type": "module"
doesn't work as well as changing extensions to.mjs
isn't viable as I am using typescript - "Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6 - from here tried
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
Reset to default 2First, remove type:module
from package.json. Then remove module:monjs
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 monjs. module:monjs
is forcing it to be monjs whereas esModuleInterop
is forcing it to be es6. And type:module
shows 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
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 piling with tsc
had been working well while webstorm was throwing out error. I just changed the import
ES6 syntax to require()
CommonJS.
本文标签:
版权声明:本文标题:javascript - Typescript SyntaxError: Cannot use import statement outside a module (side file containing functions) - Stack Overf 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741465278a2380277.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论