azure functions -running npm install on azure portal - node.js

This is a how to question, not a problem
I would like use an npm package in my javascript Azure function. How do I get a console in the azure functions portal to run npm install? If there is a better way to reference the npm package, this will answer my question too. A documentation reference with an example of installing the npm package in an
Azure function would also be appreciated.
I apologize for this basic question. I am new to both node and azure functions and have enjoyed learning what I have so far, but this simple procedure is eluding me.
Thank you for any help you can give me

You can use the KUDU console at https://yourfunctionappname.scm.azurewebsites.net and navigate to the wwwroot folder and install the package that you need!
Here is an article that i wrote to showcase how you can install python packages!
However when you deploy the azure function package.json will have all the dependencies and you dont need to install them separately.

Assume the function name is function677761
Step1. Open the browser and visit the Kudo console.
https://function677761.scm.azurewebsites.net/DebugConsole
Step2. In the toolbar menu select DebugConsole/CMD
Step3. Run the commands:
C:\home>ls
C:\home>cd .\site\wwwroot
C:\home\site\wwwroot>npm i date-and-time
C:\home> ls
Step4. My output is
HttpTrigger1
host.json
node_modules
package-lock.json
package.json
Step5. Now inside the file package.json there is the node.js library date-and-time just installed.
"dependencies": {
"date-and-time": "^2.4.2"
}
Step6. I can read the file package.json from here.
https://portal.azure.com/##myemail345.onmicrosoft.com/resource/subscriptions/3877920b-027777-4513-8777a-ca378888917/resourcegroups/group001/providers/Microsoft.Web/sites/function677761/functionsAppFiles
These values are custom.
Depend by my account.
myemail345
group001
function677761
3877920b-027777-4513-8777a-ca378888917
Source1.
https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings?tabs=portal#settings
Section.
Manually install extensions .
Source2.
https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-node?tabs=v2-v3-v4-export%2Cv2-v3-v4-done%2Cv2%2Cv2-log-custom-telemetry%2Cv2-accessing-request-and-response%2Cwindows-setting-the-node-version
Section.
Folder structure

Related

How To Setup Private NPM Module With Firebase Cloud Functions .npmrc?

I have created a private typings npm module that I am using for my firebase functions and app projects. When I went to deploy firebase functions, I get a big error for every function that basically says ERR! remote: Invalid username or password.
For what I have read, it looks like I need to create a .npmrc file and put it in the /functions directory. (https://cloud.google.com/functions/docs/writing/specifying-dependencies-nodejs#using_private_modules)
I cannot however find proper instructions on how to do this anywhere. From what I found, I have done the following:
ran npm login
ran npm token create --read-only
This then gave me a token that looks like this: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.
I then create a file called .npmrc in my functions directory, and placed //registry.npmjs.org/:_authToken=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX in it.
Additionally, I saw that the error message was trying to use ssh to install my private repo package, I have not setup ssh and am using https instead, because of this I changed my package file to git+https://github.com/accounts-name/repo#commit-num so that it uses HTTPS.
However, I still get the same error message. What am I missing? The above is what I have strung together from lots of google searching.
It seems that you have made too many different changes while trying to make it work, so let's just go through the whole process step by step.
Check the package.json of your npm module and publish it:
Remove "private" property or set it to false because private modules in the npm are meant to be never published. That is not obvious but that is true.
Next step is to apply restricted access to the package. In order to do that, add such property in the package.json file:
"publishConfig": {
"access": "restricted"
},
Make sure that npm account you use for publishing supports private packages.
Now open the terminal in the root directory of your package, type npm login then sign in to npm. Check if you put the proper version in the package.json.
Run npm publish. The package should be published in few seconds. No worries, thanks to publishConfig property nobody can access it.
Now it is time to allow package installation in your project
Go to the directory of the project and open package.json file
Check that you have the name and version of your package in the dependencies list
Open browser, navigate to https://npmjs.com, login to your account, navigate to settings page of your account and open the tokens tab
Create new token and copy it
Now again go to the directory of your project, on same level where package.json file is situated (that is important!) and create .npmrc file there.
Put such string in the .npmrc file:
//registry.npmjs.org/:_authToken=TOKEN_HERE
You are done!
Deployment with remote CI/CD services
The easiest approach is not add .npmrc into .gitignore. In such case the file will be always in repository, so npm install will run smoothly on any machine where project was cloned
If you don't want to have token string in the repository, you can move it to the environment variable of your CI/CD service and then link .npmrc file to that variable. For example, you can put generated token into the NPM_TOKEN env variable (Just token from npmjs, not the whole string from .npmrc!)
And then change the .npmrc file in the next way:
//registry.npmjs.org/:_authToken=${NPM_TOKEN}.
So, with those steps you should be able to install your restricted packages without any issues. Good luck!
If you are trying to deploy you functions with firebase deploy from a CI and your .npmrc file looks like this.
#acmecorp:registry=https://npm.pkg.github.com/
//npm.pkg.github.com/:_authToken=${NPM_REGISTRY_TOKEN}
You will run into the problem even if you have the env var set.
Build failed: Error: Failed to replace env in config: ${NPM_REGISTRY_TOKEN}
Firebase for some reason needs access to that private repo. But the env var is not sent over to firebase.
Solution I've implemented was to replace ${NPM_REGISTRY_TOKEN} in the .npmrc file on every run of the CI pipeline.
sed -i.bak "s/\${NPM_REGISTRY_TOKEN}/${NPM_REGISTRY_TOKEN}/g" .npmrc
This breaks if you use Yarn. Took me a while to find a thread pointing to npm install in the firebase cli predeploy step. If there's no package-lock.json and you only use yarn, this will break. Remove yarn.lock and install using npm to resolve the issue.

Cannot publish Bot to Azure using npm in VS Code

I'm trying to publish my chatbot application to Azure as instructed in the Azure Portal build menu:
Screenshot of instructions
I successfully download the code zip file, and it runs fine in VSCode and the Bot Emulator. However, when I do npm run azure-publish , I get the error npm ERR! missing script: azure-publish. I have not changed any of the build files or the node modules. This exact project runs without an issue in the Azure Online App Editor.
I don't understand where I'm going wrong. Where can I find the script for azure-publish? Is it even possible to publish using npm on VS Code? I've seen examples of publishing using Visual Studio, but haven't come across one with VS Code. Screenshot of directory
Any help or insight is much appreciated.
Resolved!
For anyone who might have the same problem, I had missed adding "scripts": {"azure-publish": "node publish.js"} to my package.json. Online Code Editor doesn't do it for you, so it has to be manually done if downloading code zip file.

How to include simple javascript file as node dependency in Cloud Functions for Firebase when using Kotlin multiplatform?

I would like to include a javascript file as module dependency. It's generated by Kotlin multiplatform, and it's called common-js.js. This is part of my package.json:
"dependencies": {
"common-js": "file:common-js"
}
I put this file into node_modules/common-js.js and it works with firebase serve with no problems.
However when I run firebase deploy, the deploy fails with:
Build failed: exit status 254
npm ERR! addLocal Could not install /workspace/common-js
How to achieve this with Cloud Functions? I tried putting it to other directories than node_modules but it doesn't work even locally.
Cloud Functions currently doesn't support the deployment of a fully-materialized node_modules folder. What it will do, during deployment, is install all the modules in your package.json with the assumption that npm can find them in the npm registry.
If you want to upload code from your machine to Cloud Functions, you'll have to include that along with all your other source code.
If you want deploy code that can be obtained through some other public source (like a GitHub repo), you should install that using npm like this:
npm install https://github.com/your-name/your-repo.git
If your code can't be downloaded publicly, it needs to be included with your source.
As Doug mentioned here, Firebase Functions doesn't support upload of node_modules. I need to upload it with other sources, but I didn't find a way how to create a node module outside node_modules directory. I solved it by modifying the generated index.js file with a gradle task which just loads a JS file, not a node module. Here is my gradle task:
task modifyCommonJsPath(type: Copy) {
from "functions/generated"
into "functions"
include "index.js"
filter {
it.replaceAll( "require\\('common-js'\\)", "require('./generated/common-js')" )
}
}
compileKotlin2Js.finalizedBy modifyCommonJsPath

How to use a package in Angular2 without publishing it to npm

I have a package which is published to npm and we are successfully using the package in our angular2 applications by defining the dependency in package.json as
"dependencies": {
"somesharedmodule": "^1.0.21",
}
and in our components as import {SharedService} from 'somesharedmodule'.
Now we dont want to make the library public(not interested to make private library too).My question is - i have a module in some folder of my local system.Can i use this module in my application without making the package published in npm. can somebody please guide me the alternatives to use a module in my application without publishing it?
Not clear what exactly you want to do... Bu there are lots of options:
npm link https://docs.npmjs.com/cli/link
use github private repo:
"dependencies": {
"ng2-resource": "https://github.com/{nickName}/{repoName}.git"
}
link your IDE to use local folder as node_modules
manually copy/paste library into project's node_modules

Getting Blade to work with Meteor on Windows 8

I installed Meteor for Windows via this MSI installer. Now I would like to get Blade working with it. I read through this tutorial in the Blade docs, but atmosphere/meteorite doesn't seem to work with Windows.
Is there a workaround?
Have you got meteor working? There is a manual way, ive not tried it before but it should work.
Install the blade module with npm npm install blade.
Clone the blade git repo and place the meteor subdirectory in meteors package directory, rename it to blade ( I think its C:\Program Files\Meteor\packages (but you'd have to check that directory)
Edit the files in the files in the blade directory to reference the files in the blade npm module correctly. In linux/unix this bit is easy because you could just symlink directly it into packages without copying the folders in and re referencing the files.
Finally go to your meteor project and run meteor add blade and it should be good to go.

Resources