"SyntaxError: Cannot use import statement outside a module" when attempting to build a turborepo app

I have a turbo repo that has a websocket app. I have a package for my redis client so it can be used in all my apps. However when I try to start my websocket app in production I get
F:\Dev\radiance\packages\redis\index.ts:1
import Redis, { type RedisOptions } from "ioredis";
^^^^^^

SyntaxError: Cannot use import statement outside a module
at internalCompileFunction (node:internal/vm:73:18)
at wrapSafe (node:internal/modules/cjs/loader:1176:20)
at Module._compile (node:internal/modules/cjs/loader:1218:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
at Module.load (node:internal/modules/cjs/loader:1117:32)
at Module._load (node:internal/modules/cjs/loader:958:12)
at Module.require (node:internal/modules/cjs/loader:1141:19)
at require (node:internal/modules/cjs/helpers:110:18)
at Object.<anonymous> (F:\Dev\radiance\apps\websocket\dist\wsServer.js:4:17)
at Module._compile (node:internal/modules/cjs/loader:1254:14)
F:\Dev\radiance\packages\redis\index.ts:1
import Redis, { type RedisOptions } from "ioredis";
^^^^^^

SyntaxError: Cannot use import statement outside a module
at internalCompileFunction (node:internal/vm:73:18)
at wrapSafe (node:internal/modules/cjs/loader:1176:20)
at Module._compile (node:internal/modules/cjs/loader:1218:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
at Module.load (node:internal/modules/cjs/loader:1117:32)
at Module._load (node:internal/modules/cjs/loader:958:12)
at Module.require (node:internal/modules/cjs/loader:1141:19)
at require (node:internal/modules/cjs/helpers:110:18)
at Object.<anonymous> (F:\Dev\radiance\apps\websocket\dist\wsServer.js:4:17)
at Module._compile (node:internal/modules/cjs/loader:1254:14)
I am believe its not transpiling the internal pacakges correctly but unsure how to solve. Linked the repo below. https://github.com/ZiiMs/Radiance Thanks for the help.
GitHub
GitHub - ZiiMs/Radiance: A simple chat app, based on discord.
A simple chat app, based on discord. Contribute to ZiiMs/Radiance development by creating an account on GitHub.
Solution:
Solved it by removing type module, and adding ts loader to esbuild.
Jump to solution
3 Replies
Neto
Neto2y ago
Stack Overflow
Why is 'type: module' in package.json file?
I upgraded the node and built the existing file. But it didn't build, and there was an error. Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: │ │ ~~/nuxt.
ZiiM
ZiiMOP2y ago
Changing it to module gives me error saying:
ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and 'F:\Dev\radiance\apps\websocket\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and 'F:\Dev\radiance\apps\websocket\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
Solution
ZiiM
ZiiM2y ago
Solved it by removing type module, and adding ts loader to esbuild.
Want results from more Discord servers?
Add your server