NPM packages not being installed using npx

I am currently working on a CLI application with NPM, but for some reason I get the following error after I package it and try running it:

> npx -y [organization]-[package]-0.0.1.tgz

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\Users\wathhr\AppData\Local\npm-cache\_npx\9765d4346a8e56c1\node_modules\[organization]\[package]\node_modules\fs-extra\lib\index.js' imported from C:\Users\wathhr\AppData\Local\npm-cache\_npx\9765d4346a8e56c1\node_modules\[organization]\[package]\dist\bin.js
    at new NodeError (node:internal/errors:405:5)
    at finalizeResolution (node:internal/modules/esm/resolve:324:11)
    at moduleResolve (node:internal/modules/esm/resolve:943:10)
    at defaultResolve (node:internal/modules/esm/resolve:1129:11)
    at nextResolve (node:internal/modules/esm/loader:163:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:835:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:77:40)
    at link (node:internal/modules/esm/module_job:76:36) {

Relevant parts of the package.json:

  "type": "module",
  "scripts": {
    "prepare": "npm run build"
  "devDependencies": {
    "@types/fs-extra": "^11.0.1",
    "@types/node": "^20.5.1",
    "@typescript-eslint/eslint-plugin": "^6.4.0",
    "@typescript-eslint/parser": "^6.4.0",
    "esbuild": "^0.19.2",
    "eslint": "^8.47.0"
  "dependencies": {
    "@clack/prompts": "^0.7.0",
    "command-exists": "^1.2.9",
    "fs-extra": "^11.1.1",
    "ts-deepmerge": "^6.2.0"
  "bin": "dist/bin.js",
  "main": "dist/bin.js",
  "files": [

I am using esbuild to bundle the app with the following config:

  entryPoints: ['./src/index.ts'],
  outfile: './dist/bin.js',
  banner: {
    'js': '#!/usr/bin/env node',
  minify: false,
  bundle: true,
  external: ['./node_modules/*'], // don't bundle node_modules because it causes errors
  platform: 'node',
  format: 'esm',
  logLevel: 'info',

How can I fix this?

if you need more information please ask so I can provide it :)


  • Replacing

    external: ['./node_modules/*']

    in the esbuild config with

    external: [
      ...Object.keys(pkg.devDependencies ?? {}),
      ...Object.keys(pkg.dependencies ?? {}),

    fixed the issue!