Skip to content

Fix esbuild bundling errors caused by @mapbox/node-pre-gyp optional requires - #259

Merged
yanyiwu merged 2 commits into
masterfrom
copilot/fix-compile-error-dependencies
Mar 5, 2026
Merged

Fix esbuild bundling errors caused by @mapbox/node-pre-gyp optional requires#259
yanyiwu merged 2 commits into
masterfrom
copilot/fix-compile-error-dependencies

Conversation

Copilot AI commented Mar 5, 2026

Copy link
Copy Markdown
Contributor

When bundling apps that use nodejieba with esbuild, the bundler statically analyzes @mapbox/node-pre-gyp's dependency tree and fails on optional/conditional require() calls in s3_setup.js for mock-aws-s3, aws-sdk, nock, and an unbundleable .html file.

Changes

  • index.js: Replace the runtime require('@mapbox/node-pre-gyp') + binary.find() with a direct path computation derived from the static module_path/module_name values in package.json.
// Before
var binary = require('@mapbox/node-pre-gyp');
var path = require('path');
var binding_path = binary.find(path.resolve(path.join(__dirname,'./package.json')));
var nodejieba = require(binding_path);

// After
var path = require('path');
var BINARY_PATH = path.join(__dirname, 'build/Release/nodejieba.node');
var nodejieba = require(BINARY_PATH);

@mapbox/node-pre-gyp remains in dependencies — it's still invoked via npx in the install lifecycle script to download/build the native binary. Removing the runtime require() is sufficient to prevent esbuild from traversing its dependency tree.

Original prompt

This section details on the original issue you should resolve

<issue_title>编译报错,找不到外部依赖包</issue_title>
<issue_description>✘ [ERROR] Could not resolve "mock-aws-s3"

node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:43:28:
  43 │     const AWSMock = require('mock-aws-s3');
     ╵                             ~~~~~~~~~~~~~

You can mark the path "mock-aws-s3" as external to exclude it from the bundle, which will remove
this error and leave the unresolved path in the bundle. You can also surround this "require" call
with a try/catch block to handle this failure at run-time instead of bundle-time.

✘ [ERROR] Could not resolve "aws-sdk"

node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:76:22:
  76 │   const AWS = require('aws-sdk');
     ╵                       ~~~~~~~~~

You can mark the path "aws-sdk" as external to exclude it from the bundle, which will remove this
error and leave the unresolved path in the bundle. You can also surround this "require" call with
a try/catch block to handle this failure at run-time instead of bundle-time.

✘ [ERROR] Could not resolve "nock"

node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:112:23:
  112 │   const nock = require('nock');
      ╵                        ~~~~~~

You can mark the path "nock" as external to exclude it from the bundle, which will remove this
error and leave the unresolved path in the bundle. You can also surround this "require" call with
a try/catch block to handle this failure at run-time instead of bundle-time.

✘ [ERROR] No loader is configured for ".html" files: node_modules/@mapbox/node-pre-gyp/lib/util/nw-pre-gyp/index.html

node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js:86:21:
  86 │       return require('./' + command)(self, argvx, callback)</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…de-pre-gyp

Replace the runtime require('@mapbox/node-pre-gyp') in index.js with a
direct binary path computation. The module_path (./build/Release/) and
module_name (nodejieba) in package.json are static, so the binary is
always at build/Release/nodejieba.node.

This prevents esbuild from traversing @mapbox/node-pre-gyp's dependency
tree and encountering optional requires for mock-aws-s3, aws-sdk, nock,
and .html files which are not bundleable.

The @mapbox/node-pre-gyp package remains in dependencies since it is
still needed for the npm install lifecycle script.

Co-authored-by: yanyiwu <2162645+yanyiwu@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix compilation error due to missing external dependencies Fix esbuild bundling errors caused by @mapbox/node-pre-gyp optional requires Mar 5, 2026
@yanyiwu
yanyiwu marked this pull request as ready for review March 5, 2026 11:35
@yanyiwu
yanyiwu merged commit 28aac33 into master Mar 5, 2026
18 checks passed
@yanyiwu
yanyiwu deleted the copilot/fix-compile-error-dependencies branch March 5, 2026 11:35
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

Successfully merging this pull request may close these issues.

编译报错,找不到外部依赖包

2 participants