node-redis
node-redis copied to clipboard
Basic Example not going through
Description
Hi, I am new to node and trying to run the basic example you provide. When I run it I get
import { createClient } from 'redis'; ^^^^^^
SyntaxError: Cannot use import statement outside a module at internalCompileFunction (node:internal/vm:73:18) at wrapSafe (node:internal/modules/cjs/loader:1187:20) at Module._compile (node:internal/modules/cjs/loader:1231:27) at Module._extensions..js (node:internal/modules/cjs/loader:1321:10) at Module.load (node:internal/modules/cjs/loader:1125:32) at Module._load (node:internal/modules/cjs/loader:965:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12) at node:internal/main/run_main_module:23:47
Node.js v20.0.0
Node.js Version
v20.0.0
Redis Server Version
latest docker image
Node Redis Version
Platform
Windows 10
Logs
import { createClient } from 'redis';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at internalCompileFunction (node:internal/vm:73:18)
at wrapSafe (node:internal/modules/cjs/loader:1187:20)
at Module._compile (node:internal/modules/cjs/loader:1231:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1321:10)
at Module.load (node:internal/modules/cjs/loader:1125:32)
at Module._load (node:internal/modules/cjs/loader:965:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
at node:internal/main/run_main_module:23:47
Node.js v20.0.0
@eliassal not a bug of node-redis, maybe you have not configured the "module" type in your package.json, hence Node is treating it as CommonJS, which does not support import statements
try to add this on your package.json
{
"type": "module" <-- THIS ONE ONLY
}
@eliassal you can try below code to import createClient
.
const { createClient } = require('redis');
const client = createClient().on('error', err => console.log("[Error]: ", err)).connect();