NodeJS操作方法需要注入函数的模块

时间:2016-02-17 10:34:12

标签: javascript node.js d3.js webpack

我正在使用webpack捆绑使用d3和d3.slider模块构建的可视化,但这是一个更普遍的问题 - 我如何需要()一个模块,该模块使用函数修补模块(此处为d3) (这里是d3.slider())?

的package.json:

{
  "name": "d3_slider_error",
  "version": "0.0.1",
  "description": "",
  "main": "",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack-dev-server"
  },
  "dependencies": {
    "d3": "^3.5.15",
    "d3-slider": "git+ssh://git@github.com:MasterMaps/d3-slider.git",
    "webpack": "^1.9.10",
    "webpack-dev-server": "^1.9.0",
    "html-webpack-plugin": "^1.5.0"
  }
}

webpack.config.js:

var HtmlWebpackPlugin = require('html-webpack-plugin');
var path = require('path');

module.exports = {
    entry: "./src/main.js", // APP_PATH
    output: {
        filename: 'main.js',
        path: path.resolve('./dist') // BUILD_PATH  
    },
    module: {
        loaders: [
            //
        ]
    },
    resolve: {
        modulesDirectories: ['node_modules']
    },
    plugins: [
        new HtmlWebpackPlugin({title : "d3-slider-error"})
    ]
};

的src / main.js:

var d3 = require('d3');
require('d3.slider');

var timeSlider = d3.slider();

console.log("Done!");

此日志:TypeError:d3.slider不是函数

1 个答案:

答案 0 :(得分:0)

我找到了一个丑陋的解决方案,但由于没有其他问题,我在这里发布。我将此添加到package.json依赖项:

"script-loader": "git+ssh://git@github.com:webpack/script-loader.git"

然后在某处,例如在入口点(假设d3.slider在node_modules中):

require("script!d3.slider");

Script-loader在全局环境中执行一次脚本。应该适用于所有类似的问题,遗留脚本等。完成。