Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error: Could not resolve "fs" #90

Closed
frading opened this issue May 7, 2020 · 4 comments
Closed

error: Could not resolve "fs" #90

frading opened this issue May 7, 2020 · 4 comments

Comments

@frading
Copy link

frading commented May 7, 2020

My library includes packages (opentype that is in threejs, and ammo.js) which use require, path and fs.

When compiling with webpack, I add the following to the config:

node: {
	fs: 'empty'
},

But how would you solve this with esbuild? Here is below the errors that are printed out:

modules/three/examples/jsm/libs/opentype.module.min.js:19:457: error: "require" must not be called indirectly
...ports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.co...
                                                                                                         ~~~~~~~
modules/three/examples/jsm/libs/opentype.module.min.js:19:757: error: "require" must not be called indirectly
...,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
                                                                                                                             ~~~~~~~
node_modules/ammojs-typed/ammo/ammo.js:11:83: error: Could not resolve "fs"
if(ea){ia=__dirname+"/";var na,oa;ka=function(a,b){var e=pa(a);e||(na||(na=require("fs")),oa||(oa=require("path")),a=oa.normalize(a),e=na.readFileSync(a));return b?e:e.toString()};ma=function(a){a=ka(a,!0);a.buffe...
                                                                                   ~~~~
node_modules/ammojs-typed/ammo/ammo.js:11:106: error: Could not resolve "path"
...a){ia=__dirname+"/";var na,oa;ka=function(a,b){var e=pa(a);e||(na||(na=require("fs")),oa||(oa=require("path")),a=oa.normalize(a),e=na.readFileSync(a));return b?e:e.toString()};ma=function(a){a=ka(a,!0);a.buffer...
                                                                                                         ~~~~~~
4 errors

And here is how to reproduce:

git clone https://github.com/polygonjs/polygonjs-engine.git;
cd polygonjs-engine;
git checkout esbuild;
yarn;
node_modules/esbuild/bin/esbuild src/engine/index.ts --bundle '--define:process.env.NODE_ENV="development"' --loader:.glsl=text --outfile=out.js;
@jorgelo
Copy link

jorgelo commented May 7, 2020

I found that when using it via the API, the build method can take in parameter of externals. This seems to exclude them from the build.

@frading
Copy link
Author

frading commented May 7, 2020

@jorgelo thank you, that does get rid of the fs and path errors. But I haven't found a way to get rid of opentype's require yet.

Here is the current build script I'm using:

import {build} from 'esbuild';
import {BuildOptions} from 'esbuild/lib/main';

const out = './dist/out.js';

const options: BuildOptions = {
	stdio: 'inherit',
	entryPoints: ['./src/engine/index.ts'],
	target: 'esnext',
	outfile: out,
	minify: true,
	bundle: true,
	define: {
		'process.env.NODE_ENV': '"development"',
	},
	external: ['require', 'fs', 'path'],
	loader: {
		'.glsl': 'text',
	},
};

build(options).catch(() => process.exit(1));

@evanw evanw closed this as completed in 792652c Jun 12, 2020
@evanw
Copy link
Owner

evanw commented Jun 12, 2020

This should be fixed in version 0.5.3. It turns out the problem was specifically caused by code bundled with Browserify, so I have special-cased this code pattern in esbuild. You can read more in the release notes.

@frading
Copy link
Author

frading commented Jun 12, 2020

Amazing, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants