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

SyntaxError: Cannot use import statement outside a module #138

Open
kroggen opened this issue Dec 31, 2021 · 2 comments
Open

SyntaxError: Cannot use import statement outside a module #138

kroggen opened this issue Dec 31, 2021 · 2 comments

Comments

@kroggen
Copy link
Member

kroggen commented Dec 31, 2021

I am getting this error:

import { AergoClient, GrpcProvider } from '@herajs/client';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

when running a simple script:

import { AergoClient, GrpcProvider } from '@herajs/client';

const aergo = new AergoClient({}, new GrpcProvider({url: 'testnet-api.aergo.io:7845'}));

async function update() {
    const blockchainState = await aergo.blockchain();
    console.log(blockchainState.bestHeight, blockchainState.bestBlockHash);
    setTimeout(update, 3000);
}
update();

The same error with node 14 and 16, tested on Mac

@kroggen
Copy link
Member Author

kroggen commented Dec 31, 2021

I was able to make it work by using require(), like this:

var client = require('@herajs/client');

const aergo = new client.AergoClient({}, new client.GrpcProvider({url: 'testnet-api.aergo.io:7845'}));

It would be good to have this on the documentation

@graup
Copy link
Member

graup commented Feb 17, 2022

That's not really an issue with this code but with your code's configuration. To use ES modules, you need to enable it in your package.json or use a bundler to transpile it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants