6
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 3 years have passed since last update.

NodeV14でrequireが使えない場合の対処

Last updated at Posted at 2021-03-09

#現象
Node.jsでの実行(※ブラウザ実行ではない)にもかかわらず、
ファイル読み込み関数requireが使えずに下記エラーが出る。

require is not defined node

#調査結果
NodeV14以降のmoduleではrequire使えないらしい。
下記のようにインポートすればOK。

import { createRequire } from 'module';
const require = createRequire(import.meta.url);

#参考
https://stackoverflow.com/questions/31931614/require-is-not-defined-node-js

As Abel said, ES Modules in Node >= 14 no longer have require by default.

If you want to add it, put this code at the top of your file:

import { createRequire } from 'module';
const require = createRequire(import.meta.url);
6
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
6
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?