一些细节:
npm
v3.10.3 nvm
v0.29.0 我最近构建了一个npm
包,它使用ffi
与共享C库进行交互。该包完全独立运行(使用babel
构建)。我试图将其集成到概念验证应用程序中,并且遇到bindings
的很多问题。我正在使用webpack
捆绑我的POC应用以及我遇到此问题的地方。
这是我的webpack.config.js
:
/* eslint-disable */
var webpack = require('webpack');
var path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var buildPath = path.resolve(__dirname, 'wp-build/');
module.exports = [
{
name: 'frontend',
entry: './src/frontend/index.js',
output: {
path: buildPath,
filename: 'frontend.bundle.js',
},
module: {
loaders: [{
test: /\.js/,
exclude: /(node_modules|build)/,
loader: 'babel-loader',
}],
},
plugins: [
new HtmlWebpackPlugin(),
],
devServer: {
contentBase: buildPath,
filename: 'frontend.bundle.js',
publicPath: '/assets/',
port: 8000,
stats: {
colors: true,
},
},
},
{
name: 'backend',
entry: './src/backend/server.js',
output: {
path: buildPath,
filename: 'backend.bundle.js',
},
module: {
loaders: [{
test: /\.js/,
exclude: /(node_modules|build|\.json|\.md)/,
loader: 'babel-loader',
}],
},
options: {
symlinks: true,
},
target: 'node',
},
];
frontend
任务从此Javascript(ES6)构建一个包:
console.log('This is the frontend calling!!');
简单吧?
backend
任务从此Javascript(ES6)构建一个包:
import Stack from 'stack-vod';
const stack = new Stack();
console.log(stack);
其中stack-vod
是我使用ffi
的包。
这里是POC应用的package.json
文件:
{
"name": "stack-as-package",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "./node_modules/.bin/webpack",
"dev": "./node_modules/.bin/webpack-dev-server"
},
"author": "Zachary Abresch <zachary.abresch@gmail.com>",
"license": "MIT",
"devDependencies": {
"babel-core": "^6.17.0",
"babel-eslint": "^7.0.0",
"babel-loader": "^6.2.5",
"babel-preset-es2015": "^6.16.0",
"babel-preset-stage-0": "^6.16.0",
"eslint": "^3.7.1",
"eslint-config-airbnb": "^12.0.0",
"eslint-plugin-import": "^1.16.0",
"eslint-plugin-jsx-a11y": "^2.2.3",
"eslint-plugin-react": "^6.4.1",
"html-webpack-plugin": "^2.22.0",
"webpack": "^1.13.2",
"webpack-dev-server": "^1.16.2"
},
"dependencies": {
"stack-vod": "[Private Gitlab Repository]"
}
}
以下是复制问题的步骤:
npm i
npm run build
node ./wp-build/backend.bundle.js
第1步工作正常。保存一些警告,安装npm
包时没有错误。
第2步显示此输出:
> stack-as-package@1.0.0 build /Users/zabresch/Documents/8x8/scratching/stack-as-package
> webpack
Hash: 513274db1b2b97352c7b3aed8d398daba0b6527f
Version: webpack 1.13.2
Child frontend:
Hash: 513274db1b2b97352c7b
Version: webpack 1.13.2
Time: 859ms
Asset Size Chunks Chunk Names
frontend.bundle.js 1.45 kB 0 [emitted] main
index.html 191 bytes [emitted]
+ 1 hidden modules
Child html-webpack-plugin for "index.html":
+ 3 hidden modules
Child backend:
Hash: 3aed8d398daba0b6527f
Version: webpack 1.13.2
Time: 949ms
Asset Size Chunks Chunk Names
backend.bundle.js 137 kB 0 [emitted] main
+ 40 hidden modules
WARNING in ./~/bindings/bindings.js
Critical dependencies:
76:22-40 the request of a dependency is an expression
76:43-53 the request of a dependency is an expression
@ ./~/bindings/bindings.js 76:22-40 76:43-53
WARNING in ./~/bindings/package.json
Module parse failed: /Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/bindings/package.json Unexpected token (2:9)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (2:9)
at Parser.pp$4.raise (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:2221:15)
at Parser.pp.unexpected (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:603:10)
at Parser.pp.semicolon (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:581:61)
at Parser.pp$1.parseExpressionStatement (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:966:10)
at Parser.pp$1.parseStatement (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:730:24)
at Parser.pp$1.parseBlock (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:981:25)
at Parser.pp$1.parseStatement (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:709:33)
at Parser.pp$1.parseTopLevel (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:638:25)
at Parser.parse (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:516:17)
at Object.parse (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:3098:39)
at Parser.parse (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/lib/Parser.js:902:15)
at DependenciesBlock.<anonymous> (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/lib/NormalModule.js:104:16)
at DependenciesBlock.onModuleBuild (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack-core/lib/NormalModuleMixin.js:310:10)
at nextLoader (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack-core/lib/NormalModuleMixin.js:275:25)
at /Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack-core/lib/NormalModuleMixin.js:259:5
at Storage.provide (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:52:20)
at CachedInputFileSystem.readFile (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:140:24)
at DependenciesBlock.onLoadPitchDone (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack-core/lib/NormalModuleMixin.js:255:7)
at DependenciesBlock.loadPitch (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack-core/lib/NormalModuleMixin.js:182:27)
at DependenciesBlock.doBuild (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack-core/lib/NormalModuleMixin.js:241:4)
at DependenciesBlock.build (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/lib/NormalModule.js:84:14)
at Compilation.buildModule (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/lib/Compilation.js:126:9)
at /Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/lib/Compilation.js:309:10
at /Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/lib/NormalModuleFactory.js:58:13
at NormalModuleFactory.applyPluginsAsyncWaterfall (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/tapable/lib/Tapable.js:75:69)
at onDoneResolving (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/lib/NormalModuleFactory.js:38:11)
at onDoneResolving (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/lib/NormalModuleFactory.js:121:6)
at /Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/lib/NormalModuleFactory.js:116:7
at /Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/async/lib/async.js:726:13
at /Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/async/lib/async.js:52:16
@ ./~/bindings ^\.\/.*$
WARNING in ./~/bindings/README.md
Module parse failed: /Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/bindings/README.md Unexpected token (2:3)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (2:3)
at Parser.pp$4.raise (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:2221:15)
at Parser.pp.unexpected (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:603:10)
at Parser.pp$3.parseExprAtom (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:1822:12)
at Parser.pp$3.parseExprSubscripts (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:1715:21)
at Parser.pp$3.parseMaybeUnary (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:1692:19)
at Parser.pp$3.parseExprOp (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:1656:43)
at Parser.pp$3.parseExprOp (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:1658:21)
at Parser.pp$3.parseExprOps (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:1639:17)
at Parser.pp$3.parseMaybeConditional (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:1620:21)
at Parser.pp$3.parseMaybeAssign (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:1597:21)
at Parser.pp$3.parseExpression (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:1573:21)
at Parser.pp$1.parseStatement (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:727:47)
at Parser.pp$1.parseTopLevel (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:638:25)
at Parser.parse (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:516:17)
at Object.parse (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/node_modules/acorn/dist/acorn.js:3098:39)
at Parser.parse (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/lib/Parser.js:902:15)
at DependenciesBlock.<anonymous> (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack/lib/NormalModule.js:104:16)
at DependenciesBlock.onModuleBuild (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack-core/lib/NormalModuleMixin.js:310:10)
at nextLoader (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack-core/lib/NormalModuleMixin.js:275:25)
at /Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/webpack-core/lib/NormalModuleMixin.js:259:5
at Storage.finished (/Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:38:16)
at /Users/zabresch/Documents/8x8/scratching/stack-as-package/node_modules/graceful-fs/graceful-fs.js:78:16
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:445:3)
@ ./~/bindings ^\.\/.*$
第3步以此错误响应:
/Users/zabresch/Documents/8x8/scratching/stack-as-package/wp-build/backend.bundle.js:2688
throw err
^
Error: Could not locate the bindings file. Tried:
→ /Users/zabresch/Documents/8x8/scratching/stack-as-package/build/binding.node
→ /Users/zabresch/Documents/8x8/scratching/stack-as-package/build/Debug/binding.node
→ /Users/zabresch/Documents/8x8/scratching/stack-as-package/build/Release/binding.node
→ /Users/zabresch/Documents/8x8/scratching/stack-as-package/out/Debug/binding.node
→ /Users/zabresch/Documents/8x8/scratching/stack-as-package/Debug/binding.node
→ /Users/zabresch/Documents/8x8/scratching/stack-as-package/out/Release/binding.node
→ /Users/zabresch/Documents/8x8/scratching/stack-as-package/Release/binding.node
→ /Users/zabresch/Documents/8x8/scratching/stack-as-package/build/default/binding.node
→ /Users/zabresch/Documents/8x8/scratching/stack-as-package/compiled/6.5.0/darwin/x64/binding.node
at bindings (/Users/zabresch/Documents/8x8/scratching/stack-as-package/wp-build/backend.bundle.js:2685:10)
at Object.<anonymous> (/Users/zabresch/Documents/8x8/scratching/stack-as-package/wp-build/backend.bundle.js:561:52)
at __webpack_require__ (/Users/zabresch/Documents/8x8/scratching/stack-as-package/wp-build/backend.bundle.js:20:30)
at Object.<anonymous> (/Users/zabresch/Documents/8x8/scratching/stack-as-package/wp-build/backend.bundle.js:466:12)
at __webpack_require__ (/Users/zabresch/Documents/8x8/scratching/stack-as-package/wp-build/backend.bundle.js:20:30)
at Object.<anonymous> (/Users/zabresch/Documents/8x8/scratching/stack-as-package/wp-build/backend.bundle.js:80:13)
at Object.<anonymous> (/Users/zabresch/Documents/8x8/scratching/stack-as-package/wp-build/backend.bundle.js:455:31)
at __webpack_require__ (/Users/zabresch/Documents/8x8/scratching/stack-as-package/wp-build/backend.bundle.js:20:30)
at Object.defineProperty.value (/Users/zabresch/Documents/8x8/scratching/stack-as-package/wp-build/backend.bundle.js:64:14)
at __webpack_require__ (/Users/zabresch/Documents/8x8/scratching/stack-as-package/wp-build/backend.bundle.js:20:30)
我已经尝试了 ton &#34;修复&#34;我在SO和GH上找到了但是 none 他们已经工作了。任何人都可以告诉我这里发生了什么以及可以采取哪些措施来解决这个问题?我已经连续几天抨击了我的头,两个队友上周花了几天试图解决这个问题。
BTW,webpack
是我的团队的要求,因为这最终将用于使用electron
的大型项目,并且webpack
已经被用于在那里构建。因此,像#34;不要使用webpack&#34;不会切掉芥末。 :)
答案 0 :(得分:1)
首先,您可能需要json和md文件加载器,执行npm i -S json-loader
和npm i -S file-loader
,然后在 webpack.config.js 中更新
"loaders": [{
test: /\.js/,
exclude: /(node_modules|build)/,
loader: 'babel-loader',
}, {
test: /\.md(\?v=\d+\.\d+\.\d+)?$/,
loader: "file"
}, {
test: /\.json$/,
loader: 'json-loader'
}]
如果这不能解决问题,请在执行此操作后检查 binding.node 是否存在,然后回复,我将更新我的回答。