typescript - 构建时输入ORM 'Module not found' (webpack)

标签 typescript webpack node-gyp typeorm

我在 google 上搜索了这个问题一段时间,在 typeorm github 上找到了几篇帖子,但它们都被关闭了,没有提供解决方案。这似乎是一个 webpack 问题,但我不明白问题到底是什么。该示例对我来说编译得很好。

当我调用从 typeorm 导入的 createConnection 时,就会发生这种情况。我得到的错误如下。我已经删掉了大部分“无法解决......”的内容,但你明白了。

WARNING in ./node_modules/typeorm/platform/PlatformTools.js
Module not found: Error: Can't resolve 'redis' in '/my/project/node_modules/typeorm/platform'
@ ./node_modules/typeorm/platform/PlatformTools.js
@ ./node_modules/typeorm/index.js
@ ./src/main.ts

WARNING in ./node_modules/typeorm/platform/PlatformTools.js
Module not found: Error: Can't resolve 'sql.js' in '/my/project/node_modules/typeorm/platform'
@ ./node_modules/typeorm/platform/PlatformTools.js
@ ./node_modules/typeorm/index.js
@ ./src/main.ts

WARNING in ./node_modules/node-pre-gyp/lib/util/nw-pre-gyp/index.html 1:0
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
> <!doctype html>
| <html>
| <head>
@ ./node_modules/node-pre-gyp/lib sync ^\.\/.*$ ./util/nw-pre-gyp/index.html
@ ./node_modules/node-pre-gyp/lib/node-pre-gyp.js
@ ./node_modules/sqlite3/lib/sqlite3.js
@ ./node_modules/typeorm/platform/PlatformTools.js
@ ./node_modules/typeorm/index.js
@ ./src/main.ts

WARNING in ./node_modules/node-gyp/lib/Find-VS2017.cs 7:6
Module parse failed: Unexpected token (7:6)
You may need an appropriate loader to handle this file type.
| // Usage:
| // powershell -ExecutionPolicy Unrestricted -Version "2.0" -Command "&{Add-Type -Path Find-VS2017.cs; [VisualStudioConfiguration.Main]::Query()}"
> using System;
| using System.Text;
| using System.Runtime.InteropServices;
@ ./node_modules/node-gyp/lib sync ^\.\/.*$ ./Find-VS2017.cs
@ ./node_modules/node-gyp/lib/node-gyp.js
@ ./node_modules/node-pre-gyp/lib/util/compile.js
@ ./node_modules/node-pre-gyp/lib sync ^\.\/.*$
@ ./node_modules/node-pre-gyp/lib/node-pre-gyp.js
@ ./node_modules/sqlite3/lib/sqlite3.js
@ ./node_modules/typeorm/platform/PlatformTools.js
@ ./node_modules/typeorm/index.js
@ ./src/main.ts

ERROR in ./node_modules/node-pre-gyp/lib/unpublish.js
Module not found: Error: Can't resolve 'aws-sdk' in '/my/project/node_modules/node-pre-gyp/lib'
@ ./node_modules/node-pre-gyp/lib/unpublish.js 16:14-32
@ ./node_modules/node-pre-gyp/lib sync ^\.\/.*$
@ ./node_modules/node-pre-gyp/lib/node-pre-gyp.js
@ ./node_modules/sqlite3/lib/sqlite3.js
@ ./node_modules/typeorm/platform/PlatformTools.js
@ ./node_modules/typeorm/index.js
@ ./src/main.ts

ERROR in ./node_modules/node-pre-gyp/lib/info.js
Module not found: Error: Can't resolve 'aws-sdk' in '/my/project/node_modules/node-pre-gyp/lib'
@ ./node_modules/node-pre-gyp/lib/info.js 14:14-32
@ ./node_modules/node-pre-gyp/lib sync ^\.\/.*$
@ ./node_modules/node-pre-gyp/lib/node-pre-gyp.js
@ ./node_modules/sqlite3/lib/sqlite3.js
@ ./node_modules/typeorm/platform/PlatformTools.js
@ ./node_modules/typeorm/index.js
@ ./src/main.ts

ERROR in ./node_modules/node-pre-gyp/lib/publish.js
Module not found: Error: Can't resolve 'aws-sdk' in '/my/project/node_modules/node-pre-gyp/lib'
@ ./node_modules/node-pre-gyp/lib/publish.js 18:14-32
@ ./node_modules/node-pre-gyp/lib sync ^\.\/.*$
@ ./node_modules/node-pre-gyp/lib/node-pre-gyp.js
@ ./node_modules/sqlite3/lib/sqlite3.js
@ ./node_modules/typeorm/platform/PlatformTools.js
@ ./node_modules/typeorm/index.js
@ ./src/main.ts

我的package.json(我删除了一些依赖项以缩短它)

{
  "name": "something-with-electron",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "prestart": "npm run build",
    "start": "npm run build && electron ./dist/main.js",
    "build": "webpack --config ./webpack.config.js",
    "postinstall": "install-app-deps"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {},
  "devDependencies": {
    "@babel/cli": "^7.2.0",
    "@babel/core": "^7.2.0",
    "@babel/plugin-proposal-class-properties": "^7.2.1",
    "@babel/preset-env": "^7.2.0",
    "@babel/preset-react": "^7.0.0",
    "@babel/preset-typescript": "^7.1.0",
    "@blueprintjs/core": "^3.10.0",
    "@types/lodash": "^4.14.119",
    "@types/mime-types": "^2.1.0",
    "@types/node": "^10.12.12",
    "@types/react": "^16.7.13",
    "@types/react-dom": "^16.0.11",
    "@types/react-redux": "^6.0.11",
    "@types/electron-devtools-installer": "^2.2.0",
    "babel-loader": "^8.0.4",
    "css-loader": "^2.0.0",
    "electron": "^3.0.10",
    "electron-builder": "^20.38.5",
    "electron-devtools-installer": "^2.2.4",
    "html-webpack-plugin": "^3.2.0",
    "node-sass": "^4.11.0",
    "react": "^16.6.3",
    "react-dom": "^16.6.3",
    "redux": "^4.0.1",
    "sass-loader": "^7.1.0",
    "sqlite3": "^4.0.6",
    "standard": "^12.0.1",
    "standard-loader": "^6.0.1",
    "style-loader": "^0.23.1",
    "ts-loader": "^5.3.1",
    "tslint": "^5.11.0",
    "tslint-config-standard": "^8.0.1",
    "tslint-loader": "^3.5.4",
    "tslint-webpack-plugin": "^1.3.0",
    "typeorm": "^0.2.11",
    "typescript": "^3.2.2",
    "webpack": "^4.27.1",
    "webpack-cli": "^3.1.2"
  }
}

还有我的webpack.config.js:

const path = require('path')
const HTMLWebpackPlugin = require('html-webpack-plugin');

const commonConfig = {
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].js'
  },
  mode: 'development',
  module: {
    rules: [
      {
        test: /\.ts$/,
        enforce: 'pre',
        loader: 'babel-loader'
      },
      {
        test: /\.tsx?$/,
        loader: 'babel-loader'
      },
      {
        test: /\.js$/,
        enforce: 'pre',
        loader: 'standard-loader'
      },
      {
        test: /\.scss$/,
        use: [
          {
            loader: 'style-loader'
          },
          {
            loader: 'css-loader'
          },
          {
            loader: 'sass-loader'
          }
        ]
      }
    ]
  },
  node: {
    __dirname: false,
    __filename: false
  },
  resolve: {
    extensions: ['.js', '.ts', '.tsx', '.jsx', '.json']
  }
}

module.exports = [
  Object.assign(
    {
      target: 'electron-main',
      entry: { main: './src/main.ts' }
    },
    commonConfig),

  Object.assign(
    {
      target: 'electron-renderer',
      entry: { gui: './src/gui.tsx' },
      plugins: [new HTMLWebpackPlugin({
        title: 'something',
        template: './src/index.html'
      })]
    },
    commonConfig)
]

感谢任何帮助!

最佳答案

我在设置项目时遇到了同样的问题。

要解决此问题,您可以将以下插件添加到您的 webpack 配置中,以删除您不想使用的插件的警告

new FilterWarningsPlugin({
    exclude: [/mongodb/, /mssql/, /mysql/, /mysql2/, /oracledb/, /redis/, /sqlite3/, /sql.js/, /react-native-sqlite-storage/]
})

希望对你有帮助:)

关于typescript - 构建时输入ORM 'Module not found' (webpack),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54224535/

相关文章:

javascript - 获得正确的日期,但是当我推送到我的月份数组时,我得到奇怪的日期输出

javascript - Webpack 无法在外部包中构建 ES6

javascript - Webpack: 运行时出错 "npm start"

javascript - 有 Arcgis esri commonjs 版本吗?

visual-studio-2010 - 错误 MSB3411 : Could not l oad the Visual C++ component "VCBuild.exe"

reactjs - 如何将react-hooks、redux和typescript结合在一起?

javascript - 我可以获得从 Firestore 集合组查询中获取的文档的路径或引用吗?

css - 在 Highcharts 中突出显示 x 轴标签

node.js - 找不到 v8-debug@0.4.6 和 node@4.4.5 的预构建二进制文件

node.js - 推送到 Heroku 失败 - 错误 fsevents@0.2.0 安装 : `node-gyp rebuild`