๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿชฒ debug

ReferenceError: __dirname is not defined in ES module scope

2024.06.26 - [๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป dev] - ๋ชจ๋“ˆ ์‹œ์Šคํ…œ์—์„œ ๋ชจ๋“ˆ ์‹œ์Šคํ…œ์ด ๋ฌด์—‡์ธ์ง€, ์–ด๋–ค ๊ฒƒ๋“ค์ด ์žˆ๊ณ , ์žฅ๋‹จ์ ๋“ค์„ ์‚ดํŽด๋ดค๋‹ค.

 

๋ชจ๋“ˆ ์‹œ์Šคํ…œ

๐Ÿ—ฃ๏ธ import๊ณผ require์˜ ์ฐจ์ด๋ฅผ ์•Œ๊ณ  ๊ณ„์‹œ๋‚˜์š”?  ๐Ÿค” ๋…ธ๋“œ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” require ๋งŽ์ด ๋ณธ ๊ฑฐ ๊ฐ™๊ณ , react์—์„œ  import ๋งŽ์ด ์ผ๋˜ ๊ฑฐ ๊ฐ™์€๋ฐ? import๊ณผ require์— ๋Œ€ํ•ด ์•Œ๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ชจ๋“ˆ ์‹œ์Šคํ…œ์ด๋ผ๋Š” ๊ฒƒ

pyotato-dev.tistory.com

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ์˜ค๋Š˜์€ turborepo๋กœ nextjs์— sass๋กœ ์Šคํƒ€์ผ๋งํ•˜๊ธฐ ์œ„ํ•ด scss์„ next.config.mjs์— ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๋‹ค๋ค„๋ณด์ž.

 

sass ๊ฐ€ ๋ชจ๋…ธ๋ ˆํฌ์— ์ž˜ ๋Œ์•„๊ฐ€๊ธฐ ์œ„ํ•ด์„œ๋Š” sass compiler ์„ค์ •์„ ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

nextjs ๊ฐœ๋ฐœ ๋ฌธ์„œ์— ๋‚˜์˜จ ๋Œ€๋กœ ์„ค์ •์„ ํ•ด์ฃผ์ž.

pnpm --filter $์„ค์น˜ํ•˜๋ ค๋Š”_๋ ˆํฌ install sass --save-dev

 

๋‹ค์Œ์€ ๋ฌธ์„œ์— ๋‚˜์˜จ sass next.config.js ์„ค์ •์ด๋‹ค.

const path = require('path')
 
module.exports = {
  sassOptions: {
    includePaths: [path.join(__dirname, 'styles')],
  },
}

 

ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๋Š” ํŠธ๋ฆฌ์‰์ดํ‚น์„ ํฌํ•จํ•œ ESM์˜ ์žฅ์ ๋“ค์„ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด type:"module"๋กœ ์„ค์ •ํ•ด๋‹ค.

๋”ฐ๋ผ์„œ CJS ๋ฌธ๋ฒ•์ธ require์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

require is not defined in ES module scope, you can use import instead

 

๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•ด ์ฃผ๋ฉด ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธด๋‹ค.

// @ts-check
 
/**
 * @type {import('next').NextConfig}
 */
const nextConfig = {
  sassOptions: {
    includePaths: [path.join(__dirname, 'styles')],
  },
}
 
export default nextConfig

 

__dirname is not defined in ES module scope

path, __dirname์„ ๋ชจ๋“ˆ ์Šค์ฝ”ํ”„์—์„œ ์ฐพ์ง€ ๋ชปํ•˜๊ณ  ์žˆ๋‹ค.

์™œ ๊ทธ๋Ÿด๊นŒ?

CJS ๋‚ด์— ๋‚ด์žฅ๋œ ๊ธ€๋กœ๋ฒŒ ๊ฐ’๋“ค์ด ESM์—๋Š” ์กด์žฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด ๊ฐ’๋“ค์—๋Š” __filename, exports, module, require๊ฐ€ ์žˆ๋‹ค.

[์ถœ์ฒ˜: nodejs.org ] __dirname์€ ๊ธ€๋กœ๋ฒŒ ๋ณ€์ˆ˜์ผ ๊ฒƒ ๊ฐ™์ง€๋งŒ ์•„๋‹ˆ๋‹ค.

 

๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด import๋ฅผ ํ•ด์ฃผ์ž.

// @ts-check

/**
 * https://nextjs.org/docs/app/building-your-application/styling/sass#customizing-sass-options
 */

import path from "path"; //โž•
import { fileURLToPath } from "url"; //โž•

const __dirname = fileURLToPath(new URL(".", import.meta.url)); //โž•

/** @type {import('next').NextConfig} */
const nextConfig = {
  sassOptions: {
    includePaths: [path.join(__dirname, "styles")],
  },
};

export default nextConfig;

๐Ÿ€ ๋งˆ์น˜๋ฉด์„œ

์ด๋ฒˆ์—๋Š” ์•„์ฃผ ๊ฐ„๋‹จํ•œ ๋‚ด์šฉ์ธ sass๋ฅผ next.config.mjs์— ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์‚ดํŽด๋ดค๋‹ค.

__filename, exports, module, require๊ฐ€ CJS์˜ ๋‚ด์žฅ ๊ธ€๋กœ๋ฒŒ ๋ณ€์ˆ˜๊ธฐ ๋•Œ๋ฌธ์— import๋ฅผ ํ•ด์ค˜์•ผ ํ•œ๋‹ค๋Š” ๊ฑธ ์ƒˆ๋กญ๊ฒŒ ์•Œ์•˜๋‹ค. 

 


๐Ÿ“š References

https://nextjs.org/docs/app/building-your-application/styling/sass#customizing-sass-options

 

Styling: Sass | Next.js

Style your Next.js application using Sass.

nextjs.org

https://nextjs.org/docs/app/api-reference/next-config-js

 

API Reference: next.config.js Options | Next.js

Learn how to configure your application with next.config.js.

nextjs.org

https://blog.logrocket.com/alternatives-dirname-node-js-es-modules/

 

Alternatives to __dirname in Node.js with ES modules - LogRocket Blog

ES modules with Node.js add a new, standardized global that allows your code to run anywhere, locally or remotely.

blog.logrocket.com

https://nodejs.org/docs/latest/api/modules.html#__dirname

 

Modules: CommonJS modules | Node.js v22.4.1 Documentation

Modules: CommonJS modules# CommonJS modules are the original way to package JavaScript code for Node.js. Node.js also supports the ECMAScript modules standard used by browsers and other JavaScript runtimes. In Node.js, each file is treated as a separate mo

nodejs.org

https://nodejs.org/docs/latest/api/globals.html#__dirname

 

Global objects | Node.js v22.4.1 Documentation

Global objects# These objects are available in all modules. The following variables may appear to be global but are not. They exist only in the scope of CommonJS modules: The objects listed here are specific to Node.js. There are built-in objects that are

nodejs.org