0

I have some code using library:

import { generateKeyPair } from 'jose/util/generate_key_pair'
async function funkcja () {
const {publicKey, privateKey} = await generateKeyPair('PS256')
console.log(publicKey)
console.log(privateKey)
}
funkcja()

and while trying to node it i get following error:

  ubuntu@ubuntu-VirtualBox:~/Desktop/js$ node hello.js
/home/ubuntu/Desktop/js/hello.js:1
import { generateKeyPair } from 'jose/util/generate_key_pair'
       ^

SyntaxError: Unexpected token {
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

I have no idea why this is happening, my friend is using exact same library and same code and on his computer it's working fine. (path to the library is correct) The only differennce is that he has node v12 and i have node v10.

  • [Does this help](https://stackoverflow.com/questions/39436322/node-js-syntaxerror-unexpected-token-import)? – Andy Apr 25 '21 at 12:02
  • Does this answer your question? [Node.js - SyntaxError: Unexpected token import](https://stackoverflow.com/questions/39436322/node-js-syntaxerror-unexpected-token-import) – Diogo Rocha Apr 25 '21 at 12:06

2 Answers2

0

Try this one

const { generateKeyPair } = require('jose/util/generate_key_pair');

Don't forget to use module.exports in generate_key_pair js file

Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
VP Helper
  • 1
  • 2
0

How i read it in Update on ES6 Modules in Node.js the import of variables via curly brackets seems to be not supported in node.js.

it will simply not be possible to use the syntax:

import {foo, bar} from 'foobar';

But this would be possible:

import foobar from 'foobar';
console.log(foobar.foo(), foobar.bar());

So if generateKeyPair is a variable or function from 'jose/util/generate_key_pair' it should be:

import generate_key_pair from 'jose/util/generate_key_pair'
async function funkcja () {
  const {publicKey, privateKey} = await generate_key_pair.generateKeyPair('PS256')
  console.log(publicKey)
  console.log(privateKey)
}
funkcja()
biberman
  • 5,606
  • 4
  • 11
  • 35