Using Javascript to access file in Truffle console:path problem

Hi,

I am trying to access a file in Truffle console using javascript command:

$ truffle console
truffle(development)> const contractPath = path.resolve("~/Truffle_programs/search_opcode/", "contracts", "Attack.sol"); 
undefined
truffle(development)> const contractCode = fs.readFileSync(contractPath, "utf8");

I am getting following error message:

Thrown:
{ fs.js:114
throw err;
^
Error: ENOENT: no such file or directory, open '/home/zulfi/Truffle_programs/search_opcode/contracts/~/Truffle_programs/search_opcode/contracts/Attack.sol'

My file resides at:'/home/zulfi/Truffle_programs/search_opcode/contracts/

and its name is: Attack.sol

Somebody please guide me.

Zulfi.

Hi,

As can be read in the docs, Node’s path.resolve() method resolves a sequence of paths or path segments into an absolute path.

If, after processing all given path segments, an absolute path has not been generated, the current working directory is used.

I guess this is what is happening in your case.

A simple fix would be to make the first path segment absolute:

const contractPath = path.resolve("~/Truffle_programs/search_opcode/", "contracts", "Attack.sol");
// '/home/zulfi/Truffle_programs/search_opcode/contracts/~/Truffle_programs/search_opcode/contracts/Attack.sol'

const contractPath1 = path.resolve("/~/Truffle_programs/search_opcode/", "contracts", "Attack.sol");
// '/~/Truffle_programs/search_opcode/contracts/Attack.sol'

Does that help?

P.S. What is Attack.sol doing? That sounds a bit sus in the contect of Ethereum

1 Like

Hi my friend,

Thanks for your response.

I got the following error:

$ truffle console
truffle(development)> const contractPath1 = path.resolve("/~/Truffle_programs/search_opcode/", "contracts", "Attack.sol");
undefined
truffle(development)> const contractCode = fs.readFileSync(contractPath1, "utf8");
Thrown:
{ fs.js:114
    throw err;
    ^

Error: ENOENT: no such file or directory, open '/~/Truffle_programs/search_opcode/contracts/Attack.sol'

Somebody please guide me.

Zulfi.

Oops, sorry. Yeah, that won’t work.

I don’t know how I managed to post that, but what I meant to suggest was to use "/home/zulfi/" instead of "~/".

const contractPath = path.resolve('/home/zulfi/Truffle_programs/search_opcode/', 'contracts', 'Attack.sol');

Which should resolve to:

'/home/zulfi/Truffle_programs/search_opcode/contracts/Attack.sol'

That should work in your specific use case, but if you don’t want to hardcode "/home/zulfi/" and would like the path to resolve to the current user’s home directory (on OSX/Linux), you could always grab that from process.env.HOME.

const contractPath = path.resolve(process.env.HOME, 'Truffle_programs/search_opcode/', 'contracts', 'Attack.sol');

Which should also resolve to:

'/home/zulfi/Truffle_programs/search_opcode/contracts/Attack.sol'

Is that what you’re after?

Ref.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.