{"id":1615,"date":"2022-07-24T13:53:27","date_gmt":"2022-07-24T04:53:27","guid":{"rendered":"https:\/\/teno-hira.com\/media\/?p=1615"},"modified":"2023-06-09T22:23:50","modified_gmt":"2023-06-09T13:23:50","slug":"%e3%80%90node-js%e3%80%91es-module%e3%81%a7__dirname%e3%82%92%e4%bd%bf%e3%81%86%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/teno-hira.com\/media\/?p=1615","title":{"rendered":"\u3010Node.js\u3011ES Module\u3067__dirname\u3092\u4f7f\u3046\u65b9\u6cd5"},"content":{"rendered":"<p>&nbsp;<\/p>\n<h2>\u554f\u984c\u30fb\u80cc\u666f\uff5cReferenceError: __dirname is not defined in ES module scope<\/h2>\n<p>Node.js\u3067<code>__dirname<\/code>\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u73fe\u5728\u306eJavaScript\u30d5\u30a1\u30a4\u30eb\u304c\u5b58\u5728\u3059\u308b\u30d5\u30a9\u30eb\u30c0\u30fc\u306e\u30d1\u30b9\u3092\u53d6\u5f97\u3067\u304d\u308b\u3002<\/p>\n<p>Node.js\u3092\u6271\u3063\u3066\u3044\u308b\u8005\u306a\u3089\u3001\u5229\u7528\u3059\u308b\u3053\u3068\u304c\u591a\u3044\u6a5f\u80fd\u306e1\u3064\u3060\u3068\u601d\u3046\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u304c\u3057\u304b\u3057\u3001<code>__dirname<\/code>\u306fCommon JS\u3067\u3057\u304b\u30b5\u30dd\u30fc\u30c8\u3055\u308c\u3066\u304a\u3089\u305a\u3001ES Module\u3067\u5229\u7528\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u6012\u3089\u308c\u3066\u3057\u307e\u3046\u3002<\/p>\n<blockquote><p>ReferenceError: __dirname is not defined in ES module scope<br \/>\nThis file is being treated as an ES module because it has a '.js' file extension and '\/example\/example\/example\/example\/server\/package.json' contains \"type\": \"module\". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.<\/p>\n<p>\u7d04\uff09<code>__dirname<\/code>\u306fES module\u30b9\u30b3\u30fc\u30d7\u3067\u306f\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002package.json\u3067<code>\"type\":\"module\"<\/code>\u3092\u6307\u5b9a\u3057\u3066\u3044\u3066\u304b\u3064\u62e1\u5f35\u5b50\u304c<code>.js<\/code>\u306a\u306e\u3067\u3001\u3053\u306e\u30d5\u30a1\u30a4\u30eb\u306fES module\u3068\u3057\u3066\u6271\u308f\u308c\u307e\u3059\u3002CommonJS\u306eScript\uff08<code>__dirname<\/code>\u306e\u3053\u3068\uff09\u3092\u5229\u7528\u3057\u305f\u3044\u5834\u5408\u306f\u3001\u62e1\u5f35\u5b50\u306b<code>.cjs<\/code>\u3092\u4f7f\u7528\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>\u3061\u306a\u307f\u306b\u3053\u306e\u30a8\u30e9\u30fc\u306f\u6b21\u306e\u30b3\u30fc\u30c9\u3092\u8a18\u8ff0\u3057\u305f\u969b\u306b\u767a\u751f\u3057\u305f\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-js\">app.use(express.static(path.join(__dirname, '..\/client\/build')));<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>\u3053\u306e\u5834\u5408\u3001\u7d20\u76f4\u306b\u62e1\u5f35\u5b50\u306b<code>.cjs<\/code>\u3092\u4f7f\u7528\u3059\u308b\u306a\u308apackage.json\u3067<code>\"type\":\"commonjs\"<\/code>\u3092\u8a18\u8ff0\u3059\u308b\u306a\u308a\u3057\u3066\u3001Common JS\u30d5\u30a1\u30a4\u30eb\u3068\u3057\u3066\u53d6\u308a\u6271\u3048\u3070\u6e08\u3080\u306e\u3060\u304c\u3001\u3069\u3046\u3057\u3066\u3082ES Module\u3092\u5229\u7528\u3057\u305f\u3044\u30b7\u30fc\u30f3\u3082\u3042\u308b\u3060\u308d\u3046\u3002<\/p>\n<h2>\u89e3\u6c7a\u7b56<\/h2>\n<p>\u7d50\u8ad6\u3068\u3057\u3066\u3001\u3053\u306e\u554f\u984c\u306f<code>{fileURLToPath}<\/code>\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u3067\u89e3\u6c7a\u3067\u304d\u308b\u3002<\/p>\n<p>\u307e\u305a\u306f\u3001Node.js\u5074\uff08index.js\u30d5\u30a1\u30a4\u30eb\uff09\u306b\u3066\u3001<code>path<\/code>\u3068<code>fileURLToPath<\/code>\u3092import\u3059\u308b\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-js\">import path from 'path';\r\nimport { fileURLToPath } from 'url';<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>\u6b21\u306b\u3001<code>fileURLToPath<\/code>\u3092\u5229\u7528\u3057\u3066\u3001<code>__dirname<\/code>\u306e\u6a5f\u80fd\u3092\u7591\u4f3c\u7684\u306b\u518d\u73fe\u3059\u308b\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-js\">const __filename = fileURLToPath(import.meta.url);\r\n\r\nconst __dirname = path.dirname(__filename);<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>\u3053\u308c\u3067\u3001ES Module\u3067\u3082CommonJS\u306e__dirname\u6a5f\u80fd\u3092\u518d\u73fe\u3067\u304d\u308b\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/flaviocopes.com\/fix-dirname-not-defined-es-module-scope\/\">How to fix \"__dirname is not defined in ES module scope\"<\/a><\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/px.a8.net\/svt\/ejp?a8mat=3B96LD+1SWAHM+3XAE+609HT\" rel=\"nofollow\"><br \/>\n<img loading=\"lazy\" decoding=\"async\" border=\"0\" width=\"300\" height=\"250\" alt=\"\" src=\"https:\/\/www21.a8.net\/svt\/bgt?aid=200302753109&wid=002&eno=01&mid=s00000018311001009000&mc=1\"><\/a><br \/>\n<img loading=\"lazy\" decoding=\"async\" border=\"0\" width=\"1\" height=\"1\" src=\"https:\/\/www17.a8.net\/0.gif?a8mat=3B96LD+1SWAHM+3XAE+609HT\" alt=\"\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; \u554f\u984c\u30fb\u80cc\u666f\uff5cReferenceError: __dirname is not defined in ES module scope Node.js\u3067__dirname\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u73fe\u5728\u306eJavaScript\u30d5\u30a1\u30a4\u30eb\u304c\u5b58\u5728\u3059\u308b\u30d5\u2026<\/p>\n","protected":false},"author":15,"featured_media":1616,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[87],"tags":[],"class_list":["post-1615","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cat-nodejs"],"_links":{"self":[{"href":"https:\/\/teno-hira.com\/media\/index.php?rest_route=\/wp\/v2\/posts\/1615","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/teno-hira.com\/media\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/teno-hira.com\/media\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/teno-hira.com\/media\/index.php?rest_route=\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/teno-hira.com\/media\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1615"}],"version-history":[{"count":5,"href":"https:\/\/teno-hira.com\/media\/index.php?rest_route=\/wp\/v2\/posts\/1615\/revisions"}],"predecessor-version":[{"id":1942,"href":"https:\/\/teno-hira.com\/media\/index.php?rest_route=\/wp\/v2\/posts\/1615\/revisions\/1942"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/teno-hira.com\/media\/index.php?rest_route=\/wp\/v2\/media\/1616"}],"wp:attachment":[{"href":"https:\/\/teno-hira.com\/media\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/teno-hira.com\/media\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/teno-hira.com\/media\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}