0
0

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.

json-serverのserver.js起動でエラーになった件

Posted at

json-serverでserver.jsを起動しようとしたらエラーになったので、解決策をメモ。


以下コマンドでserver.jsを起動しようとした所・・・

node server.js

以下のエラー。

node:internal/modules/cjs/loader:1042
  throw err;
  ^

Error: Cannot find module 'json-server'

調べてみたら同じエラーが出たという以下サイトが見つかった。

で、色々調べてたら、コレがヒントに。
https://github.com/typicode/json-server/issues/454
要はnpmで取ってきたjson-serverのパスがNODE_PATHに当たっていなかったってことか。
ってことで、改めて環境変数を取ることに。
C:\work\Dummy_Rest> npm root -g
C:\Users\wattak\AppData\Roaming\npm\node_modules
C:\work\Dummy_Rest> set NODE_PATH=C:\Users\wattak\AppData\Roaming\npm\node_modules
その後 node コマンドによる global.module.paths で上記設定されたパスが入っていることを確認すると。。。
C:\work\Dummy_Rest> node server.js
Dummy REST API is running.
と、無事起動しましたとさ。

しかしその通りやったものの私の環境(Mac)ではうまくパスが入らず。
さらに調べて以下サイトを発見。

以下のやり方でいきました(Macだから最初のサイトのやり方でうまくいかなかったのかな?)

※ Macの場合は、ダブルクォーテーションだとNODE_PATH = npm root -gとなるのでで以下のようにすること。

// .bash_profileに設定
NODE_PATH=`npm root -g`
export NODE_PATH

以上でserver.jsが起動できました!

% node server.js
JSON Server is running
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?