Can't Import backend canister ID

I have a svelte project with the following structure:

src/backend
    frontend
    declarations

When I use the canister ID i imported with:

import { idlFactory, canisterId } from "../../../declarations/backend";

I get:
Error: Canister ID is required, but received undefined instead. If you are using automatically generated declarations, this may be because your application is not setting the canister ID in process.env correctly.

Even though my “.env” file is populated:

CANISTER_ID_BACKEND='by6od-j4aaa-aaaaa-qaadq-cai'
...

It should be working! Try placing a line with

console.log(process.env.CANISTER_ID_BACKEND);

and see if it’s also undefined. You may need to double check your vite or webpack (or whatever bundler) config to make sure it’s loading the .env file correctly

1 Like