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?
const { Game } = require("steamdb-js");should work ifimport { Game } from "steamdb-js";doesn't. Basically there are two ways of dependency loading. The one withrequireis called CommonJS and is the original one used by node.js. The one withimportis called ES Modules and is more modern.