什么?你的Nestjs项目部署的时候还要把node_module带上去?

目录

创建webpack配置文件

修改nestjs-cli 

 修改package


最近跟一个小伙伴聊天,他说他部署Nestjs项目的时候,每次都要把 package.json带上去然后使用npm 把依赖安装一次,才能运行项目。然后我也是给了小伙伴解决方案。

创建webpack配置文件

# webpack.config.js
const path = require('path');
const webpack = require('webpack');

module.exports = function (options) {
    return {
        ...options,
        entry: './src/main.ts',
        output: {
            filename: 'main.js',
            path: path.resolve(__dirname, 'dist'),
        },
        target: 'node',
        externals: [],
        optimization: {
            minimize: false
        }
    };
}; 

修改nestjs-cli 

# nest-cli.json
{
  "$schema": "https://json.schemastore.org/nest-cli",
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "deleteOutDir": true,
    "builder": "webpack",// 修改打包方式为webpack
    "typeCheck": true,
    "webpackConfigPath": "webpack.config.js",// 配置文件路径
    "tsConfigPath": "tsconfig.build.json"
  }
}

 修改package

{
    "scripts": {
        "build": "nest build --webpack"
    }
}

你可能感兴趣的:(node.js,node.js,javascript)