Webpack,(npm)绑定,并且要求(`fs`)不能一起工作

目前正在使用一个用于通过odbc INSERT语句向SQL数据库提交用户信息的SPA,但是我的本地环境没有正确构build,我无法弄清楚为什么。

链接到npm的绑定: https ://www.npmjs.com/package/bindings

macOS version 10.12.6 node -v v8.8.1 webpack -v 3.8.1 

截至目前,我可以使用node post.js成功地发布到我的数据库。 但是,当我运行grunt build我收到以下错误:

 WARNING in ./node_modules/bindings/bindings.js 81:22-40 Critical dependency: the request of a dependency is an expression WARNING in ./node_modules/bindings/bindings.js 81:43-53 Critical dependency: the request of a dependency is an expression ERROR in ./node_modules/bindings/bindings.js Module not found: Error: Can't resolve 'fs' in '/Users/pat/to/project/node_modules/bindings' @ ./node_modules/bindings/bindings.js 6:11-24 @ ./node_modules/odbc/lib/odbc.js @ ./assets/scripts/index.js @ ./index.js Warning: Use --force to continue. Aborted due to warnings. 

注意:每当我在任何文件中需要fs时,都会收到相同的问题(对于需要fs的文件,发生同样的错误)。 binding.js中对fs的要求是问题的最后一个实例,因为当我从binding.js中删除代码时,代码会中断。

所以当我看一下上面提到的binding.js文件时,我们可以看到以下内容( require(fs)是重要的一行):

 /** * Module dependencies. */ var fs = require('fs') , path = require('path') , join = path.join , dirname = path.dirname , exists = ((fs.accessSync && function (path) { try { fs.accessSync(path); } catch (e) { return false; } return true; }) || fs.existsSync || path.existsSync) , defaults = { arrow: process.env.NODE_BINDINGS_ARROW || ' → ' , compiled: process.env.NODE_BINDINGS_COMPILED_DIR || 'compiled' , platform: process.platform , arch: process.arch , version: process.versions.node , bindings: 'bindings.node' , try: [ // node-gyp's linked version in the "build" dir [ 'module_root', 'build', 'bindings' ] // node-waf and gyp_addon (aka node-gyp) , [ 'module_root', 'build', 'Debug', 'bindings' ] , [ 'module_root', 'build', 'Release', 'bindings' ] // Debug files, for development (legacy behavior, remove for node v0.9) , [ 'module_root', 'out', 'Debug', 'bindings' ] , [ 'module_root', 'Debug', 'bindings' ] // Release files, but manually compiled (legacy behavior, remove for node v0.9) , [ 'module_root', 'out', 'Release', 'bindings' ] , [ 'module_root', 'Release', 'bindings' ] // Legacy from node-waf, node <= 0.4.x , [ 'module_root', 'build', 'default', 'bindings' ] // Production "Release" buildtype binary (meh...) , [ 'module_root', 'compiled', 'version', 'platform', 'arch', 'bindings' ] ] } /** * The main `bindings()` function loads the compiled bindings for a given module. * It uses V8's Error API to determine the parent filename that this function is * being invoked from, which is then used to find the root directory. */ function bindings (opts) { // Argument surgery if (typeof opts == 'string') { opts = { bindings: opts } } else if (!opts) { opts = {} } // maps `defaults` onto `opts` object Object.keys(defaults).map(function(i) { if (!(i in opts)) opts[i] = defaults[i]; }); // Get the module root if (!opts.module_root) { opts.module_root = exports.getRoot(exports.getFileName()) } // Ensure the given bindings name ends with .node if (path.extname(opts.bindings) != '.node') { opts.bindings += '.node' } var tries = [] , i = 0 , l = opts.try.length , n , b , err for (; i<l; i++) { n = join.apply(null, opts.try[i].map(function (p) { return opts[p] || p })) tries.push(n) try { b = opts.path ? require.resolve(n) : require(n) if (!opts.path) { b.path = n } return b } catch (e) { if (!/not find/i.test(e.message)) { throw e } } } err = new Error('Could not locate the bindings file. Tried:\n' + tries.map(function (a) { return opts.arrow + a }).join('\n')) err.tries = tries throw err } module.exports = exports = bindings /** * Gets the filename of the JavaScript file that invokes this function. * Used to help find the root directory of a module. * Optionally accepts an filename argument to skip when searching for the invoking filename */ exports.getFileName = function getFileName (calling_file) { var origPST = Error.prepareStackTrace , origSTL = Error.stackTraceLimit , dummy = {} , fileName Error.stackTraceLimit = 10 Error.prepareStackTrace = function (e, st) { for (var i=0, l=st.length; i<l; i++) { fileName = st[i].getFileName() if (fileName !== __filename) { if (calling_file) { if (fileName !== calling_file) { return } } else { return } } } } // run the 'prepareStackTrace' function above Error.captureStackTrace(dummy) dummy.stack // cleanup Error.prepareStackTrace = origPST Error.stackTraceLimit = origSTL return fileName } /** * Gets the root directory of a module, given an arbitrary filename * somewhere in the module tree. The "root directory" is the directory * containing the `package.json` file. * * In: /home/nate/node-native-module/lib/index.js * Out: /home/nate/node-native-module */ exports.getRoot = function getRoot (file) { var dir = dirname(file) , prev while (true) { if (dir === '.') { // Avoids an infinite loop in rare cases, like the REPL dir = process.cwd() } if (exists(join(dir, 'package.json')) || exists(join(dir, 'node_modules'))) { // Found the 'package.json' file or 'node_modules' dir; we're done return dir } if (prev === dir) { // Got to the top throw new Error('Could not find module root given file: "' + file + '". Do you have a `package.json` file? ') } // Try the parent dir next prev = dir dir = join(dir, '..') } } 

所以看起来在代码块的顶部需要fs是造成这个问题的原因。 如果我删除fs require并再次运行grunt生成,我得到以下警告但没有错误:

 WARNING in ./node_modules/bindings/bindings.js 81:22-40 Critical dependency: the request of a dependency is an expression WARNING in ./node_modules/bindings/bindings.js 81:43-53 Critical dependency: the request of a dependency is an expression 

运行“grunt serve”时收到相同的警告,但没有错误。 然后,当我访问本地主机上的页面,我收到以下错误:

 bindings.js:10 Uncaught ReferenceError: fs is not defined at Object.<anonymous> (bindings.js:10) at Object.Array.concat.splitPathRe (bindings.js:171) at __webpack_require__ (bootstrap 2037e18d07470b1345e0:54) at Object.<anonymous> (odbc.js:18) at Object.Array.concat.webpackEmptyContext.keys (odbc.js:820) at __webpack_require__ (bootstrap 2037e18d07470b1345e0:54) at Object.<anonymous> (index.js:7) at Object.<anonymous> (application.js:984) at __webpack_require__ (bootstrap 2037e18d07470b1345e0:54) at Object.<anonymous> (index.js:8) 

所以删除FS需要允许我build立,但然后binding.js不能正常工作,因为我删除了FS要求。 另外,在去除fs require(在binding.js中)之后,我不能使用节点向数据库发送消息,因为我在binding.js中删除了fs require(接收上面的错误消息)。

我已经尝试通过将以下代码添加到我的“grunt / webpack.js”文件中的debugging,如几个post中所build议的:

 target: 'node', node: { fs: 'empty' } 

链接发布的意见是从: https : //github.com/webpack-contrib/css-loader/issues/447

这里是我的整个'grunt / webpack.js'文件(想确保我把它放在正确的位置):

 'use strict' const webpack = require('webpack') const path = require('path') module.exports = { options: { entry: { application: './index.js', specs: './spec/_all.js', vendor: ['jquery', 'bootstrap-sass'] }, output: { filename: '[name].js', path: path.join(__dirname, '/../public'), publicPath: 'public/' }, plugins: [ new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', minChunks: Infinity }), new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery', 'window.jQuery': 'jquery' }) ], module: { rules: [ { test: /\.js$/, exclude: /(node_modules|bower_components)/, loader: 'babel-loader', query: { presets: ['es2015'] } }, { test: /\.css$/, use: [ { loader: 'style-loader' }, { loader: 'css-loader' } ] }, { test: /\.scss$/, use: [ { loader: 'style-loader' }, { loader: 'css-loader' }, { loader: 'sass-loader', options: { includePaths: [ path.resolve(__dirname, './node_modules') ] } } ] }, { test: /\.woff[\d]?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: 'url-loader?limit=10000&mimetype=application/font-woff' }, { test: /\.(ttf|eot|svg|png|jpg|gif)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: 'file-loader' }, { test: /\.(hbs|handlebars)$/, loader: 'handlebars-loader', query: { helperDirs: [ path.join(__dirname, '/../assets/scripts/templates/helpers') ] } } ] }, resolve: { alias: { handlebars: 'handlebars/dist/handlebars.js' } }, stats: { colors: true, modules: true, reasons: true } }, target: 'node', node: { fs: 'empty', console: 'empty', net: 'empty', tls: 'empty' }, build: { failOnError: true, watch: false, keepalive: false } } 

这里是我的package.json作为所有其他相关安装的参考:

 { "name": "omnipodeurope", "version": "0.0.3", "private": true, "license": { "software": "GNU GPLv3", "content": "CCBYNCSA 4.0" }, "dependencies": { "bindings": "^1.3.0", "fs": "0.0.1-security", "jquery": "^3.2.1", "odbc": "^1.3.0", "videojs": "^1.0.0" }, "devDependencies": { "babel-core": "^6.25.0", "babel-loader": "^7.1.1", "babel-preset-es2015": "^6.24.1", "bootstrap-sass": "^3.3.7", "chai": "^4.1.1", "clone": "^2.1.1", "css-loader": "^0.27.0", "eslint": "^4.4.1", "eslint-config-standard": "^10.2.1", "eslint-plugin-import": "^2.7.0", "eslint-plugin-node": "^5.1.1", "eslint-plugin-promise": "^3.5.0", "eslint-plugin-standard": "^3.0.1", "expose-loader": "^0.7.3", "file-loader": "^1.0.0", "grunt": "^1.0.1", "grunt-concurrent": "^2.3.1", "grunt-eslint": "^20.0.0", "grunt-jsonlint": "^1.1.0", "grunt-mocha-phantomjs": "^4.0.0", "grunt-nodemon": "^0.4.2", "grunt-open": "^0.2.3", "grunt-sass-lint": "^0.2.2", "grunt-shell": "^2.1.0", "grunt-webpack": "^3.0.2", "handlebars": "^4.0.10", "handlebars-loader": "^1.5.0", "html-loader": "^0.5.1", "load-grunt-config": "^0.19.2", "mocha": "^3.5.0", "node-libs-browser": "^2.0.0", "node-sass": "^4.5.3", "sass-loader": "^6.0.6", "style-loader": "^0.18.2", "time-grunt": "^1.4.0", "url-loader": "^0.5.9", "webpack": "^3.5.1", "webpack-dev-server": "^2.7.1" } } 

任何帮助,将不胜感激。 请让我知道,如果我应该张贴任何可能相关的额外信息。

谢谢大家!

更新:

我已经修改了binding.js,以便它的原始forms,并添加到我的grunt/webpack.js文件如上所述:

 target: 'node', node: { fs: 'empty' } 

现在,我在浏览器中看到了DOM中的以下内容:所以看起来webpack正在试图将fs设置为自己的variables,但由于某种原因,它找不到正确的模块。 什么

 var fs = __webpack_require__(!(function webpackMissingModule() { var e = new Error("Cannot find module \"fs\""); e.code = 'MODULE_NOT_FOUND'; throw e; }())) , path = __webpack_require__(20) 

什么会导致webpack无法findFS模块? 难道是因为我有多个与fs相关的软件包? (fs和node-fs);