/main.mo:1.1-1.32: import error [M0011], canister alias "ledger" not defined

Getting this error when trying to deploy to the IC:
/main.mo:1.1-1.32: import error [M0011], canister alias "ledger" not defined

this is how the I’m importing the ledger canister:

import Ledger "canister:ledger";

Anyone see any obvious error?

I’m not sure what the error is but I find it easier to import a canisters from my type file and just use the service definition. Then you can have a set function to pass in the canister id.

1 Like

Do you have an example of this that you could link? Thank you :pray:t5:

@quint nails it here: icp-canister/main.mo at main · aviate-labs/icp-canister · GitHub. If you need more of the ledger interface you can just add it to ledger.mo.

Definitely appreciate the assist! And I’ll be goin with this method if all else fails, but for some reason, i thought you could just import the ledger canister and begin using it without having to go through the trouble of creating an actor class for it and instantiating that actor class with the ledger canister-Id.

Would you, or anyone else reading this, be able to confirm or deny this? Thanks in advance :pray:t5:

@skilesare I’ve implemented the method for importing the ledger canister that you referenced here and got it running locally. Now all I need is the canister-id for the mainnet ledger canister so that i can use it to instantiate the actor. do you know where I can find it? I’m checking git hub but I’m not seeing it.

Cross posting an answer here: Where can I find the canister-id for the mainnet ledger canister? - #2 by Ori

Looking at your other post, the reason for the error is that you removed the ledger entry from your dfx.json, which is where dfx looks for your canister import. Mainnet canisters outside of your project don’t import the same way (yet), so for now you have to specify the canister id and define the actor interface as per quint’s or this example (in fact it doesn’t actually have to be the whole ledger canister interface, just the methods that you’re calling so the compiler can perform type checking).
Defining the actor type in a separate file is a good approach.

Re the accidentally deploying the ledger issue you’ve faced, as you’ve seen the issue arises because dfx deploy deploys all canisters specified in the dfx.json and for local development the tutorial example has you include the ledger canister here too. One solution, as suggested, is to deploy canisters individually by naming them, eg dfx deploy my_canister, you could do this for each of your app’s canisters and just don’t deploy the ledger canister.

Possibly a better workflow for you Jesse, is to set up the local ledger canister in an entirely separate project, see this post here:

Doing things this way would ensure you only ever have your app’s canisters in your app project, and you can then simply dfx deploy --network ic and only your app’s canisters would be deployed. You would need to call the ledger using a canister id and actor definition as above for both local and mainnet deployment (note the local and mainnet canister ids will be different), but this is a more consistent approach and it separates concerns, which is useful when it comes to testing and deploying, especially if you start to build projects that call multiple other project’s canisters.


I get this after I do your method?
trapped explicitly: Panicked at ‘Deserialization Failed: “Fail to decode argument 0 from table0 to record { account : vec nat8 }”’, /ic/rs/rust_canisters/dfn_core/src/endpoint.rs:34:41"

Any idea what this error is?

Just kidding, the answer is I was turning the principal ID to a blob and not doing the math conversion to an accountid then to a blob. Answer in here Enable Canisters to Hold ICP - #217 by jorgenbuilder

Strangely, even when you include the canister name in dfx.json, the VSCode Motoko plugin still says “canister alias … not defined”.

But when I run dfx build in the terminal, I don’t get that compiler error.

Team, I have followed the tutorial for local deployment of the ledger, but how can I call methods on it from inside one of my local canisters? I can successfully call the mainnet ledger (as described above), but I’d really like to test locally. How does one import and use a local ledger canister?

My local ledger canister is working ( CLI commands are successful ) and I can call it directly from JavaScript; just not from inside another local canister. I suspect it is because I’m not using the correct principal for the local ledger. How would I find that?

I am likely missing something conceptually, any guidance would be appreciated!

EDIT: I just copied the principal from here and it seemes to work: it this the correct way to do it?

This is one way to do it. You can also have a look at the ledger-transfer examples:


Can I ask how you ended up fixing this? I have the same issue where I get canister alias icp_ledger_canister is not defined, whereas dfx build doesn’t complain, so it appears to be an issue with the VSCode extension.

1 Like

In case anyone else is also running into this issue, please feel free to reply here or in the corresponding GitHub issue so we can narrow down the root cause.

1 Like