Any idea on what can cause the following issue? We're trying to migrate to the official Cloud SQL Node.js Connector but for some reason I'm getting the following issue when running a test connection with the new config:
Support to node crypto module is required in node_modules/@google-cloud/cloud-sql-connector/src/node-crypto.ts:26:11
It's this bit in the package:
Object.defineProperty(exports, "__esModule", { value: true });
exports.cryptoModule = cryptoModule;
const errors_1 = require("./errors");
async function cryptoModule() {
// check for availability of crypto module and throws an error otherwise
// ref: https://nodejs.org/dist/latest-v18.x/docs/api/crypto.html#determining-if-crypto-support-is-unavailable
let crypto;
try {
crypto = await import('node:crypto');
/* c8 ignore next 6 */
}
catch (err) {
throw new errors_1.CloudSQLConnectorError({
message: 'Support to node crypto module is required',
code: 'ENOCRYPTOMODULE',
});
}
return crypto;
}
//# sourceMappingURL=node-crypto.js.map
We are running on Node v20
This is our webpack config:
const webpack = require("webpack");
const NodeExternals = require("webpack-node-externals");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const path = require("path");
let {
TRAVIS_BRANCH,
TRAVIS_COMMIT,
TRAVIS_COMMIT_MESSAGE,
TRAVIS_BUILD_NUMBER,
TRAVIS_BUILD_ID,
} = process.env;
let buildInfo = {
started: Date.now(),
buildId: isNaN(parseInt(TRAVIS_BUILD_ID)) ? null : parseInt(TRAVIS_BUILD_ID),
buildNumber: isNaN(parseInt(TRAVIS_BUILD_NUMBER))
? null
: parseInt(TRAVIS_BUILD_NUMBER),
commitMessage: TRAVIS_COMMIT_MESSAGE || null,
commit: TRAVIS_COMMIT || null,
branch: TRAVIS_BRANCH || "local",
};
module.exports = {
mode: "production",
entry: { index: "./index.js" },
optimization: {
minimizer: [
new TerserPlugin({
terserOptions: {
mangle: false, // <-- IMPORTANT, we use func.name for things
},
}),
],
},
output: {
filename: "[name].js",
libraryTarget: "commonjs",
devtoolModuleFilenameTemplate: "[resource-path]",
},
target: "node",
module: {
rules: [
{
test: /\.(jsx?)$/,
exclude: /node_modules/,
use: ["swc-loader"],
},
{
test: /\.(tsx?)$/,
exclude: /node_modules/,
use: {
loader: "swc-loader",
options: {
jsc: {
parser: {
syntax: "typescript",
},
},
},
},
},
],
},
resolve: {
extensions: [".js", ".ts", ".json"],
alias: {
"@Api": path.resolve(process.cwd(), "src/api"),
"@Models": path.resolve(process.cwd(), "src/api/models"),
"@Types": path.resolve(process.cwd(), "src/api/types"),
"@Consts": path.resolve(process.cwd(), "src/api/consts"),
"@Utils": path.resolve(process.cwd(), "src/api/utils"),
"@Functions": path.resolve(process.cwd(), "src/functions"),
},
},
devtool: "source-map",
plugins: [
new webpack.BannerPlugin({
raw: true,
banner: "require('source-map-support').install();",
}),
new webpack.DefinePlugin({
"process.env.BUILD_INFO": JSON.stringify(buildInfo),
}),
new CopyWebpackPlugin({
patterns: [{ from: "package.json" }],
}),
],
externals: [NodeExternals()],
};
And this is where it fails:
console.log("[sql-connector] crypto available:", typeof require("crypto"));
console.log("[sql-connector] connector loaded from:", require.resolve("@google-cloud/cloud-sql-connector"));
const clientOpts = await connector.getOptions({
instanceConnectionName: config.connect.cloudConnectionName,
authType: AuthTypes.IAM,
ipType: IpAddressTypes.PRIVATE,
});
Crpyto is available at the time of the logs and connector is properly loaded from: @google-cloud/cloud-sql-connector
I've tried:
- using different auth / ip types
- Checked the format of cloudConnectionName which matches the required format of:
my-project:europe-west1:db-name - Tried setting a fallback on the crypto dependency to avoid polyfills like:
fallback: {
crypto: false,
},
//and also in externals
externals: [NodeExternals(), {crypto: false}],
- checked that the target is
"node"
I want to avoid patching this package by updating the import to the commonJs require format and using npx patch-package
webpack --config ./webpack/webpack.config.js && webpack --config ./webpack/webpack.api.config.js --watch