我编辑了我的问题,因为我仍然有同样的问题。
我正在使用 webpack、TypeScript 和 Sequelize。我正在尝试在后端 TypeScript 文件中使用 Sequelize。
我通过 npm
安装了 Sequelize 和 Sequelize TypeScript。
npm i sequelize
npm i sequelize-typescript
然后,在我的 server.ts
文件中,我像这样测试连接:
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);
});
当我构建我的应用程序时,我的节点控制台出现错误:"module mysql not found"
。
所以我做了 npm i mysql
现在,我的控制台出现了很多错误:
这是我的 webpack 配置
:
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"
},
};
我正在尝试这些解决方案 https://github.com/sequelize/sequelize/issues/7509 和 https://github.com/sequelize/sequelize/issues/7509#issuecomment-335312273
我不明白这是什么问题。有人有想法吗?谢谢。
最佳答案
您可以通过更新您的 webpack 配置来解决 fs
问题:
node: {
fs: 'empty'
}
pg-hstore
问题与此sequelize
有关issue .提到的解决方法 here似乎是最简单的。您只需将以下内容添加到您的 webpack 配置中:
plugins: [
new webpack.ContextReplacementPlugin(
/Sequelize(\\|\/)/,
path.resolve(__dirname, '../src')
)
]
关于typescript - 如何使用 Webpack、TypeScript 和 Sequelize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47534458/