简体   繁体   中英

“SyntaxError: Cannot use import statement outside a module” with a npm module

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?

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!)

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM