4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JavaScriptでimport文が使えなかった話

Posted at

はじめに

JavaScript学習として、簡単なToDoリストのサイトを作成中です。
その際にcssファイルがimportできずエラーになったので、解決方法をまとめます。

問題

VSCodeでファイルを作成後、ブラウザ上に表示させようとしたが、cssをimportしている箇所で "Cannot use import statement outside a modul" というエラーが出ました。

解決方法

import文はサーバーを立てないと使えない ようです。
サーバーを立てない場合は、htmlにcssを読み込ませる必要があります。

記載例

index.html
 <link rel="stylesheet" href="src/styles.css" />

こちらの方法が成功した場合、import文は不要になります。

おわりに

ファイル読み込み=import みたいなイメージがあったので、このような決まりごとがあるとは知りませんでした。困っている方のお役に立てたら幸いです。

参考

4
3
0

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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?