[hello_world] Error: Failed while trying to deploy canisters

Hi everyone. I’m a newbie at programming, sorry. I am using dfx version 0.15.1, Windows 11, Ubuntu 20.04.6

When I try to locally deploy hello_world, I keep getting this situation:

Error: Failed while trying to deploy canisters.
Caused by: Failed while trying to deploy canisters.
Failed to build all canisters.
Failed while trying to build all canisters.
The post-build step failed for canister ‘bd3sg-teaaa-aaaaa-qaaba-cai’ (hello_world_frontend) with > an embedded error: Failed to build frontend for network ‘local’.
“npm” “run” “build”’ failed with exit status ‘exit status: 1’.
Stdout:
hello_world_frontend@0.2.0 prebuild
dfx generate

I get the same error with the command “dfx deploy --playground”

I don’t know what to do. Please help me understand what is wrong. :dotted_line_face:

It looks like the dfx generate part of your build step is failing, are there any more logs below the last line that you’ve provided? If not, you can try run dfx generate alone to see what error pops up.

Hi Nathanos! Thank you for your reply.

here’s the full picture:

On requesting dfx generate I get this result:

Building canisters before generate for Motoko
Generating type declarations for canister hello_world_frontend:
src/declarations/hello_world_frontend/hello_world_frontend.did.d.ts
src/declarations/hello_world_frontend/hello_world_frontend.did.js
src/declarations/hello_world_frontend/hello_world_frontend.did
Generating type declarations for canister hello_world_backend:
src/declarations/hello_world_backend/hello_world_backend.did.d.ts
src/declarations/hello_world_backend/hello_world_backend.did.js
src/declarations/hello_world_backend/hello_world_backend.did

To my non-professional eye it looks quite sane and doesn’t look like a mistake.

Yeah it seems dfx generate is working correctly. I’ve never seen this error that have at the bottom of the screenshot.

Are you running these commands from the command prompt by any chance? There’s mentions of CMD.exe in that error.

If you’re in the standard command prompt then you’ll see your current path at the beginning of the line like this:
C:\Users\Username. If that’s the case then you should run wsl.exe to enter the Ubuntu Bash and then you’ll see username@hostname:/mnt/c/Users/Username instead.

1 Like

anytime i run npx azle new azle_hello_world
i get these errors…

mijma@DESKTOP-NS66FFM:~/development/ic-project$ npx azle new azle_hello_world
Need to install the following packages:
azle@0.18.6
Ok to proceed? (y) y
npm ERR! code ENOTEMPTY
npm ERR! syscall rename
npm ERR! path /home/mijma/.npm/_npx/22da3992a7cf1870/node_modules/azle
npm ERR! dest /home/mijma/.npm/_npx/22da3992a7cf1870/node_modules/.azle-3k9oa9UC
npm ERR! errno -39
npm ERR! ENOTEMPTY: directory not empty, rename ‘/home/mijma/.npm/_npx/22da3992a7cf1870/node_modules/azle’ → ‘/home/mijma/.npm/_npx/22da3992a7cf1870/node_modules/.azle-3k9oa9UC’

npm ERR! A complete log of this run can be found in: /home/mijma/.npm/_logs/2023-12-04T12_03_54_540Z-debug-0.log

I tried it. Still an error. But this time I was able to get the log file:

0 verbose cli C:\Program Files\nodejs\node.exe C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js
1 info using npm@8.19.2
2 info using node@v18.12.1
3 timing npm:load:whichnode Completed in 1ms
4 timing config:load:defaults Completed in 2ms
5 timing config:load:file:C:\Program Files\nodejs\node_modules\npm\npmrc Completed in 7ms
6 timing config:load:builtin Completed in 7ms
7 timing config:load:cli Completed in 2ms
8 timing config:load:env Completed in 1ms
9 timing config:load:file:C:\Windows.npmrc Completed in 1ms
10 timing config:load:project Completed in 4ms
11 timing config:load:file:C:\Users\user.npmrc Completed in 0ms
12 timing config:load:user Completed in 0ms
13 timing config:load:file:C:\Users\user\AppData\Roaming\npm\etc\npmrc Completed in 0ms
14 timing config:load:global Completed in 0ms
15 timing config:load:validate Completed in 1ms
16 timing config:load:credentials Completed in 1ms
17 timing config:load:setEnvs Completed in 1ms
18 timing config:load Completed in 19ms
19 timing npm:load:configload Completed in 20ms
20 timing npm:load:mkdirpcache Completed in 1ms
21 timing npm:load:mkdirplogs Completed in 0ms
22 verbose title npm run generate
23 verbose argv “run” “generate”
24 timing npm:load:setTitle Completed in 1ms
25 timing config:load:flatten Completed in 3ms
26 timing npm:load:display Completed in 4ms
27 verbose logfile logs-max:10 dir:C:\Users\user\AppData\Local\npm-cache_logs
28 verbose logfile C:\Users\user\AppData\Local\npm-cache_logs\2023-12-07T23_12_31_048Z-debug-0.log
29 timing npm:load:logFile Completed in 4ms
30 timing npm:load:timers Completed in 0ms
31 timing npm:load:configScope Completed in 0ms
32 timing npm:load Completed in 32ms
33 silly logfile start cleaning logs, removing 2 files
34 timing command:run Completed in 3ms
35 verbose stack Error: ENOENT: no such file or directory, open ‘C:\Windows\package.json’
36 verbose cwd C:\Windows
37 verbose Windows_NT 10.0.22621
38 verbose node v18.12.1
39 verbose npm v8.19.2
40 error code ENOENT
41 error syscall open
42 error path C:\Windows/package.json
43 error errno -4058
44 error enoent ENOENT: no such file or directory, open ‘C:\Windows\package.json’
45 error enoent This is related to npm not being able to find a file.
45 error enoent
46 verbose exit -4058
47 timing npm Completed in 49ms
48 verbose code -4058
49 error A complete log of this run can be found in:
49 error C:\Users\user\AppData\Local\npm-cache_logs\2023-12-07T23_12_31_048Z-debug-0.log

Ah, thanks for sharing that. Now I can clearly see what the issue is. You haven’t installed NodeJS inside of WSL, so it’s delegating to the NodeJS that’s installed on Windows. That won’t work because NodeJS needs to be built differently for Windows than it does for WSL, which is running a Linux OS. You can see this is the case because of the C:\Program Files\nodejs\node.exe part of your log file.

You can validate it by running whereis nodejs, if you see a C:\Program Files\... path then that confirms this is the issue.

You can install NodeJS inside of WSL according to the instructions for your distribution: Installing Node.js via package manager | Node.js, if you didn’t pick a specific distribution then the default is Debian / Ubuntu.

Or if you prefer, you could check a node version manager such as nvm, or fnm. I personally prefer fnm if you go that route and are not sure which one to choose.

After installing NodeJS, you can run whereis nodejs again and you should now see a /mnt/c/... path and this will confirm that it’s correctly installed within WSL.

1 Like

Oh, right. I installed Node js and still kept running into errors. :slightly_smiling_face: I spent a lot of time googling, but finally my problem was solved by an AI who advised me to replace a piece of code in webpack.config file. Everything worked successfully after I did that. I had almost given up hope, haha. If interested, I can send the latest error text, and the code I used to replace in the original file.

Yeah, now I’m very interested to know what was causing this!

1 Like

Here’s the latest error I’ve gotten in the midst of my battle:

Error: Failed while trying to deploy canisters.
Caused by: Failed while trying to deploy canisters.
Failed to build all canisters.
Failed while trying to build all canisters.
The post-build step failed for canister ‘y4oop-liaaa-aaaab-qacha-cai’ (hello_world_frontend) with an embedded error: Failed to build frontend for network ‘playground’.: The command ‘cd “/home/user/hello_world” && CANISTER_CANDID_PATH=“/home/user/hello_world/.dfx/playground/canisters/hello_world_frontend/assetstorage.did” CANISTER_CANDID_PATH_HELLO_WORLD_BACKEND=“/home/user/hello_world/.dfx/playground/canisters/hello_world_backend/hello_world_backend.did” CANISTER_CANDID_PATH_hello_world_backend=“/home/user/hello_world/.dfx/playground/canisters/hello_world_backend/hello_world_backend.did” CANISTER_ID=“y4oop-liaaa-aaaab-qacha-cai” CANISTER_ID_HELLO_WORLD_BACKEND=“ofoea-eyaaa-aaaab-qab6a-cai” CANISTER_ID_HELLO_WORLD_FRONTEND=“y4oop-liaaa-aaaab-qacha-cai” CANISTER_ID_hello_world_backend=“ofoea-eyaaa-aaaab-qab6a-cai” CANISTER_ID_hello_world_frontend=“y4oop-liaaa-aaaab-qacha-cai” DFX_NETWORK=“playground” DFX_VERSION=“0.15.2” HELLO_WORLD_BACKEND_CANISTER_ID=“ofoea-eyaaa-aaaab-qab6a-cai” HELLO_WORLD_FRONTEND_CANISTER_ID=“y4oop-liaaa-aaaab-qacha-cai” “npm” “run” “build”’ failed with exit status ‘exit status: 2’.
Stdout:

hello_world_frontend@0.1.0 prebuild
npm run generate

hello_world_frontend@0.1.0 generate
dfx generate hello_world_backend

hello_world_frontend@0.1.0 build
webpack

No production canister_ids.json found. Continuing with local

Stderr:
Building canisters before generate for Motoko
Generating type declarations for canister hello_world_backend:
src/declarations/hello_world_backend/hello_world_backend.did.d.ts
src/declarations/hello_world_backend/hello_world_backend.did.js
src/declarations/hello_world_backend/hello_world_backend.did
[webpack-cli] Failed to load ‘/home/user/hello_world/webpack.config.js’ config
[webpack-cli] TypeError: Cannot convert undefined or null to object
at Function.entries ()
at initCanisterEnv (/home/user/hello_world/webpack.config.js:30:17)
at Object. (/home/user/hello_world/webpack.config.js:37:30)
at Module._compile (node:internal/modules/cjs/loader:1376:14)
at Module._extensions…js (node:internal/modules/cjs/loader:1435:10)
at Module.load (node:internal/modules/cjs/loader:1207:32)
at Module._load (node:internal/modules/cjs/loader:1023:12)
at Module.require (node:internal/modules/cjs/loader:1235:19)
at require (node:internal/modules/helpers:176:18)
at WebpackCLI.tryRequireThenImport (/home/user/hello_world/node_modules/webpack-cli/lib/webpack-cli.js:204:22)

Next, I replaced this piece of original code (webpack.config) :

function initCanisterEnv() {
let localCanisters, prodCanisters;
try {
localCanisters = require(path.resolve(
“.dfx”,
“local”,
“canister_ids.json”
));
} catch (error) {
console.log(“No local canister_ids.json found. Continuing production”);
}
try {
prodCanisters = require(path.resolve(“canister_ids.json”));
} catch (error) {
console.log(“No production canister_ids.json found. Continuing with local”);
}

const network =
process.env.DFX_NETWORK ||
(process.env.NODE_ENV === “production” ? “ic” : “local”);

const canisterConfig = network === “local” ? localCanisters : prodCanisters;

return Object.entries(canisterConfig).reduce((prev, current) => {
const [canisterName, canisterDetails] = current;
prev[canisterName.toUpperCase() + “_CANISTER_ID”] =
canisterDetails[network];
return prev;
}, {});
}
const canisterEnvVariables = initCanisterEnv();

…replaced it with this. And it worked.

function initCanisterEnv() {
let localCanisters, prodCanisters;

try {
localCanisters = require(path.resolve(“.dfx”, “local”, “canister_ids.json”));
} catch (error) {
console.log(“No local canister_ids.json found. Continuing production”);
}

try {
prodCanisters = require(path.resolve(“canister_ids.json”));
} catch (error) {
console.log(“No production canister_ids.json found. Continuing with local”);
}

const network =
process.env.DFX_NETWORK ||
(process.env.NODE_ENV === “production” ? “ic” : “local”);

const canisterConfig = network === “local” ? localCanisters : prodCanisters;

// Check if canisterConfig is defined before using Object.entries
if (canisterConfig) {
return Object.entries(canisterConfig).reduce((prev, current) => {
const [canisterName, canisterDetails] = current;
prev[canisterName.toUpperCase() + “_CANISTER_ID”] =
canisterDetails[network];
return prev;
}, {});
}

return {};
}

const canisterEnvVariables = initCanisterEnv();

1 Like