Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

README.md, SyntaxError: Cannot use import statement outside a module #58

Open
ZNAXNOR opened this issue Jan 13, 2023 · 1 comment
Open

Comments

@ZNAXNOR
Copy link

ZNAXNOR commented Jan 13, 2023

On video time 8:17:16
README.md

I followed the instructions by commenting the lines

const ethers = require("ethers")
const fs = require("fs-extra")
require("dotenv").config()

and replacing them with

import { ethers } from "ethers"
import * as fs from "fs-extra"
import "dotenv/config" 

now, in deploy.ts file

async function main() {
const provider = new ethers.providers.JsonRpcProvider(process.env.RPC_URL)
    const wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider)
    ...
}

and in encryptKey.ts file

async function main() {
    const wallet = new ethers.Wallet(process.env.PRIVATE_KEY)
    const encryptedJsonKey = await wallet.encrypt(
        process.env.PRIVATE_KEY_PASSWORD,
        process.env.PRIVATE_KEY
    )

<process.env.PRIVATE_KEY> in both files is giving the error

var process: NodeJS.Process
Argument of type 'string | undefined' is not assignable to parameter of type 'BytesLike | ExternallyOwnedAccount | SigningKey'.
  Type 'undefined' is not assignable to type 'BytesLike | ExternallyOwnedAccount | SigningKey'.ts(2345)

I tried uncommenting const ethers = require("ethers"), replacing import { ethers } from "ethers" and the error is gone but when run,

(node:10196) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
import * as fs from "fs-extra"
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:74:18)
    at wrapSafe (node:internal/modules/cjs/loader:1141:20)
    at Module._compile (node:internal/modules/cjs/loader:1182:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
    at Module.load (node:internal/modules/cjs/loader:1081:32)
    at Module._load (node:internal/modules/cjs/loader:922:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

Node.js v18.13.0

please help!

@mdipanjan
Copy link

For Es6 import you need to import the fs module like this: import fs from "fs-extra";

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

No branches or pull requests

2 participants