Error creating canister using ic-management library: The specified canister does not exist

const agent = await createAgent();
    const managementCanister = ICManagementCanister.create({ agent });


    if (host === "https://ic0.app") {
      try {
        const walletCanisterId = Principal.fromText(
          process.env.WALLET_CANISTER_ID
        );

        console.log("Checking wallet canister:", walletCanisterId.toText());
        const statuss = await managementCanister.canisterStatus(
          walletCanisterId
        );
        console.log("Wallet canister verified successfully", statuss);
      } catch (walletError) {
        console.error("Wallet canister verification failed:", walletError);
        throw new Error(
          "Wallet canister not found or not accessible. Check your WALLET_CANISTER_ID and credentials."
        );
      }
    }


    let newFrontendCanisterId;
    if (host.includes("localhost") || host.includes("127.0.0.1")) {

      const canister_id =
        await managementCanister.provisionalCreateCanisterWithCycles({
          settings: {
            controllers: [(await agent.getPrincipal()).toText()],
            freezing_threshold: [],
            memory_allocation: [],
            compute_allocation: [],
          },
          amount: [],
        });
      newFrontendCanisterId = canister_id;
    } else {
      const canister_id = await managementCanister.createCanister({
        settings: {
          controllers: [(await agent.getPrincipal()).toText()],
        },
      });
      newFrontendCanisterId = canister_id;
    }

i’m getting this error when trying to create the canister on icp network though it was working fine on localhost

Error creating canister: AgentHTTPResponseError [AgentError]: Server returned an error:
  Code: 400 (Bad Request)
  Body: error: canister_not_found
details: The specified canister does not exist.

    at HttpAgent._HttpAgent_requestAndRetry (/Users/mac/Documents/Learning/Misc/ManagementCanister/n
    at HttpAgent._HttpAgent_requestAndRetry .... {
  response: {
    ok: false,
    status: 400,
    statusText: 'Bad Request',
    headers: [
      [Array], [Array],
      [Array], [Array],
      [Array], [Array],
      [Array], [Array],
      [Array]
    ]
  }
}

any clue on why i could be getting this error ? i used provisionalCreateCanisterWithCycles for local replica and it worked but it doesn’t seems to work on the ic network https://ic0.app

Alright so after some research and some kind help from “daviddalbusco” this is what i found out:
Simply put I wasn’t able to deploy the canister directly from the js script so I had to use an another way which is just as good if not a better way and that is to create a canister that manages the creation of canister for you. here’s the simple snippet for creating a canister and returning its canister_id:

import Cycles "mo:base/ExperimentalCycles";
import Principal "mo:base/Principal";

shared ({ caller = initializer }) actor class Deployer() {
  type canister_id = Principal;
  type canister_settings = {
    freezing_threshold : ?Nat;
    controllers : ?[Principal];
    memory_allocation : ?Nat;
    compute_allocation : ?Nat;
  };

  let IC = actor "aaaaa-aa" : actor {
    create_canister : { settings : ?canister_settings } -> async {
      canister_id : canister_id;
    };
  };

  public func deployCanister() : async canister_id {
    Cycles.add(1_000_000_000_000); 
    let settings = {
      freezing_threshold = null;
      controllers = ?[initializer];
      memory_allocation = null;
      compute_allocation = null;
    };

    let result = await IC.create_canister({ settings = ?settings });
    let canister_id = result.canister_id;

    canister_id;
  };
};

Note: It won’t work on playground because it gives error that you can only call create_canister method with null settings
Now we can simple just install the code to the created canister using a js script:

import { ICManagementCanister } from "@dfinity/ic-management";
import { readFile } from "fs/promises";    
  const managementCanister = ICManagementCanister.create({ agent });
  const wasmBuffer = await readFile(PATH_TO_WASM_FILE);
  const wasmModule = new Uint8Array(wasmBuffer);
  await managementCanister.installCode({
      mode: { reinstall: null },
      canisterId: <your_canister_id> , 
      wasmModule, 
      arg: [],
    });

download the assetstorage wasm from the dfx repo
make sure to use the assetstorage.wasm (without the .gz)