๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • What would life be If we had no courage to attemp anything?
Development/Electron

[Electron] SyntaxError: Cannot use import statement outside a module

by DevIseo 2023. 6. 17.

์˜ค๋ฅ˜

๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜ค๋ฅ˜๋“ค์ด ๋œจ๋ฉด์„œ(๊ฒฝ๊ณ ์ฐฝ) electron ์•ฑ์ด ์‹คํ–‰๋˜์ง€ ์•Š์Œ.

(node:29788) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `electron --trace-warnings ...` to show where the warning was created)
App threw an error during load
D:\\2023\\march\\realPractice\\login\\public\\electron.js:1
import  * as operator from './script/operator.js'
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1062:15)
    at Module._compile (node:internal/modules/cjs/loader:1097:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1196:10)
    at Module.load (node:internal/modules/cjs/loader:1011:32)
    at Module._load (node:internal/modules/cjs/loader:846:12)
    at f._load (node:electron/js2c/asar_bundle:2:13328)
    at loadApplicationPackage (D:\\2023\\march\\realPractice\\login\\node_modules\\electron\\dist\\resources\\default_app.asar\\main.js:121:16)
    at Object.<anonymous> (D:\\2023\\march\\realPractice\\login\\node_modules\\electron\\dist\\resources\\default_app.asar\\main.js:233:9)
    at Module._compile (node:internal/modules/cjs/loader:1141:14)
[25444:0411/155342.712:ERROR:gpu_init.cc(523)] Passthrough is not supported, GL is disabled, ANGLE is

๋ฌธ์ œ์›์ธ

electron์€ CommonJS ๋ฐฉ์‹์ธ require๋กœ ์“ฐ๊ณ  ์žˆ๋Š”๋ฐ, ๊ทธ ์•ˆ์— ESM๋ฐฉ์‹์ธ import๋ฌธ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ.

์™œ ์ด๋Ÿฐ ํ˜„์ƒ์ด ๋‚˜ํƒ€๋‚ ๊นŒ?

React์™€ Electron์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋‘ ๊ฐœ์˜ ํ”„๋กœ์ ํŠธ์—์„œ ๋ชจ๋“ˆ ์‹œ์Šคํ…œ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด ์„œ๋กœ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์—, ๋ชจ๋“ˆ ์‹œ์Šคํ…œ์„ ๋งž์ถฐ์ฃผ๋Š” ์ž‘์—…์ด ํ•„์š”.

React ์•ฑ์—์„œ๋Š” import/export๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , Electron์—์„œ๋Š” require/module.exports ๋ฅผ ์‚ฌ์šฉ.

๋”ฐ๋ผ์„œ ๋‘ ๋ชจ๋“ˆ ์‹œ์Šคํ…œ์„ ๋ชจ๋‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” CommonJS ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœ.

ํ•ด๊ฒฐ ๊ณผ์ •

๋จผ์ € terminal ์—์„œ ์ œ์‹œํ•œ ๋ฐฉ๋ฒ• ๋‘๊ฐ€์ง€๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์•˜๋‹ค.

  1. package.json ํŒŒ์ผ์˜ root์— ๋‹ค์Œ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•.
    {
    	"type":"module",
    }
    
  2. ⇒ module๋กœ ์ •์˜ํ•ด๋„ ์ด๋ฏธ ํŒŒ์ผ์•ˆ์— require์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒ.
  3. .js ํ˜•์‹์„ .mjs ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•ด module ์ •์˜
  4. ⇒์—ฌ์ „ํžˆ require ์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒ
  5. require ๋ฌธ์„ import๋กœ ๋ณ€ํ™˜
  6. ⇒ node ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋“ˆ๋“ค์ด require๋กœ ๋ถˆ๋Ÿฌ์™€์•ผ ํ•˜๋Š” ๊ฒƒ๋“ค์ด ์žˆ์Œ

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

๊ฒฐ๊ตญ electron ์ฝ”๋“œ์— ์ •์˜๋œ ๋ชจ๋“  import ์„ require ๋กœ ๋ณ€๊ฒฝ.

๋˜ํ•œ exportํ•˜๋˜ ํ•จ์ˆ˜๋“ค๋„ module.exports ๋กœ ๋ณ€๊ฒฝ.

๊ทธ๋Ÿฌ๋‚˜ electron์•ฑ์€ ์‹คํ–‰๋˜์—ˆ์ง€๋งŒ ํ™”๋ฉด์ด ๋žœ๋”๋ง ๋˜์ง€ ์•Š๋Š” ๋ฌธ์ œ ๋ฐœ์ƒ.

2023.06.18 - [Development/Electron] - [Electron] electron์•ฑ ํ™”๋ฉด์ด ๋žœ๋”๋ง์ด ๋˜์ง€ ์•Š๋Š” ๋ฌธ์ œ

๋Œ“๊ธ€