Trying to generate RSA keys with crypto package and deploy it on AWS Lambda, I get an error that crypto package is undefined. Are there easy ways to deploy this package to Lambda without having building docker containers?
Yes, I read that node.js native packages have different binaries on mac (my current os) and linux, so there is an approach to build docker and deploy it, but I found it's not very clear for me, so if this is the only way to do it, maybe there are good resources to read about it also.
Thanks!
I tried to avoid docker as well but it's actually pretty easy to setup. Install the Community Edition
Pull this image with this:
docker pull lambci/lambda
To mount your dev folder run this:
docker run -v ~/[mydev-folder]:/var/task lambci/lambda:nodejs8.10
Open Kitematic from the Docker app. You should see the container you pulled. Select it and start it if it's not started. Then click "Exec" and you should get a bash prompt opened in /var/task which should be pointing at your dev folder.
I usually delete node_modules and then run npm install from inside the docker container. I also sls deploy from there was well.
You need to import the package as require("crypto"). It is just not defined on the global object.
const handler = () => {
console.log(crypto); // undefined
console.log(global.crypto); // undefined
console.log(require("crypto"); // Bingo! :D
}
If you arrived here because you are bundling a Nodejs lambda with rollup and using a version of uuid 7+, then you need to add
external: ["crypto"]
to your rollup.config.js so that rollup does not attempt to replace the require statement by whatever if finds better.
Related
When I create an application on Node.js, I perform the following steps to publish it for other people:
Add a main.js file, which calls node:
#!/usr/bin/env -S node --stack-size=10000
console.log("Hello world!")
Include it on package.json:
...
"name": "my_app",
...
"bin": {
"my_app": "src/main.js"
},
...
Type npm publish.
And done: anyone can then now install it by typing npm i -g my_app, and it will be made available to use on the terminal by typing my_app. What are the equivalent steps to publish an application built using Deno instead?
How to publish a Deno command-line application
Create an entry file (ex: main.ts).
Host your project somewhere.
Install it anywhere using the URL to main.ts:
deno install -n your_app https://your_url/your_app/main.ts
Passing options
You can pass Deno options in the same way you would for deno run:
deno --unstable install -n your_app --allow-all URL_HERE
Using Github
If you have a Github repository, you can get a URL to your file by browsing to it, and clicking on "Raw". It will be something like:
https://raw.githubusercontent.com/your_org/your_app/tree/master/main.ts
This URL can be used to install your app.
Using deno.land/x
You can also register a Github repository on https://deno.land/x. This will give you a shorter url:
https://deno.land/x/your_app/main.ts
As well as a way to keep track of immutable versions:
https://deno.land/x/your_app#version/main.ts
Thanks for the devs on Deno's Discord for the help.
Since deno uses urls for dependencies you do not need to publish anything from your cli. You can simply host it as a public repo on github. Then you can add it to their third-party library here by clicking on the 'publish a module' button
Situation
I have an Alpine/NodeJS Docker image running my app (Alpine Linux 3.11, nodeJS v12.15.0), and I recently needed to internationalize currencies in this app.
I noticed that my container was missing locales, so I needed to install full-ICU. Consequently I modified my Alpine-based Docker image to add two lines to install full-ICU:
RUN npm i -g full-icu
ENV NODE_ICU_DATA=“/home/node/.npm/lib/node_modules/full-icu”
The installation went smoothly, the console output said:
Step 10/15 : RUN npm i -g full-icu
---> Running in b14d826c8689
/home/node/.npm/bin/node-full-icu-path -> /home/node/.npm/lib/node_modules/full-icu/node-icu-data.js
> full-icu#1.3.1 postinstall /home/node/.npm/lib/node_modules/full-icu
> node postinstall.js
npm install icu4c-data#64l (Node 12.15.0 and small-icu 64.2) -> icudt64l.dat
full-icu$ /usr/bin/node /home/node/.npm/lib/node_modules/npm/bin/npm-cli.js install icu4c-data#64l
+ icu4c-data#0.64.2
added 1 package from 1 contributor in 62.073s
√ icudt64l.dat (link)
Node will use this ICU datafile if the environment variable NODE_ICU_DATA is set to “/home/node/.npm/lib/node_modules/full-icu”
or with node --icu-data-dir=/home/node/.npm/lib/node_modules/full-icu YOURAPP.js
For package.json:
{"scripts":{"start":"node --icu-data-dir=/home/node/.npm/lib/node_modules/full-icu YOURAPP.js"}}
By the way, if you have full data, running this in node:
> new Intl.DateTimeFormat('es',{month:'long'}).format(new Date(9E8));
... will show “enero”. If it shows “January” you don't have full data.
News: Please see https://github.com/icu-project/full-icu-npm/issues/6
+ full-icu#1.3.1
added 1 package from 1 contributor in 63.276s
It seems fine, it recognized my NodeJS version, there were no errors. I could check and see that the ICU data files were at the right place.
Problem
But when opening a shell inside this container (running docker run -ti myimage sh), and playing with Intl, I noticed that the locales were working properly only when running node with the --icu-data-dir option, not when using the NODE_ICU_DATA environment variable.
However, my preference definitely goes to the environment variable, for various reasons, so I would have liked it to work.
Tests so far
Here are my tests with node:
node --icu-data-dir=/home/node/.npm/lib/node_modules/full-icu
Welcome to Node.js v12.15.0.
Type ".help" for more information.
> new Intl.DateTimeFormat('es',{month:'long'}).format(new Date(9E8));
'enero'
It's saying "enero", so it's working. It means that full-ICU is properly installed and reachable.
export NODE_ICU_DATA=“/home/node/.npm/lib/node_modules/full-icu”
node
Welcome to Node.js v12.15.0.
Type ".help" for more information.
> new Intl.DateTimeFormat('es',{month:'long'}).format(new Date(9E8));
'January'
It doesn't care about my environment variable (also tried putting the environment variable in the Dockerfile, as shown above)
env NODE_ICU_DATA=“/home/node/.npm/lib/node_modules/full-icu” node
Welcome to Node.js v12.15.0.
Type ".help" for more information.
> new Intl.DateTimeFormat('es',{month:'long'}).format(new Date(9E8));
'January'
It also doesn't care when the environment variable is inlined.
I also tried with .js scripts by the way, not just the NodeJS console, and also various ways to pass the environment variable.
And just to be sure, I tried to install system ICU packages, with RUN apk --update add --no-cache icu icu-libs icu-dev.
So...
Would anyone have an idea about the reason why specifying the path in an environment variable doesn't work, and what I should check?
I have the same issue with on a VPS with Plesk.
I can't update the Node version and the installed Node version is the, v12.4.0.
In my case I can't also install full-icu as global module, and the process manager start my app without run the start script in the package.json.
In this situation for me, the only way for load the full-icu support is to use the environment vars.
I tried first via command line:
export NODE_ICU_DATA=/full/path/of/my/app/node_modules/full-icu
and then
-bash-4.2$ /opt/plesk/node/12/bin/node
Welcome to Node.js v12.4.0.
Type ".help" for more information.
> new Intl.DateTimeFormat('it',{month:'long'}).format(new Date(9E8));
'gennaio'
>
it works fine.
It works too if I run this command in the root folder of my app:
NODE_ICU_DATA=node_modules/full-icu /opt/plesk/node/12/bin/node
So I added an ENV var on the app node setting (in Plesk)
NODE_ICU_DATA
with a value
node_modules/full-icu
Restarting the app the i18n support rightly works.
I hope this could help others people there are in my same situation.
For anyone falling on this issue: the problem was version-specific.
Deploying a more recent NodeJS version fixed these ICU bugs.
I am working on an application via the toolchain tool on IBM Cloud and editing the code via the Eclipse Orion IDE. As I am not accessing this through my local cli, my understanding is that in order to so call npm install {package}, I would just need to include the package in the package.json file under dependencies and require it in my app. However, when I load the application, I get the require is not defined indicating that the package has not been installed. Moreover, the require() is being used in the app.js file with the application being launched but not from files in my public directory.
After playing around further, it seems it might have to do with the way the directory tree is being traced as the error is only thrown in subdirectories. For example, require('express') works in app.js which is in the main directory ./ but fails when it is called in test.js in ./subdirectory/test.js. I feel like I'm missing something painfully simple like configuration of endpoint or something.
I've been searching around but I can't seem to find how to get the packages loaded, preferably without using the cli. Appreciate any pointers. Thanks!
Update: After playing around further, I am also getting module is not defined error when trying to require from another file in the same directory. For example module.exports = 'str' returns this error. While trying to require('./file') returns the require is not defined. It might have to do with how node is wrapping the functions?
Update 2: Tried "start": "npm install && node app.js" in package.json but no luck. Adding a build stage which calls npm install before deployment also does not work
Update 3: After adding npm install build stage, I am able to see that the dependencies have been successfully built via the logs. However, the require is not defined error still persists.
Update 4: Trying npm install from my CLI doesn't work as well even though all packages and dependencies are present
Update 5: Running cf restage or configuring cache via cacheDirectories does not work as well
Opened a related question regarding deployment here
Found out my confusion was caused due to me not realizing that require() cannot be used on the client side unless via tools such as Browserify.
I have in one Digital Ocean Linux Ubuntu 16.041 droplet an instance of a Meteor app. Moreover, I installed Node.js version of 4.4.0
I created the respective bundle on the path ~/MyApplication/bundle. As a matter of fact, I automated with a config.json file the execution which its body contains the following code:
My main.js file is created from the Meteor build --directory ~/myDirectory/ command and contains the following code:
process.argv.splice(2, 0, 'program.json');
process.chdir(require('path').join(__dirname, 'programs', 'server'));
require('./programs/server/boot.js');
That being said, I run my application with pm2 through the config JSON file with pm2 start config.json
It starts the application, but several ms afterwards it is errored. The log error show the following exception regarding the typo, but seems the mainjs file is created fine. Any suggestions to solve it?
In the end, I had to update the node version in the server from 4.4.0 to 8.9.1. After doing that changes, I also had to install the dependencies within the bundle/programs/server/ using npm install
I have written an application which needs Node.js >= 4.2.6
I'm using the OpenShift service and the default Node.Js version is 0.10. Installing this https://github.com/ryanj/nodejs-custom-version-openshift is supposed to resolve this problem. So I followed the instructions and created a new application using:
rhc app create nodeapp nodejs --from-code=git://github.com/ryanj/nodejs-custom-version-openshift.git
So far so good. Next step - copy my project to the cloned git repository. Here's where I'm unsure if my approach is right (new to Node js). Because the cloned git already has a JSON package and because my app has its own JSON package in its root directory, I enriched the JSON package that came with nodejs-custom-version-openshift.git with my dependencies and set the engines to 4.2.6. The main is set to server.js. To run my app, I'm using "var variable = require('./myapp-master/test/test');" as the last line in my server.js. I copy the content of my app to the cloned repository (including node modules etc.) careful not to overwrite any existing config files like the JSON Package and then I use git push. The node version is installed properly according to the logs (upon build), servers.js is executed and it invokes my test.js. I can see in the app logs (app-root/logs) that test.js throws a specific error that indicates that the node js version is not >= 4.2.6.
When I log-in with PUTTY and write node -v, I get 4.2.6. When I navigate to my test.js and start it manually with node test.js, it runs normally. What am I doing wrong? I suspect I'm not handling the JSON packages properly (or my approach with starting my app from server.js is flawed). Any help is greatly appreciated! Thank you for your time.
Ok, I found the solution to this: https://github.com/ryanj/nodejs-custom-version-openshift/issues/2