I have a Node.js Google cloud function which I now try to modularize into two js modules. It works fine locally on my machine but when trying to deploy to Google I get the following error:
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: npm ERR! @babel/cli not accessible from data-model
My main-function is using my data-model module via the following entry in package.json:
"dependencies": {
"@google-cloud/datastore": "^6.1.1",
"data-model": "file:../../data-model",
...
},
The data-model module in turn has the following package.json:
{
"name": "data-model",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "$(gcloud beta emulators datastore env-unset) && export GOOGLE_APPLICATION_CREDENTIALS=\"${GCPPRODFILEPATH}\" && jest --verbose --forceExit",
"build": "npx babel src --out-dir dist"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@google-cloud/datastore": "^6.6.2",
"@google-cloud/storage": "^5.16.1",
"cache-manager-redis-store": "^2.0.0",
"difference": "^1.0.2",
"gstore-node": "^7.2.6",
"ioredis": "^4.28.2",
"lodash": "^4.17.21"
},
"devDependencies": {
"@babel/cli": "^7.16.7",
"@babel/core": "^7.16.7",
"@babel/plugin-transform-modules-commonjs": "^7.16.5",
"@babel/plugin-transform-runtime": "^7.16.5",
"@babel/preset-env": "^7.16.5",
"jest": "^27.4.5"
}
}
and the index.js for data-model is just:
module.exports = Object.assign(
{},
require('./dist/garage'),
require('./dist/car'),
require('./dist/bike'),
require('./dist/util/cacheService'),
);
I'm very much a novice on npm and babel but my understanding was that when I build data-model to /dist babel would transform whatever needed and when I then use the module from main-function babel should be out of the picture. Clearly my understanding is wrong, but where?
I finally managed to solve this, got a bit side-tracked by the babel/cli error message that CloudFunction threw.
The issue was that my data-model module was in another directory:
"data-model": "file:../../data-model",
I put it inside the main-function instead:
"data-model": "file:data-model",
and it works just fine to deploy to cloud functions.
Why it is so I have no clue but it is kind of documented in the Google Cloud Docs too:
The code for this local module should be stored somewhere other than the node_modules folder within your function's root directory.