webpack: webpack does not include node_modules dependency!!

Do you want to request a feature or report a bug?

bug

What is the current behaviour? When there is no node_modules folder in the current directory when I build my typescript node project and run node dist/index.js it throws all shorts of error for eg: cannot find module 'graphql-import' cannot find module 'express'

If the current behaviour is a bug, please provide the steps to reproduce.

my webpack.prod.js file:

const path = require("path")
const nodeExternals = require("webpack-node-externals")
const Dotenv = require("dotenv-webpack")

module.exports = {
  entry: "./src/server.ts",
  externals: [nodeExternals()],
  mode: "production",
  devtool: "inline-source-map",
  target: "node",
  resolve: {
    extensions: [".ts", ".js"],
    modules: [path.resolve("./node_modules"), path.resolve("./src")]
  },
  node: {
    __dirname: false
  },
  output: {
    filename: "index.js",
    path: path.resolve(__dirname, "dist")
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: "ts-loader",
        exclude: /node_modules/
      }
    ]
  },
  plugins: [
    new Dotenv({
      path: "./.env"
    })
  ]
}

my tsconfig.json file:

{
  "compileOnSave": true,
  "compilerOptions": {
    "baseUrl": "src",
    "outDir": "./dist/",
    "skipLibCheck": true,
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "es6",
    "target": "es5",
    "allowJs": true,
    "moduleResolution": "node",
    "noUnusedLocals": false,
    "allowSyntheticDefaultImports": true,
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "lib": ["es2016", "esnext.asynciterable"]
  },
  "exclude": ["node_modules"]
}

What is the expected behaviour?

expect to run my nodejs server smoothly and printing šŸš€ Server is running on port: 4000

If this is a feature request, what is motivation or use case for changing the behaviour? No its not a feature request.

Please mention other relevant information such as the browser version, Node.js version, webpack version, and Operating System.

Node version: 10.13.0 (i tried with 12.x.x too) webpack: 4.39.2 webpack-cli: 3.3.7 typescript: 3.5.3

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16 (8 by maintainers)

Most upvoted comments

I encourage you to ask your question on StackOverflow instead. We try to use the issue tracker for bug reports and feature requests, as well as some discussions, but this appears to be a very localized question.

Iā€™m closing for these reasons.

@evilebottnawi So what about when using my code in a monorepo, I need to upload my entire repo just to execute my nodejs-api code, any workaround for this case?

It is not impact the performance

Just do not bundle server code and update server code and node_modules to cloud

Do not bundle code for node and bundle code which only for browser

so what can I do so that webpack will bundle all the dependencies to index.js file

It is expected, you still need node_modules when you use target: node