Skip to content

Module parse failed: /canvas/build/Release/canvas.node Unexpected character '�' #867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
playground opened this issue Jan 21, 2017 · 15 comments

Comments

@playground
Copy link

When build with webpack, it throws these errors.
I'm using fabric.js 1.7.3 which has a dependencies on canvas 1.6.2

SyntaxError: Unexpected character '�' (1:0)
at Parser.pp$4.raise (/test1/node/ow-dig/node_modules/webpack/node_modules/acorn/dist/acorn.js:2221:15)
at Parser.pp$7.getTokenFromCode (/test1/node/ow-dig/node_modules/webpack/node_modules/acorn/dist/acorn.js:2756:10)
at Parser.pp$7.readToken (/test1/node/ow-dig/node_modules/webpack/node_modules/acorn/dist/acorn.js:2477:17)
at Parser.pp$7.nextToken (/test1/node/ow-dig/node_modules/webpack/node_modules/acorn/dist/acorn.js:2468:15)
at Parser.parse (/test1/node/ow-dig/node_modules/webpack/node_modules/acorn/dist/acorn.js:515:10)
at Object.parse (/test1/node/ow-dig/node_modules/webpack/node_modules/acorn/dist/acorn.js:3098:39)
at Parser.parse (/test1/node/ow-dig/node_modules/webpack/lib/Parser.js:902:15)
at DependenciesBlock. (/test1/node/ow-dig/node_modules/webpack/lib/NormalModule.js:104:16)
at DependenciesBlock.onModuleBuild (/test1/node/ow-dig/node_modules/webpack-core/lib/NormalModuleMixin.js:310:10)
at nextLoader (/test1/node/ow-dig/node_modules/webpack-core/lib/NormalModuleMixin.js:275:25)
at /test1/node/ow-dig/node_modules/webpack-core/lib/NormalModuleMixin.js:259:5
at Storage.finished (/test1/node/ow-dig/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:38:16)
at /test1/node/ow-dig/node_modules/graceful-fs/graceful-fs.js:78:16
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:439:3)
@ ./~/canvas/lib/bindings.js 3:17-51

@playground playground changed the title Module parse failed: /test1/node_modules/canvas/build/Release/canvas.node Unexpected character '�' Module parse failed: /canvas/build/Release/canvas.node Unexpected character '�' Jan 21, 2017
@LinusU
Copy link
Collaborator

LinusU commented Jan 21, 2017

This is because canvas.node is a natively compiled module, and isn't a javascript file. I don't see how this will be able to work with webpack...

Are you intending to run the resulting bundle with Node.js or in the browser?

@playground
Copy link
Author

playground commented Jan 21, 2017

I intend to run the bundle with Node.js. @LinusU do you have a way to make it work?

@zbjornson
Copy link
Collaborator

@playground I hadn't heard of using webpack for node.js until now, but... I think the only way this could work is if you use the externals option to stop webpack from bundling canvas since it cannot bundle native code. There's also this plugin for webpack: https://github.com/webpack-contrib/node-loader

@jesantana
Copy link

For me providing the canvas as an external empty object worked:

externals : { canvas: {} }

In my case I am using webpack with karma to run unit test. Since canvas is not part of any test mocking it with an empty object did the trick.

@sayjeyhi
Copy link

sayjeyhi commented Sep 4, 2019

I have this issue on gatsby SSR app, below code is not the correct way and will damage you canvas

externals : { canvas: {} }

@tobiasoberrauch
Copy link

tobiasoberrauch commented Feb 3, 2024

I fixed it in this way with next 14.0.4

import './src/env.mjs';
/** @type {import('next').NextConfig} */

const nextConfig = {
  webpack: (
    config,
    { buildId, dev, isServer, defaultLoaders, nextRuntime, webpack }
  ) => {
    config.externals.push({ canvas: 'commonjs canvas' })
    return config
  },
};

export default nextConfig;

@kwb020312
Copy link

I fixed it in this way with next 14.0.4

import './src/env.mjs';
/** @type {import('next').NextConfig} */

const nextConfig = {
  webpack: (
    config,
    { buildId, dev, isServer, defaultLoaders, nextRuntime, webpack }
  ) => {
    config.externals.push({ canvas: 'commonjs canvas' })
    return config
  },
};

export default nextConfig;

You're My Hero

@vladdg-dev
Copy link

I fixed it in this way with next 14.0.4

import './src/env.mjs';
/** @type {import('next').NextConfig} */

const nextConfig = {
  webpack: (
    config,
    { buildId, dev, isServer, defaultLoaders, nextRuntime, webpack }
  ) => {
    config.externals.push({ canvas: 'commonjs canvas' })
    return config
  },
};

export default nextConfig;

This works! Thanks!

@ghost
Copy link

ghost commented Mar 5, 2024

how to fix it in nuxtjs, thanks everyone

Bentroen added a commit to OpenNBS/NoteBlockWorld that referenced this issue Mar 31, 2024
Adds a module factory to use the Canvas API when in the browser environment and node-canvas when in Node.js.

Next.js webpack config necessary to load node-canvas properly. See: Automattic/node-canvas#867 (comment)
@vijaysolankii
Copy link

Hi Community

I am currently stuck in canvas error in next js 14.1.3 and using the "fabric": "^5.3.0",

`⨯ ./node_modules/canvas/build/Release/canvas.node
Module parse failed: Unexpected character '�' (1:2)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)

Import trace for requested module:
./node_modules/canvas/build/Release/canvas.node
./node_modules/canvas/lib/bindings.js
./node_modules/canvas/index.js
./node_modules/jsdom/lib/jsdom/utils.js
./node_modules/fabric/dist/fabric.js
./lib/canvas.ts
./app/page.tsx`

can anyone help me in this
Thank

@vijaysolankii
Copy link

Hi Community

I am currently stuck in canvas error in next js 14.1.3 and using the "fabric": "^5.3.0",

`⨯ ./node_modules/canvas/build/Release/canvas.node Module parse failed: Unexpected character '�' (1:2) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders (Source code omitted for this binary file)

Import trace for requested module: ./node_modules/canvas/build/Release/canvas.node ./node_modules/canvas/lib/bindings.js ./node_modules/canvas/index.js ./node_modules/jsdom/lib/jsdom/utils.js ./node_modules/fabric/dist/fabric.js ./lib/canvas.ts ./app/page.tsx`

can anyone help me in this Thank

I've updated next config file but error still persist
image

@jaronfort
Copy link

https://dev.to/ziqinyeow/step-by-step-on-how-to-setup-fabricjs-in-the-nextjs-app-3hi3

I followed the steps here.
I found this blog post. I also added use client to parts of my Next.js application to get the bug to go away.

My next.config.js file looks something like this as suggested in the article.

/** @type {import('next').NextConfig} */
module.exports = {
	webpack: (config) => {
		config.externals.push({
			'utf-8-validate': 'commonjs utf-8-validate',
			bufferutil: 'commonjs bufferutil',
			canvas: 'commonjs canvas',
		});
		// config.infrastructureLogging = { debug: /PackFileCache/ };
		return config;
	},
};

@XiiiRuth
Copy link

how to fix it in nuxtjs, thanks everyone
add in nuxt.config.js

build: {
extend(config) {
config.externals = {
'canvas': 'commonjs canvas'
};
}
}

@oreographer
Copy link

I fixed it in this way with next 14.0.4

import './src/env.mjs';
/** @type {import('next').NextConfig} */

const nextConfig = {
  webpack: (
    config,
    { buildId, dev, isServer, defaultLoaders, nextRuntime, webpack }
  ) => {
    config.externals.push({ canvas: 'commonjs canvas' })
    return config
  },
};

export default nextConfig;

It works! Thankyou ❤

@pavithra-muniraju
Copy link

i have the same issue with angular, ng2-pdf-viewer used canvas internally and while building the project i'm getting this issue
can someone help here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests