生成(Webpack)时出现TypeORM'找不到模块'

时间:2019-01-16 20:04:06

标签: typescript webpack node-gyp typeorm

我用一段时间搜索了这个问题,并在typeorm github上找到了几篇文章,但是由于没有提供解决方案,它们都被关闭了。这似乎是一个webpack问题,但我不知道问题到底是什么。该示例对我来说编译很好。

一旦我调用createConnection导入的typeorm,就会发生这种情况。我得到的错误如下。我删掉了大多数“无法解决...”的问题,但您明白了。

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)
]

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

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

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

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

希望对您有帮助:)