Is there a way to reference environment variables for an npm package from a consumer app? - build

I am creating a package on npm and for simplicity reasons I am trying to use environment variables in my npm package ( since user secret is needed ) that would be defined by a consumer app. But I seem to run into build issues when using the created package.
The structure looks something like this:
My demo app
|
|-----My npm package
|
|-----Other packages for my demo app
...
I have my npm package ( on npm ) and a demo app to showcase how the package works. I have environment variables used in the package, but I want to define them in my demo app.
In my package I am referencing the environment variables as such:
// My package
// auth-config.js
const config = {
variable1: import.meta.env['PUBLIC_VITE_VARIABLE_1'],
variable2: import.meta.env['PUBLIC_VITE_VARIABLE_2'],
variable3: import.meta.env['PUBLIC_VITE_VARIABLE_3']
};
export default config;
And later using them in my package code:
// My package
// auth-service.ts
const user = await getUser({
variable1: config.variable1,
variable2: config.variable2,
variable3: config.variable3
});
Then I have them defined in my demo app:
// Demo app
// .env
PUBLIC_VITE_VARIABLE_1=some-value
PUBLIC_VITE_VARIABLE_2=some-value-2
PUBLIC_VITE_VARIABLE_3=some-value-3
Then, when building ( npm run build ) my demo application, I am getting the following errors:
TypeError: Cannot read properties of undefined (reading 'PUBLIC_VITE_VARIABLE_1')
at file:///C:/Users/user1/Namu/login-with-auth/node_modules/my-app/auth0/auth-config.js:2:28
However, the dev build works completely fine. And it uses the variables successfully as well.
My configuration for both the package and the demo app is:
Svelte + Svelte Kit
Vite
Typescript
I have tried using different approaches to import environment variables ( process.env instead of import.meta.env ) inside the package. I have also tried updating the svelte.config.js and vite.config.js with different paths to environment variables to try and reach the scope of the application that uses this package.
I am not sure where to go from here, since I can't find any definitive answer online and nothing seems to work so far.

I was able to solve the issue in the end. Keep in mind I am using svelte + svelte-kit + vite in both the package and the demo app using the package.
The fix goes like this:
Name the environment variables properly. Naming convention should follow what the bundler says, not what the framework says. In my case for vite it has to start with VITE_, so the environment variable would be VITE_VARIABLE_1. More info can be found Here.
Side note: this is also configurable, but I could not make the configurations work in my favour.
Using proper references in the package. For vite, the correct way to reference an env variable is by using import.meta.env['VITE_VARIABLE_1'].
Adding checks for undefined when using the variables in the package like so: import.meta.env ? import.meta.env['VITE_VARIABLE_1'] : ''.
This let's you avoid the build errors that I was running into, since at build time, import.meta.env is not defined.
And this is how the final files look like:
Where the environment variables are referenced:
// My package
// auth-config.js
const variable1 = import.meta.env ? import.meta.env['PUBLIC_VITE_VARIABLE_1'] : '';
const variable2 = import.meta.env ? import.meta.env['PUBLIC_VITE_VARIABLE_2'] : '';
const variable3 = import.meta.env ? import.meta.env['PUBLIC_VITE_VARIABLE_3'] : '';
const config = {
variable1,
variable2,
variable3
};
Where the environment variables are used:
// My package
// auth-service.ts
const user = await getUser({
variable1: config.variable1,
variable2: config.variable2,
variable3: config.variable3
});
Where the environment variables are defined:
// Demo app
// .env
PUBLIC_VITE_VARIABLE_1=some-value
PUBLIC_VITE_VARIABLE_2=some-value-2
PUBLIC_VITE_VARIABLE_3=some-value-3
You can also look at the insides of the created Package.
And the coresponding files: auth-config.js, auth-service.ts.

Related

Vendoring npm packages in deno

How does one vendor a npm package in deno?
import_map.json:
{
"imports": {
"lume/": "https://deno.land/x/lume#v1.12.1/",
}
}
Lume has some npm dependencies, like https://registry.npmjs.org/markdown-it/-/markdown-it-13.0.0.tgz.
deno.jsonc:
{
"importMap": "import_map.json",
}
dev_deps.ts:
export * as lume from "https://deno.land/x/lume#v1.12.1/mod.ts";
command:
$ deno vendor --force --unstable dev_deps.ts
# ...
Download https://registry.npmjs.org/markdown-it-attrs/-/markdown-it-attrs-4.1.3.tgz
# ...
thread 'main' panicked at 'Could not find local path
for npm:markdown-it-attrs#4.1.3', cli/tools/vendor/mappings.rs:138:11
I tried adding export * as ma from "npm:markdown-it-attrs"; to dev_depts.ts, but it did nothing.
I found the following issue on github.
Maybe this issue does have something to do with it.
I didn't find anything about how to resolve the problem in the official deno documentation and the lume documentation.
Unfortunately, currently you cannot use import_map in your Deno project if your goal is to publish a module that aims to be used in other applications, simply because you don't handle the way deno runtime will start.
From the application point of view, the deno run command cannot search every import_map configurations in your dependencies and handle them properly.
The import_map feature should be used only at end application level.
The fallback is to use by onvention a deps.ts source file to centralize all your dependencies.

Looking for documentation setup jest for unit test of admin plugin

my project was created with the swdc create-project ...
Is there a documentation, a tutorial or description for the right setup/configuration unit testing with JEST for custom plugin in administration?
This tutorial describes only how to write a test
But i think there must be a official setup documentation because of versions etc.
EDIT: a tutorial with code is now avialable
Using the suggested solution and execute the test, throws an configuration error:
● Test suite failed to run
Configuration error:
Could not locate module src/core/factory/module.factory mapped as:
undefined/src$1.
Please check your configuration for these entries:
{
"moduleNameMapper": {
"/^src(.*)$/": "undefined/src$1"
},
"resolver": undefined
}
...
Cause of error:
process.env.ADMIN_PATH not setted but required in %Project%/custom/plugins/%MyPlugin%/src/Resources/app/administration/node_modules/#shopware-ag/jest-preset-sw6-admin/jest-preset.js
My solution:
set process.env.ADMIN_PATH in %Project%/custom/plugins/%MyPlugin%/src/Resources/app/administration/jest.config.js
// jest.config.js
...
const { join, resolve } = require('path');
process.env.ADMIN_PATH = resolve('../../../../../../../src/Administration/Resources/app/administration');
...
I think it is easiest to just copy and adapt from a plugin that already has jest tests set up. Look at the administration directory for SwagPayPal for example. Copy the dependency and script sections from their package.json. Also copy the entire jest.config.js. Then within the administration directory of your plugin you should be able to npm install followed by npm run unit or npm run unit-watch and it should find *.spec.js files within the test sub-directory.

How to use a native node.js addon in React

I have a react project in which I would like to use this native node.js addon, which is a wrapper for a C++ SDK.
I've successfully used this module in the past within an Electron project, and can run the sample successfully with node as well.
My question is how I would be able to use this in React, or write my own React friendly solution using the C++ SDK.
I've tried to clone the module and place it under a lib folder in my react project, I ran npm install within that folder to install it's dependencies and tried to run the included example directly with node. This went fine. But using the sample directly from within my React app fails with the following error code ts3client.on is not a function.
So it passes the line were the library is required with var ts3client = require('../../lib/node-ts3sdk-client/api.js'); but that's the furthest I managed to get. I could play around a little bit more, but would like to get some opinions on what might be the best approach here.
Edit
As requested I have added a small example to reproduce the issue I am facing.
Create a simple react app
npm init react-app so-node-addon-react
Clone this repository under src/lib/
git clone https://github.com/svenpaulsen/node-ts3sdk-client.git
Install the module's dependencies
cd node-ts3sdk-client
npm install
Expose some part of the client example by wrapping the try-catch block starting from line 160 in an exported function like so
export function connect() {
try { ... }
catch { ... }
}
Call the function from your project's App.tsx
import { connect } from './lib/node-ts3sdk-client/examples/client_minimal'
...
connect()
Run the project
npm start
This should result in the following error: ts3client.on is not a function

Angular 4 Unit Test cannot find name alert, confirm, event

Running ng test in angular-cli with Angular 4.0.0 gives errors like:
Cannot find name 'alert'
Cannot find name 'confirm'
Cannot find name 'Event'
This wasn't happening with Angular 2. Looks like something is missing here but what is it?
ng serve and build work fine.
In my case this was solved by adding "dom" to the "lib" array in tsconfig. I had a tsconfig.app.json that was used for building the app normally, and a tsconfig.spec.json that was just used for unit testing. This latter one was missing "dom".
{
"compilerOptions": {
...
"lib": [
"es2016",
"dom"
],
...
}
I was able to solve this by changing typings.d.ts according to latest cli documentation.
src/typings.d.ts:
Old:
/* SystemJS module definition */
declare var module: {
id: string;
};
New:
/* SystemJS module definition */
declare var module: NodeModule;
interface NodeModule {
id: string;
}
To fix the issue I have generated a new project using latest #angular/cli and then compared and updated all of my tsconfg files to the current format.

How to use third party npm packages with ember cli app

EDIT: this is actually about any npm package which is not designed to play along with ember. In my case, I tried to make crypto-js work, but it seems to be always the same trouble with any npm package not specially designed for ember cli.
I want to use cryptoJS in my ember app, which I'm currently refactoring with ember cli, but I'm having a lot of trouble importing all the third party packages and libraries I'm already using, like for example cryptoJS.
CryptoJS at least has a package for npm, I don't even want to think about what happens if some of my included libraries don't have a package...
Am I just missing the point in the documentation of ember-cli or is it really not described how to import other npm packages and also how to inlcude non-package libraries properly to keep them under version control and dependency control?
If I follow the description of the crypto-js package manual:
var CryptoJS = require("crypto-js");
console.log(CryptoJS.HmacSHA1("Message", "Key"));
I get and error in my ember build
utils/customauthorizer.js: line 1, col 16, 'require' is not defined.
Thanks for any help on this, I'm very excited about the ember cli project, but importing my existing ember app has been quite painful so far...
EDIT:
Just importing unfortunately does not work.
import CryptoJS from 'crypto-js';
throws during the build
daily#dev1:~/VMD$ ember build
version: 0.1.2
Build failed.
File: vmd/utils/customauthorizer.js
ENOENT, no such file or directory '/home/daily/VMD/tmp/tree_merger-tmp_dest_dir-F7mfDQyP.tmp/crypto-js.js'
Error: ENOENT, no such file or directory '/home/daily/VMD/tmp/tree_merger-tmp_dest_dir-F7mfDQyP.tmp/crypto-js.js'
at Error (native)
at Object.fs.statSync (fs.js:721:18)
at addModule (/home/daily/VMD/node_modules/ember-cli/node_modules/broccoli-es6-concatenator/index.js:84:46)
at addModule (/home/daily/VMD/node_modules/ember-cli/node_modules/broccoli-es6-concatenator/index.js:133:9)
at addModule (/home/daily/VMD/node_modules/ember-cli/node_modules/broccoli-es6-concatenator/index.js:133:9)
at /home/daily/VMD/node_modules/ember-cli/node_modules/broccoli-es6-concatenator/index.js:59:7
at $$$internal$$tryCatch (/home/daily/VMD/node_modules/ember-cli/node_modules/rsvp/dist/rsvp.js:470:16)
at $$$internal$$invokeCallback (/home/daily/VMD/node_modules/ember-cli/node_modules/rsvp/dist/rsvp.js:482:17)
at $$$internal$$publish (/home/daily/VMD/node_modules/ember-cli/node_modules/rsvp/dist/rsvp.js:453:11)
at $$rsvp$asap$$flush (/home/daily/VMD/node_modules/ember-cli/node_modules/rsvp/dist/rsvp.js:1531:9)
The easiest and recommended answer is to use ember-browserify. (as support for bower packages will be removed in the future.)
This is an example for using the npm package dexie within an Ember CLI app.
Install browserify: npm install ember-browserify --save-dev
Install dexie (or whatever module you need): npm install dexie --save-dev
Import the module like this: import Dexie from 'npm:dexie';
UPDATE (April 2021):
ember-browserify has now been is deprecated in favor of either ember-auto-import or ember-cli-cjs-transform
(see the deprecation warning at the top of ember-browserify)
UPDATE: I got this to work much better and straight forward! Thanks to the comment of #j_mcnally!
Will leave the first answer down there so everyone can see what trouble I was coming from :)
What I did:
bower install crypto-js=svn+http://crypto-js.googlecode.com/svn/#~3.1.2 --save
In my file Brocfile.js I could just do app.import('bower_components/crypto-js/build/rollups/hmac-md5.js');
No manual downloading or moving files, just managing a dependency, much better solution!
But honestly, it was still a lot of vodoo! Until I found the documentation... sweet: http://bower.io/docs/api/#install
OLD approach
I got this to work, but I can not tell how pretty or correct that approach is. Including third party packages or libraries with ember cli is pretty far away from straight forward or self explaining.
The ressources which led me to my working solution were:
how to use third party javascript from ember-cli route
https://github.com/stefanpenner/ember-cli/issues/757
The following steps I took to get it working:
I manually downloaded the library https://code.google.com/p/crypto-js/downloads/detail?name=CryptoJS%20v3.1.2.zip and unziped it
I manually created a directory in my vendor directory: mkdir vendor/crypto-js
I appended app.import('vendor/crypto-js/hmac-md5.js'); to the Brocfile.js file
I added "CryptoJS" to the "predef" key in the .jshintrc file
Then the build worked and I could eventually use the library.
Sadly I didn't get the npm package to work! I had to manually download the zip file, unzip it and move it to the correct location and if the version changes, it's not under any version/dependency control... I will not mark this as an answer, since it does not satisfy me at all, but at least I wanted to share what I did to make it work for me.
As Timm describes, using browserify gets the code injected into your ember app. However, I was having trouble actually using the injected module. In order to do that I had to actually create the module with New before I could use it:
In order to import an NPM module.
1) install browserify:
npm install ember-browserify --save-dev
2) install your modele:
npm install my-module --save-dev
3) Import your module into your ember file of interest (app/controller/post.js):
import Module from 'npm:my-module';
4) use the module from within your code by creating the module with New:
var output = new Module(var1, var2, etc.);
even though this is an old thread thought I would contribute as I spent a while doing this. The specific package I was trying to link to ember was 'd3plus' and had to do a variety of things to get it to work.
npm install ember-browserify --save-dev
npm install d3plus --save-dev
ember install ember-cli-coffeescript
npm install --save-dev coffeeify coffeescript
then in your component do
import d3plus from 'npm:d3plus';
For a long time I was getting runtime errors when it was searching for the coffescript and figured this would be helpful for people specifically looking for d3plus.
As stated by Pablo Morra on a comment of the simplabs' post "Using npm libraries in Ember CLI", third party npm modules can be imported on Ember.js from version 2.15 directly without the need of addons or wrappers:
https://www.emberjs.com/blog/2017/09/01/ember-2-15-released.html#toc_app-import-files-within-node_modules
Unfortunately documentation is still on work and it doesn't say that npm modules can be imported, only bower and vendor ones:
https://github.com/emberjs/guides/issues/2017
https://guides.emberjs.com/v3.0.0/addons-and-dependencies/managing-dependencies/
I've gotten 2 solutions to import third party npm modules directly on Ember.js from the Ember CLI documentation about managing dependencies, although it's also out-of-date and says that npm modules can't be imported, only bower and vendor ones:
npm module as Standard Anonymous AMD Asset
https://ember-cli.com/managing-dependencies#standard-anonymous-amd-asset
AMD: Asynchronous Module Definition
I prefer and use this way because it avoids global variables and follows the import convention of Ember.js.
ember-cli-build.js:
app.import('node_modules/ic-ajax/dist/amd/main.js', {
using: [
{ transformation: 'amd', as: 'ic-ajax' }
]
});
amd is the type of transformation applied, and ic-ajax is the module name to be used when it's imported on a javascript file.
on Ember.js javascript file (router, component...):
import raw from 'ic-ajax';
// ...
icAjaxRaw( /* ... */ );
raw is a module exported by ic-ajax.
That's the way it worked for me although the Ember CLI documentation shows the import other way that didn't work for me, maybe because of the specific package I was importing:
import { raw as icAjaxRaw } from 'ic-ajax';
//...
icAjaxRaw( /* ... */ );
npm module as global variable
https://ember-cli.com/managing-dependencies#standard-non-amd-asset
ember-cli-build.js:
app.import('node_modules/moment/moment.js');
on Ember.js javascript file (router, component...):
/* global moment */
// No import for moment, it's a global called `moment`
// ...
var day = moment('Dec 25, 1995');
/* global moment */ is an annotation for ESLint not to show an error when building the project because moment() is not defined in the file.
npm module as Standard Named AMD Asset
https://ember-cli.com/managing-dependencies#standard-named-amd-asset
Ember CLI also shows a third option that didn't work for me, maybe because of the specific package I was importing:
ember-cli-build.js:
app.import('node_modules/ic-ajax/dist/named-amd/main.js');
on Ember.js javascript file (router, component...):
import { raw as icAjaxRaw } from 'ic-ajax';
//...
icAjaxRaw( /* ... */ );
npm module as AMD JavaScript modules
https://guides.emberjs.com/v3.0.0/addons-and-dependencies/managing-dependencies/#toc_amd-javascript-modules
The way described on Ember.js documentation about Managing Dependencies didn't work for me either, maybe because of the specific package I was importing:
ember-cli-build.js:
app.import('node_modules/ic-ajax/dist/named-amd/main.js', {
exports: {
'ic-ajax': [
'default',
'defineFixture',
'lookupFixture',
'raw',
'request'
]
}
});
on Ember.js javascript file (router, component...):
import { raw as icAjaxRaw } from 'ic-ajax';
//...
icAjaxRaw( /* ... */ );