Nodejs Cannot find module '../build/Release/canvas'
Asked Answered
W

10

40

I installed cairo, and node-canvas. I tried everything, but still can't find module.

sudo apt-get install libcairo2-dev
sudo npm install canvas
sudo npm install canvas -g

If I run require('canvas'), I get this error:

Error: Cannot find module '../build/Release/canvas'
    at Function._resolveFilename (module.js:332:11)
    at Function._load (module.js:279:25)
    at Module.require (module.js:354:17)
    at require (module.js:370:17)
    at Object.<anonymous> (/home/tomas/node_modules/canvas/lib/bindings.js:2:18)
    at Module._compile (module.js:441:26)
    at Object..js (module.js:459:10)
    at Module.load (module.js:348:32)
    at Function._load (module.js:308:12)
    at Module.require (module.js:354:17)

I use Ubuntu linux

Thanks in advance.

Weismannism answered 8/2, 2013 at 11:42 Comment(6)
Check if the module is in your node_module directory and if it is, try to require the full path like require('./node_modules/canvas');Bodkin
You got the module in that directory ? Check github.com/LearnBoost/node-canvas/issues/137Bodkin
yes. test.js is in the same directory as the node_modules, and canvas directory is inside the node_modulesWeismannism
canvas module is the worst of all, never had this many problems with anything in my life...been trying to do this for like 3 days...it's all over the place. putting direct path from c:... worked though..Crinkle
For me I did npm clean-install and it worked.Cruel
it seems like a directory issueHarmonica
B
27

I spent a lot of time to resolve this issue ...

Use npm uninstall canvas and then install using npm i canvas.

Bangweulu answered 30/7, 2020 at 14:55 Comment(4)
I was so skeptical this would work, but I thought I must try it. -- Solved my problem! thanks!Boccie
npm install canvas@latest works tooHearken
Is there a way to solve this from root? I'm constantly npm installing in that folder and it gets annoying after some time.Ganja
@Santiago you can try rm -rf node_modules and npm cache clean –force . Then do npm install.Bangweulu
I
11

It seemed like the install script of canvas haven't been called in my case, too. Instead of deleting the lock file I just called the script manually which solved the issue in my case (at least temporarily).

Therefore, if the canvas package has been installed already, go to /node_modules/canvas/ and run npm run install which creates the build directory.

Intracellular answered 4/11, 2021 at 11:40 Comment(0)
L
8

I had the same problem. The issue was that the install script for node-canvas never got executed, which is why the build folder was missing.

In my case, removing the yarn.lock file was the solution, as it skipped the node-canvas package installation for some reason. Once I let npm take care of the packages, it successfully installed node-canvas, ran the install script and the build folder showed up again.

Ligature answered 14/2, 2017 at 13:16 Comment(1)
I had a similar problem, I had disabled install-scripts. Re-enabling them resolved my issue.Guttate
O
2

Problem here looks like 1. Your node modules are installed in a different location But the node.js runtime is searching for it in a different place. Check the NODE_PATH and see if this is the case.

Refer the module loading explained in http://nodejs.org/api/modules.html#modules_loading_from_the_global_folders

Odessaodetta answered 6/3, 2014 at 11:14 Comment(0)
D
1

You can try this:

  • Delete node_modules/ folder
  • Run dependency installations (npm install)
  • Use a different Node version
Discordancy answered 13/1, 2023 at 21:6 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Hypersonic
K
0

Same problem in my nextjs application. In my case. I installed react-pdf package then this problem happened.

My resolve: Just Notify webapck to ignore the canvas module

Add the following webpack options in next.config.js

const nextConfig = {
  webpack(config){
    config.externals.push('canvas')
    return config
  }
};

Kenelm answered 6/7, 2023 at 4:46 Comment(0)
Q
0

In my case:

rm -rf node_modules
rm package-lock.json
npm install

:D

Quillon answered 21/8, 2023 at 10:29 Comment(1)
Easiest solution.Harr
S
0

If you use Next.js or Next.js 14, you may need to add the following to your next.config.js:

module.exports = {
 webpack: (config) => {
   config.resolve.alias.canvas = false;

   return config;
 },
}

ref: https://www.npmjs.com/package/react-pdf

this is work for me

Satisfactory answered 19/1 at 7:7 Comment(0)
K
-1

I had a similar problem but I used canvas-prebuilt which is a drop-in replacement for canvas and I fixed it for doing this: npm install -g node-gyp and then node-gyp rebuild in the terminal

Keyboard answered 26/2, 2020 at 0:8 Comment(0)
A
-2
sudo apt-get install libpixman-1-dev libcairo2-dev libpangocairo-1.0-0 libpango1.0-dev libgif-dev  libjpeg-dev

If use ubuntu,try install above lib then install canvas.work fine for me。

If you use cnpm install the canvas. try npm.

Amadavat answered 14/2, 2017 at 3:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.