evanw / esbuild

An extremely fast bundler for the web
https://esbuild.github.io/
MIT License
37.98k stars 1.14k forks source link

How can I use 'neutral' platform without getting 'The package "x" wasn't found on the file system but is built into node.' error? #3776

Closed mabdelfattah closed 4 months ago

mabdelfattah commented 4 months ago

I can't get the following code working after it is being built using 'browser' as platform, and when I try 'neutral', I get plenty of errors, how can I get around this?

import { Uppy } from '@uppy/core';
import Dashboard from '@uppy/dashboard';
import Transloadit from "@uppy/transloadit";

export default function uppyInput() {
   // Some code
}

Here is the build file:

import * as esbuild from 'esbuild';

esbuild.build({
    entryPoints: ['./resources/js/uppy-input.js'],
    outfile: './dist/uppy-input.js',
    bundle: true,
    mainFields: ['module', 'main'],
    platform: 'neutral',
    treeShaking: true,
    target: ['es2020'],
    minify: false,
});

When I run build, I get many of the following errors but for different libraries:

✘ [ERROR] Could not resolve "fs"

    node_modules/tus-js-client/lib.esm/node/fileSignature.js:1:20:
      1 │ import * as fs from 'fs';
        ╵                     ~~~~

  The package "fs" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.
evanw commented 4 months ago

That depends on what you want to do. If you want to leave the import to fs unbundled, then that's what external is for.

mabdelfattah commented 4 months ago

That depends on what you want to do. If you want to leave the import to fs unbundled, then that's what external is for.

Thank you @evanw , this fixed the problem.