Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package import not working #2652

Closed
gg-hsi opened this issue Nov 13, 2023 · 6 comments
Closed

Package import not working #2652

gg-hsi opened this issue Nov 13, 2023 · 6 comments
Labels

Comments

@gg-hsi
Copy link

gg-hsi commented Nov 13, 2023

Description

I'm trying to execute a simple script but package import is not working, I'm getting an error
here's the code used

import config from './src/config';
import { createClient } from 'redis';

async function main() {
  const { host, port, username, password } = config.get('redis');
  const client = await createClient({ username, password })
     .on('error', (err) => console.log('Redis Client Error', err))
     .connect();
   await client.disconnect();

  process.exit(0);
}

main().catch(console.error);

I'm using Node.js v18.16.0
Any help would be appreciated here.
Thanks

Node.js Version

Node.js v18.16.0

Redis Server Version

redis_version:6.2.13

Node Redis Version

redis@4.6.10

Platform

Linux

Logs

/home/Project/node_modules/@redis/client/dist/lib/cluster/cluster-slots.js:89
        return node.client ?? __classPrivateFieldGet(this, _RedisClusterSlots_instances, "m",
_RedisClusterSlots_createNodeClient).call(this, node);
                            ^
SyntaxError: Invalid or unexpected token
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
@gg-hsi gg-hsi added the Bug label Nov 13, 2023
@leibale
Copy link
Collaborator

leibale commented Nov 13, 2023

Seems like it is complaining about the ?? operator, which was added in node 14 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing#browser_compatibility)

Are you sure you are using node 18.16.0? Can you please console.log(process.version); just to make sure?

@gg-hsi
Copy link
Author

gg-hsi commented Nov 13, 2023

I'm sure about the version, here's the full logs:

/home/Project/node_modules/@redis/client/dist/lib/cluster/cluster-slots.js:89
        return node.client ?? __classPrivateFieldGet(this, _RedisClusterSlots_instances, "m", _RedisClusterSlots_createNodeClient).call(this, node);
                            ^

SyntaxError: Invalid or unexpected token
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)

Node.js v18.16.0
error Command failed.                                                                                  
Exit code: 1
Command: /home/Project/.nvm/versions/node/v18.16.0/bin/node
Arguments: -r esm script.js
Directory: /home/Project/
Output:

info Visit https://yarnpkg.com/en/docs/cli/node for documentation about this command. 

@leibale
Copy link
Collaborator

leibale commented Nov 13, 2023

@gg-hsi

I just did

nvm install v18.16.0
nvm use v18.16.0
import { createClient } from 'redis';

console.log(`Node Version: ${process.version}`);

const client = await createClient()
    .on('error', err => console.log('Redis Client Error', err))
    .connect();

console.log('Connected');

await client.disconnect();

console.log('Disconnected');

and this is what I get in the logs:

Node Version: v18.16.0
Connected
Disconnected

Am I doing anything differently?

@gg-hsi
Copy link
Author

gg-hsi commented Nov 13, 2023

I'm using this cmd:

node -r esm script.js

Which one did you use?

@leibale
Copy link
Collaborator

leibale commented Nov 13, 2023

node script.mjs

edit: why are you using -r esm? that's probably what causing the problem..
standard-things/esm#909

@gg-hsi
Copy link
Author

gg-hsi commented Nov 14, 2023

I'm importing the configuration from another .js file, that's why I'm using -r esm.
Problem solved.
Thanks.

@gg-hsi gg-hsi closed this as completed Nov 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants