javascript – Webpack:ERROR in ./~/sqlite3/~/node-pre-gyp/lib/node-pre-gyp.js找不到模块:错误:无法解析’file’或’directory’../package

我想在我的项目中使用Webpack,但是当我运行时

npm run dev

,我收到这个错误.

ERROR in ./~/sqlite3/~/node-pre-gyp/lib/node-pre-gyp.js Module not
found: Error: Cannot resolve ‘file’ or ‘directory’ ../package in
/Users/caizongming/Flowerhop/-DBLab-Alarm-Project-/Server/node_modules/sqlite3/node_modules/node-pre-gyp/lib
@ ./~/sqlite3/~/node-pre-gyp/lib/node-pre-gyp.js 60:16-37

ERROR in ./~/sqlite3/~/node-pre-gyp/lib/info.js Module not found:
Error: Cannot resolve module ‘aws-sdk’ in
/Users/caizongming/Flowerhop/-DBLab-Alarm-Project-/Server/node_modules/sqlite3/node_modules/node-pre-gyp/lib
@ ./~/sqlite3/~/node-pre-gyp/lib/info.js 14:14-32

ERROR in ./~/sqlite3/~/node-pre-gyp/lib/publish.js Module not found:
Error: Cannot resolve module ‘aws-sdk’ in
/Users/caizongming/Flowerhop/-DBLab-Alarm-Project-/Server/node_modules/sqlite3/node_modules/node-pre-gyp/lib
@ ./~/sqlite3/~/node-pre-gyp/lib/publish.js 17:14-32

ERROR in ./~/sqlite3/~/node-pre-gyp/lib/unpublish.js Module not found:
Error: Cannot resolve module ‘aws-sdk’ in
/Users/caizongming/Flowerhop/-DBLab-Alarm-Project-/Server/node_modules/sqlite3/node_modules/node-pre-gyp/lib
@ ./~/sqlite3/~/node-pre-gyp/lib/unpublish.js 15:14-32

ERROR in ./~/sqlite3/~/rc/index.js Module build failed: Error: Parse
Error: Line 1: Unexpected token ILLEGAL
at throwError (/Users/caizongming/Flowerhop/-DBLab-Alarm-Project-/Server/node_modules/jsx-loader/node_modules/jstransform/node_modules/esprima-fb/esprima.js:2823:21)
at scanPunctuator (/Users/caizongming/Flowerhop/-DBLab-Alarm-Project-/Server/node_modules/jsx-loader/node_modules/jstransform/node_modules/esprima-fb/esprima.js:1011:9)
at advance (/Users/caizongming/Flowerhop/-DBLab-Alarm-Project-/Server/node_modules/jsx-loader/node_modules/jstransform/node_modules/esprima-fb/esprima.js:1747:16)
at peek (/Users/caizongming/Flowerhop/-DBLab-Alarm-Project-/Server/node_modules/jsx-loader/node_modules/jstransform/node_modules/esprima-fb/esprima.js:1773:21)
at parseProgram (/Users/caizongming/Flowerhop/-DBLab-Alarm-Project-/Server/node_modules/jsx-loader/node_modules/jstransform/node_modules/esprima-fb/esprima.js:6535:9)
at Object.parse (/Users/caizongming/Flowerhop/-DBLab-Alarm-Project-/Server/node_modules/jsx-loader/node_modules/jstransform/node_modules/esprima-fb/esprima.js:7713:23)
at getAstForSource (/Users/caizongming/Flowerhop/-DBLab-Alarm-Project-/Server/node_modules/jsx-loader/node_modules/jstransform/src/jstransform.js:244:21)
at Object.transform (/Users/caizongming/Flowerhop/-DBLab-Alarm-Project-/Server/node_modules/jsx-loader/node_modules/jstransform/src/jstransform.js:267:11)
at Object.transform (/Users/caizongming/Flowerhop/-DBLab-Alarm-Project-/Server/node_modules/jsx-loader/node_modules/jstransform/src/simple.js:105:28)
at Object.module.exports (/Users/caizongming/Flowerhop/-DBLab-Alarm-Project-/Server/node_modules/jsx-loader/index.js:15:31)
@ ./~/sqlite3/~/node-pre-gyp/lib/info.js 11:13-26

这是我的WEBPACK.CONFIG.js

var path = require ('path');

module.exports = {
  entry: './server.js',
  output: {
    filename: 'bundle.js'
  },
  module: {
    loaders:[
      { test: /\.css$/, loader: "style!css" },
      { test: /\.js$/, loader: 'jsx-loader?harmony' },
      { test: /\.json$/, loader: 'json-loader' }
    ]
  },
  resolve: {
    fallback: path.join(__dirname, "node_modules"),
    extensions : ['', '.js', '.jsx']
  },
  resolveLoader: { fallback: path.join(__dirname, "node_modules") },
  target: 'node'
};

这是package.json.

{
  "name": "biocenter",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "directories": {
    "test": "test"
  },
  "dependencies": {
    "body-parser": "^1.15.0",
    "express": "^4.13.4",
    "request": "^2.72.0",
    "sqlite3": "^3.1.4",
    "querystring": "^0.2.0",
    "should": "^8.3.2"
  },
  "devDependencies": {
    "brfs": "^1.4.3",
    "json-loader": "^0.5.4",
    "mocha": "^2.4.5",
    "transform-loader": "^0.2.3",
    "webpack": "^1.13.1"
  },
  "scripts": {
    "test": "mocha",
    "start": "node server.js",
    "dev": "webpack-dev-server --devtool eval --progress --colors",
    "deploy": "NODE_ENV=production webpack -p"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/FlowerHop/-DBLab-Alarm-Project-.git"
  },
  "author": "Flowerhop",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/FlowerHop/-DBLab-Alarm-Project-/issues"
  },
  "homepage": "https://github.com/FlowerHop/-DBLab-Alarm-Project-#readme"
}

任何人都可以帮我解决这个问题吗?

最佳答案 我遇到了与lzma-native类似的问题,它也使用了node-pre-gyp.问题是它们都直接需要模块中的节点pre-gyp.我尝试替换使用node-pre-gyp的代码行

var nodePreGyp = require('node-pre-gyp');
var path = require('path');
var binding_path = nodePreGyp.find(path.resolve(path.join(__dirname,'./package.json')));
var native = require(binding_path);

var native = require('pre-gyp-find')('lzma_native');
// notice that the parameter here is the binary module name in package.json of lzma-native

这最终对我有用.虽然,这个解决方案要求我们修改源代码,并添加一个额外的依赖’pre-gyp-find’.也许你可以向作者发送关于此的PR.

这不是最好的解决方案,因为它实际上并没有解决,而是避免了问题.

点赞