Could not resolve "path", could not resolve "os"
When trying to build a react project doing: npm run build-dev
I get a lot of Could not resolve "path" Could not resolve "os" Could not resolve "crypto"
And of course the build fails. I'm building for the browser.
I have to note I'm using this plugin:
const envFilePlugin = require('esbuild-envfile-plugin');
The browser does not provide packages such as os, so these packages are not available when building for the browser. If you want to substitute them for other ones, you should be able to use either the alias feature or a plugin for that.
That plugin (and most esbuild plugins) are meant to run in the Node.js runtime. While your error message seems that you're bundling that plugin into your browser bundle, which is incorrect.
That plugin (and most esbuild plugins) are meant to run in the Node.js runtime. While your error message seems that you're bundling that plugin into your browser bundle, which is incorrect.
Definitely I'm building for the browser here. But the odd part is this, it's a project I'm resuming from 1 year, which it was working ok, and now trying to re-build it, and it's complaining with this. That's odd.
Hitting the same set of issues. I am targeting cloudflare workers, but esbuild is unable to handle libraries like postgresjs
✘ [ERROR] Could not resolve "os"
_worker.js:15273:15:
15273 │ import os from "os";
╵ ~~~~
The package "os" 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.
✘ [ERROR] Could not resolve "fs"
_worker.js:15274:15:
15274 │ import 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.
✘ [ERROR] Could not resolve "net"
_worker.js:15750:16:
15750 │ import net from "net";
╵ ~~~~~
The package "net" 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.
✘ [ERROR] Could not resolve "tls"
_worker.js:15751:16:
15751 │ import tls from "tls";
╵ ~~~~~
The package "tls" 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.
✘ [ERROR] Could not resolve "crypto"
_worker.js:15752:20:
15752 │ import crypto3 from "crypto";
╵ ~~~~~~~~
The package "crypto" 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.
✘ [ERROR] Could not resolve "stream"
_worker.js:15753:19:
15753 │ import Stream from "stream";
╵ ~~~~~~~~
The package "stream" 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.
✘ [ERROR] Could not resolve "perf_hooks"
_worker.js:15754:28:
15754 │ import { performance } from "perf_hooks";
╵ ~~~~~~~~~~~~
The package "perf_hooks" 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.
▲ [WARNING] Comparison with -0 using the "===" operator will also match 0 [equals-negative-zero]
_worker.js:90:27:
90 │ } else if (x === -0) {
╵ ~~
Floating-point equality is defined such that 0 and -0 are equal, so "x === -0" returns true for both 0 and -0. You need to use "Object.is(x, -0)" instead to test for -0.
✘ [ERROR] Failed to build _worker.js.
I had the same error in Astro, and it was solved by removing the following lines from my code:
import * as dotenv from "dotenv";
dotenv.config();
These lines were previously used to load credentials from a .env file. I switched to using Astro's built-in support for environment variables by changing the code to:
const PASSWORD = import.meta.env.UNSPLASH_ACCESS_KEY;
For more details, you can check the official Astro documentation on environment variables here: https://docs.astro.build/en/guides/environment-variables/.