Cannot load Node native addons with webpack

限于喜欢 提交于 2021-01-28 11:41:27

问题


Although I am using vue-cli in the example code to generate a webpack config, nothing is specific to vue.

I create the example app like this:

vue init webpack webpack_modules_example

Generated webpack.base.conf:

'use strict'
const path = require('path')
const utils = require('./utils')
const config = require('../config')
const vueLoaderConfig = require('./vue-loader.conf')

function resolve (dir) {
  return path.join(__dirname, '..', dir)
}

module.exports = {
  context: path.resolve(__dirname, '../'),
  entry: {
    app: './src/main.js'
  },
  output: {
    path: config.build.assetsRoot,
    filename: '[name].js',
    publicPath: process.env.NODE_ENV === 'production'
      ? config.build.assetsPublicPath
      : config.dev.assetsPublicPath
  },
  resolve: {
    extensions: ['.js', '.vue', '.json'],
    alias: {
      'vue$': 'vue/dist/vue.esm.js',
      'ad-block': 'ad-block/build/Release/ad-block.node',
      '@': resolve('src'),
    }
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: vueLoaderConfig
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')]
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('img/[name].[hash:7].[ext]')
        }
      },
      {
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('media/[name].[hash:7].[ext]')
        }
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
        }
      },
    ]
  },
  node: {
    setImmediate: false,
    dgram: 'empty',
    fs: 'empty',
    net: 'empty',
    tls: 'empty',
    child_process: 'empty'
  }
}

I want to include this library, so I do this:

npm install --save ad-block

And on the code (App.vue) I add this:

<script>
...
  const {AdBlockClient, FilterOptions} = require('ad-block')
...

Because it's a native module, I need to install some loader for webpack (tried several):

npm install native-ext-loader --save-dev

Add the loader to the webpack config:

  {
    test: /\.node$/,
    loader: "native-ext-loader"
  },

And create an alias in the webpack config too:

alias: {
      ...
      'ad-block': 'ad-block/build/Release/ad-block.node',
      ...
    }

But when I run npm run dev and go to http://localhost:8080/ I see this error in the console:

Uncaught Error: Cannot open /ad-block.node: TypeError: Cannot read property 'dlopen' of undefined

at Object.eval (ad-block.node?9538:1)

at eval (ad-block.node:2)

at Object../node_modules/ad-block/build/Release/ad-block.node (app.js:733)

at webpack_require (app.js:679)

at fn (app.js:89)

at eval (App.vue?26cd:9)

at Object../node_modules/babel-loader/lib/index.js!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/App.vue (app.js:757)

at webpack_require (app.js:679)

at fn (app.js:89)

at eval (App.vue?a8e9:1)

If I use this without webpack, it works. Not sure what am I missing!


回答1:


NodeJS is a server-side programming environment and as such, supports “native” add-on modules compiled to run on the same host that runs your Node service.

While Node is programmed using JavaScript, it is not browser JavaScript and so native ad-ons cannot be expected to run in the browser.

See Node C++ Addons for more information.



来源:https://stackoverflow.com/questions/52362984/cannot-load-node-native-addons-with-webpack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!