LoginSignup
1
1

More than 1 year has passed since last update.

nodejs で import がシンタックスエラーを吐く

Posted at

あるnodejsのコードを実行しようとしたら以下のようなエラーを吐かれた

import pkg from '<libname>';
^^^^^^

SyntaxError: Cannot use import statement outside a module

https://bobbyhadz.com/blog/javascript-syntaxerror-cannot-use-import-statement-outside-module#:~:text=The%20%22SyntaxError%3A%20Cannot%20use%20import,json%20for%20the%20Node%20apps.

によると、どうもJavascriptのバージョンES6からの機能を使ったコードが含まれている場合にこのようなことが起きるらしい。
Nodejsの場合にはpackage.jsonの中に以下のような記述を追加すればよいらしい。

{
  "type": "module",
}
1
1
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1