Cannot use import statement outside a module の対処方法が知りたい

実現したいこと

エラー原因と解決方法が知りたいmisskey.jsを使ったnode.jsで実行できるプロジェクトを作りたい

発生している問題・分からないこと

以下のソースファイルでnpx tscを実行すると以下のエラーが出ます エラコード1

エラーコード

dev/test/node_modules/misskey-js/built/index.js:1 import Stream, { Connection } from './streaming.js'; ^^^^^^ SyntaxError: Cannot use import statement outside a module at internalCompileFunction (node:internal/vm:77:18) at wrapSafe (node:internal/modules/cjs/loader:1287:20) at Module._compile (node:internal/modules/cjs/loader:1339:27) at Module._extensions..js (node:internal/modules/cjs/loader:1434:10) at Module.load (node:internal/modules/cjs/loader:1206:32) at Module._load (node:internal/modules/cjs/loader:1022:12) at Module.require (node:internal/modules/cjs/loader:1234:19) at require (node:internal/modules/helpers:176:18) at Object.<anonymous> (/home/dev/test/dist/Main.js:35:30) at Module._compile (node:internal/modules/cjs/loader:1375:14) Node.js v21.5.0
エラコード2
file:dev/test/dist/Main.js:12 const Misskey = require("misskey-js"); ^ ReferenceError: require is not defined in ES module scope, you can use import instead This file is being treated as an ES module because it has a '.js' file extension and '/home/dev/test/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension. at file:///home/dev/test/dist/Main.js:12:17 at ModuleJob.run (node:internal/modules/esm/module_job:218:25) at async ModuleLoader.import (node:internal/modules/esm/loader:329:24) at async loadESM (node:internal/process/esm_loader:28:7) at async handleMainPromise (node:internal/modules/run_main:120:12) Node.js v21.5.0
エラーコード3
 node dist/Main.js file:///home/dev/test/dist/Main.js:14 Object.defineProperty(exports, "__esModule", { value: true }); ^ ReferenceError: exports is not defined in ES module scope This file is being treated as an ES module because it has a '.js' file extension and '/home/ dev/test/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension. at file:///home/dev/test/dist/Main.js:14:23 at ModuleJob.run (node:internal/modules/esm/module_job:218:25) at async ModuleLoader.import (node:internal/modules/esm/loader:329:24) at async loadESM (node:internal/process/esm_loader:28:7) at async handleMainPromise (node:internal/modules/run_main:120:12) Node.js v21.5.0
### 該当のソースコード ```Typescript ```ts import * as Misskey from 'misskey-js'; async function Main() { const cli = new Misskey.api.APIClient({ origin: 'https://misskey.test', credential: 'TOKEN', }); const meta = await cli.request('meta', { detail: true }); } Main();
### 試したこと・調べたこと - [x] teratailやGoogle等で検索した - [x] ソースコードを自分なりに変更した - [ ] 知人に聞いた - [ ] その他 ##### 上記の詳細・結果 1,`const Misskey = require("misskey-js");`のように変えましたが同じエラーが出ます。エラコード1 2,package.jsonに`"type": "module"`をを追加しましたがエラーコード2が発生します 3, `import Misskey from "misskey-js";`にしましたがエラーコード3が発生します 4,その他チャットGDTを参考に色々変更を加えていみました、エラーコードが変化するだけで、 根本的な解決方法わかりません。 ### 補足 node: Node.js v21.5.0 OS: linux mint npm: 10.2.4 利用ライブラリ https://github.com/misskey-dev/misskey.js

コメントを投稿

0 コメント