node-redis icon indicating copy to clipboard operation
node-redis copied to clipboard

Basic Example not going through

Open eliassal opened this issue 1 year ago • 2 comments

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

[email protected]

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 avatar Jan 01 '24 13:01 eliassal

@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
}

SiNONiMiTY avatar Jan 04 '24 10:01 SiNONiMiTY

@eliassal you can try below code to import createClient.

const { createClient } = require('redis');

const client = createClient().on('error', err => console.log("[Error]: ", err)).connect();

laststylebender14 avatar Jan 16 '24 05:01 laststylebender14