how can I access laravel .env file from a npm packge? - node.js

I'm developing a bundle of a composer packages plus a npm package. This bundle is very specific, it's targeting only Laravel apps.
I'd like to access .env file from npm package, but I'm not able to find a way to detect where my app's .env file is located.
I repeat: I am able to read it using
require().config(..file path...)
but I am here to ask :
how to find a way to detect the path of .env file of the laravel app which required my node package?
I have no malicious intent, simply I'd like to import automatically the DB connection configuration.

On the PHP side you can detect the location of the env file by calling
App::environmentFilePath()

Related

Node Package not accessible in Sails.js

I installed node package via npm install and the Data is located in the node_modules directory as own directory.
But if re-lift and afterwards access a function of this new module, the browser told me, function not defined.
Do I need to add the path of the .js file in the layout or anywhere else?

typescript not reading env file

I am new to typescript and i built this simple mailer application, but the problem is that TypeScript is unable to read the file, when i put it in the src folder it was not copied in dist on build, when i put it outside the src folder and put the manual require just to test IDE did not showed me option for env
and here is my directory tree
what causes that? i dont have a clue.
I don't think node (or typescript) knows how to import a .env file natively.
You probably want to install and configure the dotenv npm package
Then you just add this line to the file that boots your server:
require('dotenv').config()
And now your env vars are accessible at:
process.ENV.MY_VAR_HERE
Here's a decent article on environment variables and how a .env file works with them.

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.

Which files from Node.js app must be uploaded to a web-hosting?

Perhaps this is a silly question. It came out while I was learning how to set up a Node.js application for production on Ubuntu and digital ocean.
Let's say I have a simple data visualization app made in Node.js, using node modules such as express, page, axios, yo-yo, and browserify to compile my files.
I want to upload my app to a webhost that already exists.
This is the structure's app:
node_modules
public (app.js and app.css)
src (header, home, and footer folders)
views (index.pug file)
gulpfile.js
index.scss
package.json
server.js
Which files I need to upload in order to see my app as I see it in localhost?
You need to upload everything.
What Maximelian says is true if you're going to run npm install again on your server. The standard way of doing this is sync the project using git (you can find a .gitignore template for node.js here).
Once setup you'd do something like this on the server after making the commit locally, and pushing to your remote git repo:
git pull
npm install
npm start
If you were to just ftp the full working project including node_modules it would work just by running npm start. But the above method is what I'd recommend.
If I remember correctly everything except node_modules, if you did not customized them. (rewrite some behavior after module installation)

node.js require function not finding module

I have a server.js file that I downloaded from someone's website. The first line is: var express=require('express');
When I try to run this server with "node server.js" I get the following error: "Cannot find module 'express'." The express module is installed in the default node install location:
C:\Users\myname\node_modules\express\
I'm able to successfully run express by executing "node express.js" from the express install location in node_modules. I also tried copying over the express folder and file into my c:\node-testing\ directory where my server.js file is located but I still get the error. Any idea what the problem might be and how to fix?
You can set the NODE_PATH environment variable to tell nodejs to search other paths for globally installed modules that are not in the project directory.
See http://nodejs.org/api/modules.html#modules_loading_from_the_global_folders for details.
On Unix installations there are some built-in default locations, but on Windows, it appears you have to set this environment variable manually to support a global location.
FYI, if you want require to load a module from the project directory, then you have to use
require("./filename");
with the ./ in front of it. That's why it didn't work when you copied it to the project directory. node makes a distinction between loading from the project directory vs. loading from the node_modules directory below and thus requires a different syntax to specify which one you want. Express.js is also not a stand-alone module because it depends on a bunch of other modules so you could not copy only it. I'd recommend using the NODE_PATH option or install express into your project directory (it will end up in a node_modules sub-directory).
Node.js will only search for modules in from the current (and parent) directories. Unlike npm, Node has no concept of "global" modules.
You need to run npm install to install your modules into the directory containing your code.

Resources