1

I'm trying to use a npm module (https://github.com/AmyrAhmady/steamdb-js) but when I use the example code, I got a "SyntaxError: Cannot use import statement outside a module".

This is the example code:

import { Game } from "steamdb-js";

async function main() {
  const game = new Game(271590);
  await game.fetchData();
  const data = await game.parse();
  //console.log(data); // This prints out all parsed data, you can use it for easier in-code usage
  console.log(game.getGameInfo());
}

main();

and this is the error:

PS C:\Users\user\Desktop\Développement\TEST> node .\index.js
C:\Users\selim\Desktop\Développement\TEST\index.js:1
import { Game } from "steamdb-js";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1116:16)
    at Module._compile (internal/modules/cjs/loader.js:1164:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

Is it my fault or the npm module's fault?

1
  • const { Game } = require("steamdb-js"); should work if import { Game } from "steamdb-js"; doesn't. Basically there are two ways of dependency loading. The one with require is called CommonJS and is the original one used by node.js. The one with import is called ES Modules and is more modern. Commented Apr 5, 2022 at 17:34

2 Answers 2

2

Been a while since this thread was created, but I think it's my responsibility to answer that when it comes to a library that I made.

first of all, example and steamdb-js code is using ES5 standards (such as import and export syntax) You need to enable those first, you can use latest available Nodejs versions, or use a transpiler; If you are not using Node +v13 or don't know how to do that, here's my recommendation:

Now everything should work fine and steamdb-js should be able to be """execute"""

I'm putting stress on execute cause there's an issue now, we know steamdb is using CloudFlare, they have "Under Attack" mode enabled and that's an issue that can be bypassed, but the sad part is due to CloudFlare's new changes, I doubt current libraries out there work at the moment, I'll update my library as soon as I find a way.

Thanks!

(My first ever post on Stackoverflow!)

Sign up to request clarification or add additional context in comments.

Comments

1

One of the easiest ways is to just add "type": "module" to your package.json file. There's a more in-depth description in the Node Documentation.

2 Comments

I've done that, but my console says : SyntaxError: The requested module 'steamdb-js' is expected to be of type CommonJS, which does not support named exports.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.