How to use Webpack, typescript, and sequelize

Viewed 2323

I edited my question beacuse I still have the same problem.

I'm using webpack, TypeScript and Sequelize. I'm trying to use Sequelize in a backend TypeScript file.

I installed Sequelize and Sequelize TypeScript by npm.

npm i sequelize 
npm i sequelize-typescript

Then, in my server.ts file, I test the connection like this:

import {Sequelize} from 'sequelize-typescript';

const sequelize =  new Sequelize({
        database: 'dbgala',
        dialect: 'mysql',
        username: 'root',
        password: ''
});

sequelize
    .authenticate()
    .then(() => {
        console.log('Connection has been established successfully.');
    })
    .catch(err => {
        console.error('Unable to connect to the database:', err);
    });

When I build my app, I have an error in my node console: "module mysql not found".

So I did npm i mysql

And now, I have lots of errors in my console:

enter image description here

This is my webpack config :

module.exports = {
    entry: "./server/server.ts", // server.ts is the backend
    output: {
        filename: "bundle.js",
        path: __dirname + "/dist"
    },

    devtool: "source-map",

    resolve: {
        // Add '.ts' and '.tsx' as resolvable extensions.
        extensions: [".ts", ".tsx", ".js", ".json"]
    },

    module: {
        rules: [
          'awesome-typescript-loader'.
            { test: /\.tsx?$/, loader: "awesome-typescript-loader" },
'source-map-loader'.
            { enforce: "pre", test: /\.js$/, loader: "source-map-loader" }
        ]
    },
builds.
    externals: {
        "react": "React",
        "react-dom": "ReactDOM"
    },
};

I'm trying these solutions https://github.com/sequelize/sequelize/issues/7509 and https://github.com/sequelize/sequelize/issues/7509#issuecomment-335312273

I don't understand what is the problem. Does anyone have an idea? Thank you.

2 Answers

Hi all, I also had this problem. The solution was as follows:

add in your webpack.config.js next

  externals: [
    'pg',
    { 'sqlite3':'commonjs sqlite3', },
    'tedious',
    'pg-hstore'
  ],
Related