專案使用typescript並用webpack打包
本地端直接使用nodemon可以正確執行
使用webpack打包後可以使用node index.min.js執行
但是打包成docker image後卻無法正確運行container
node:internal/modules/cjs/loader:944
throw err;
^
Error: Cannot find module 'dotenv/config'
Require stack:
- /src/index.min.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:941:15)
at Function.Module._load (node:internal/modules/cjs/loader:774:27)
at Module.require (node:internal/modules/cjs/loader:1013:19)
at require (node:internal/modules/cjs/helpers:93:18)
at Object.1201 (/src/index.min.js:1:136901)
at a (/src/index.min.js:1:137375)
at Object.7728 (/src/index.min.js:1:135220)
at a (/src/index.min.js:1:137375)
at /src/index.min.js:1:137415
at Object.<anonymous> (/src/index.min.js:1:137423) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/src/index.min.js' ]
}
const path = require('path');
const webpack = require('webpack');
const nodeExternals = require('webpack-node-externals');
module.exports = {
target: 'node',
mode: 'production',
entry: './src/server.ts',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.min.js',
},
optimization: {
// minimize: false,
},
resolve: {
// Add `.ts` and `.tsx` as a resolvable extension.
extensions: ['.ts', '.tsx', '.js'],
},
externals: [nodeExternals(), 'pg', 'sqlite3', 'tedious', 'pg-hstore', 'dotenv/config'],
plugins: [
new webpack.ContextReplacementPlugin(/Sequelize(\\|\/)/, path.resolve(__dirname, './src')),
],
module: {
rules: [
// all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
{ test: /\.tsx?$/, loader: 'ts-loader' },
],
},
};
FROM node:current-alpine3.12
RUN mkdir /src
COPY dist /src
COPY .env /src
WORKDIR /src
CMD ["node","index.min.js"]
{
"compilerOptions": {
"strict": true,
"esModuleInterop": true,
"outDir": "build"
}
}
原本以為是dotenv/config的問題
改用
require('dotenv/confit).require()
之後卻又變成找不到express
有前輩遇過類似的問題嗎?