Reputation: 7951
I have a small NodeJS project that I want to deploy in Cloud Functions. Because this project uses Pubsub, I used the emulator in my local machine to simulate Pubsub and so I can test locally without deploying first. I followed the instructions on Functions Framework and Pubsub emulator from here and here.
This is how my folder is structured:
project-folder/
+|index.js
|package.json
|lib/
+|mod_1.js
|mod_2.js
...
|mod_n.js
|node_modules/
Where the js
files in lib
folder are my utility functions. I import these modules in the program's entry-point file index.js
(as I am not too familiar with "standard" NodeJS dependency management) in local dev like this:
const mod_1 = require('./lib/mod_1');
const { mod_2a, mod_2b } = require('./lib/mod_2');
...
The package.json
looks like this:
"scripts": {
"start": "functions-framework --port=3000 --target=app",
And I start my application using npm start
, which pretty much works.
However, when I try to deploy to Cloud Functions using gcloud functions deploy...
command in my shell, the deployment fails as it cannot find my private modules. So reading from documentation about specifying dependencies in Cloud Functions here, I updated the dependencies in my package.json
to include my modules in lib
:
"dependencies": {
"@google-cloud/functions-framework": "^1.5.1",
"@google-cloud/pubsub": "^2.1.0",
"express": "^4.17.1",
...
"mod_1": "file:./lib/mod_1",
"mod_2": "file:./lib/mod_2",
...
},
But still fails to deploy due to the same issue and now with the added problem that I have error when I run my program locally as it cannot detect the modules. I tried various combinations in index.js
and package.json
like removing the lib
prefix in require
, adding a .js
, etc, but none seems to work.
My question is:
(1) Is there a single, universal standard (folder naming, importing libraries, etc) for dependencies in NodeJS? It seems to me that different frameworks has different styles.
(2) How can I make the dependency work both in my local dev environment and when I deploy to Cloud Functions?
My NodeJS runtime is version 10 (at least for Cloud Functions).
Upvotes: 0
Views: 333
Reputation: 821
Node.js dependencies are other modules installed through NPM, your utility functions are not module dependencies. You don't need to add them to package.json. When you deploy your function all your source code will get packaged into a zip-file and uploaded. Here is a link to the correct guide you should be looking at: https://cloud.google.com/functions/docs/deploying/filesystem
So remove the mod_1 and mod_2 lines from your package.json, and any other references you might in there that point directly to your own source code.
Upvotes: 2