-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
"Cannot find module" error with What3Words library when bundling TypeScript with platform=node #1753
Comments
They are: (function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "./constants", "./requests/autosuggest", "./requests/autosuggest-selection", "./requests/available-languages", "./requests/convert-to-3wa", "./requests/convert-to-coordinates", "./requests/grid-section", "./utils"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.valid3wa = exports.getWords = exports.getOptions = exports.setOptions = exports.gridSectionGeoJson = exports.gridSection = exports.convertToCoordinatesGeoJson = exports.convertToCoordinates = exports.convertTo3waGeoJson = exports.convertTo3wa = exports.availableLanguages = exports.autosuggestSelection = exports.autosuggest = exports.W3W_REGEX = void 0;
var constants_1 = require("./constants");
Object.defineProperty(exports, "W3W_REGEX", { enumerable: true, get: function () { return constants_1.W3W_REGEX; } });
var autosuggest_1 = require("./requests/autosuggest");
Object.defineProperty(exports, "autosuggest", { enumerable: true, get: function () { return autosuggest_1.autosuggest; } });
var autosuggest_selection_1 = require("./requests/autosuggest-selection");
Object.defineProperty(exports, "autosuggestSelection", { enumerable: true, get: function () { return autosuggest_selection_1.autosuggestSelection; } });
var available_languages_1 = require("./requests/available-languages");
Object.defineProperty(exports, "availableLanguages", { enumerable: true, get: function () { return available_languages_1.availableLanguages; } });
var convert_to_3wa_1 = require("./requests/convert-to-3wa");
Object.defineProperty(exports, "convertTo3wa", { enumerable: true, get: function () { return convert_to_3wa_1.convertTo3wa; } });
Object.defineProperty(exports, "convertTo3waGeoJson", { enumerable: true, get: function () { return convert_to_3wa_1.convertTo3waGeoJson; } });
var convert_to_coordinates_1 = require("./requests/convert-to-coordinates");
Object.defineProperty(exports, "convertToCoordinates", { enumerable: true, get: function () { return convert_to_coordinates_1.convertToCoordinates; } });
Object.defineProperty(exports, "convertToCoordinatesGeoJson", { enumerable: true, get: function () { return convert_to_coordinates_1.convertToCoordinatesGeoJson; } });
var grid_section_1 = require("./requests/grid-section");
Object.defineProperty(exports, "gridSection", { enumerable: true, get: function () { return grid_section_1.gridSection; } });
Object.defineProperty(exports, "gridSectionGeoJson", { enumerable: true, get: function () { return grid_section_1.gridSectionGeoJson; } });
var utils_1 = require("./utils");
Object.defineProperty(exports, "setOptions", { enumerable: true, get: function () { return utils_1.setOptions; } });
Object.defineProperty(exports, "getOptions", { enumerable: true, get: function () { return utils_1.getOptions; } });
Object.defineProperty(exports, "getWords", { enumerable: true, get: function () { return utils_1.getWords; } });
Object.defineProperty(exports, "valid3wa", { enumerable: true, get: function () { return utils_1.valid3wa; } });
});
//# sourceMappingURL=index.js.map Content from here: https://unpkg.com/@what3words/api@3.3.6/umd/index.js. The problem is the package you are trying to use doesn't work with bundlers that require statically-analyzable CommonJS modules (notice how You could try to work around this with |
Thanks for the detailed response @evanw, much appreciated. Unfortunately the es2015 in the library references navigator which creates its own problems. Do you happen to know what is required for the transpilation on the library side to make this 'friendly' for the static analysis esbuild relies on, so I could propose a patch? The library source is pretty straightforward, and the underlying typescript all uses static imports so I'm assuming it's something to do with their babel typescript/transpilation config? I've tried googling around but not found the right guidance yet. |
Here's a simple patch for that package: --- a/package.json
+++ b/package.json
@@ -30,7 +30,7 @@
"clear": "rm -rf dist",
"create-version": "node ./scripts/version.js",
"build-js": "tsc --target es2015 --module es2015 --outDir dist/es2015",
- "build-node": "tsc --target es5 --module umd --outDir dist/umd",
+ "build-node": "tsc --target es5 --module CommonJS --outDir dist/umd",
"build-script": "node ./scripts/index.js",
"publish": "npm publish dist",
"test": "jest", See https://www.typescriptlang.org/tsconfig#module for more information about TypeScript's
I'm going to close this issue because it's not a problem with esbuild. The build script for that package is misconfigured. |
Thanks @evanw much appreciated |
I'm not clear if this is an issue with the What3Words node library, or the dependency, but when bundling with platform=node, the output JS does not include all the required dependencies.
I noticed in the documentation the limitation around dynamic dependencies (https://esbuild.github.io/api/#non-analyzable-imports) however, the imports don't appear to be dynamic in the original source code https://github.com/what3words/w3w-node-wrapper/blob/master/src/index.ts#L1 or in the transpiled output published to NPM.
Can anyone clarify where the issue lies?
I'm running esbuild 0.13.12, and node v12.17.0
index.ts:
package.json:
This is the output when running
esbuild --bundle "index.ts"
(click to expand output)
This is the output when running
esbuild --bundle "index.ts" --platform=node
. This appears to be much shorter and does not include the required files. When running it fails with "Cannot find module './constants'"(click to expand output)
I've raised a ticket with what3words here: what3words/w3w-node-wrapper#30 for reference
The text was updated successfully, but these errors were encountered: