Skip to content

webpack issue with canvas #942

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
wylde3161 opened this issue Jul 17, 2017 · 4 comments
Closed

webpack issue with canvas #942

wylde3161 opened this issue Jul 17, 2017 · 4 comments

Comments

@wylde3161
Copy link

did a project in NodeJS, when i pack it using webpack some issues arose and the end result is that the canvas object is not defined and emits an error.

webpack.config.js

var webpack = require('webpack');
module.exports = {
    context: __dirname,
    entry: ["./index.js"],
    output: {
        library: "ThumbS",
        path: __dirname,
        filename: "davidService.js"
    },
    module:{
        loaders: [
            {
                test: /\.js$/,
                exclude: /(node_modules)/,
                loader: 'babel-loader',
                query: {
                    presets: ['es2015']
                }
            },
            {
                test: /\.json$/,
                loader: 'json-loader'
            }
        ]
    },
    resolve: {
        extensions: ['.js', '.jsx']
    },
    node: {
        console: false,
        fs: 'empty',
        net: 'empty',
        tls: 'empty'
    },
    externals:{
         canvas: {
             commonjs: "canvas",
             commonjs2: "canvas",
             amd: "canvas",
             root: "_"
         }
    }
};

index.html

<script type="text/javascript" src="davidService.js"></script>
var ts = new ThumbS();

Your Environment

  • Version of node-canvas 1.6.5
  • Environment node 6.11.0 on MacBook Pro macOS Sierra 10.12.5:
@ghost
Copy link

ghost commented Oct 19, 2017

I solved this by using node-loader: https://webpack.js.org/loaders/node-loader/

  1. Install node-loader as dev dependency.
  2. Add rule in webpack.config.js as follows
      {
        test: /\.node$/,
        use: 'node-loader'
      }

@LinusU
Copy link
Collaborator

LinusU commented Oct 19, 2017

@wylde3161 are you trying to create a bundle that will use the browser Canvas? In that case, good news, that should be supported in the 2.x alphas! Could you please try with that one? 😄

@zbjornson
Copy link
Collaborator

Per above ... this is fixed in 2.x!

@tonyxyz
Copy link

tonyxyz commented Jul 10, 2018

webpack-node-externals solves this nicely. Install as a dev dependency and then at a suitable point in your webpack config:

const nodeExternals = require('webpack-node-externals');

module.exports = {
  entry: './src/my_entry_point.js',
  target: 'node',
  externals: [nodeExternals()],
}

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

No branches or pull requests

4 participants