Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Can't use npm package in Typescript and Webpack project #93

Closed
tzorzan opened this issue Apr 23, 2019 · 18 comments · Fixed by #125
Closed

Can't use npm package in Typescript and Webpack project #93

tzorzan opened this issue Apr 23, 2019 · 18 comments · Fixed by #125
Assignees

Comments

@tzorzan
Copy link

tzorzan commented Apr 23, 2019

Hi,
I'm trying to use the library in my project, based on Typescript targeting es6 and using commonjs modules. The final output is produced using Webpack and targeting the web platform.

I've installed the dependency with npm:

npm install --save jose-jwe-jws

and try to reference it in my code:

import {Jose} from "jose-jwe-jws";
...
let cryptographer = new Jose.WebCryptographer();
...

Everything compiles good, but when I run the result of webpack in my page, I've got an error:

Uncaught ReferenceError: Jose is not defined

I've got the same error trying to run tests with Mocha... :-(
What I'm missing?

Thank You

@tzorzan tzorzan changed the title Can't use jose-jwe-jws with typescript and webpack Can't use npm package in Typescript and Webpack project Apr 23, 2019
@wfortin
Copy link

wfortin commented Apr 29, 2019

Having the same issue while updating from v0.11.1 to v.1.1.3.

Trying to import in a few different ways:

import {Jose} from "jose-jwe-jws";
import * as Jose from "jose-jwe-jws";
import Jose from "jose-jwe-jws";

All turn out with Jose beign an empty object.

@wfortin
Copy link

wfortin commented Apr 29, 2019

v0.11.1 works when I import and use as such:

import * as Jose from 'jose-jwe-jws'
const cryptographer = new Jose.Jose.WebCryptographer()

I need to update to the latest version since snyk.io has found vulnerabilities in lodash.merge@4.6.1 in v0.11.1

@wfortin
Copy link

wfortin commented Apr 29, 2019

Seems that the issue lies in the webpack configuration:

  output: {
    filename: 'jose.js',
    library:'Jose',
    libraryTarget: 'var'

using

    libraryTarget: 'umd' | 'commonjs'

Pull #89 does some changes in regards to this. Can this be looked at, right now this configuration doesn't allow using the library as a node module or a ES6 module.
Thanks.

@alokmenghrajani
Copy link
Contributor

#89 has conflicts. If it builds, I'm happy to merge it.

@wfortin
Copy link

wfortin commented Apr 29, 2019

Let me suggest a PR for this.

@tzorzan
Copy link
Author

tzorzan commented Apr 30, 2019

Thank you for your feedback. I've already headed to this configuration:

    libraryTarget: 'umd' | 'commonjs'

and also tried:

Pull #89

installing npm package from Mischala/js-jose#exportCjsPackage but neither works.
I'll try to investigate further.

Thanks.

@wfortin
Copy link

wfortin commented Apr 30, 2019

Yeah, my attempts have proved that we would need to tweak the definition file... And I end up having issues with decryption on that new package. Let me know if you end up with a solution.

@wfortin
Copy link

wfortin commented Apr 30, 2019

I have resorted to use version 0.1.6 where I can use it like this in my TS project.

import * as Jose from 'jose-jwe-jws'

const cryptographer = new Jose.Jose.WebCryptographer()

@damnhandy
Copy link

We're seeing the same issue here. Just like @wfortin, only 0.1.6. works were as 0.1.7 fails.

mbyczkowski added a commit that referenced this issue Jun 30, 2019
Also, added jest testing framework and first unit test to improve
coverage.
mbyczkowski added a commit that referenced this issue Jul 22, 2019
Also, added jest testing framework and first unit test to improve
coverage.
mbyczkowski added a commit that referenced this issue Jul 23, 2019
@mbyczkowski
Copy link
Contributor

@damnhandy @wfortin this should be fixed in 0.2.0

@wfortin
Copy link

wfortin commented Jul 29, 2019

@mbyczkowski care to give an example with 0.2.0?

I still end up with an object that contains almost nothing when importing in all these different ways:

import {Jose} from "jose-jwe-jws";
import * as Jose from "jose-jwe-jws";
import Jose from "jose-jwe-jws";

Seems that the TS definitions are fixed now, but at runtime I get:

import * as Jose from "jose-jwe-jws";

image

import {Jose} from "jose-jwe-jws";

image

import Jose from "jose-jwe-jws";

I get the error Module '"jose-jwe-jws"' has no default export.

So it seems the webpack configuration is still incorrect, as the object is defined but the exported methods are not in that object.
The added test in here 60a948c checks if the object is not empty, which is true, but it doesn't contain the methods/keys like WebCryptographer, Utils, etc.

So I'm still sticking to 0.1.6 as mentionned here

@wfortin
Copy link

wfortin commented Jul 29, 2019

I've added a codesandbox to replicate the issue here:

v0.2.0 (not working as described in my last comment) : https://codesandbox.io/s/hopeful-mirzakhani-2m2bz?fontsize=14

v0.1.6 (working) : https://codesandbox.io/s/jose-jwe-jws020-0w7y9?fontsize=14

Hope this helps!

@mbyczkowski mbyczkowski self-assigned this Jul 29, 2019
@mbyczkowski
Copy link
Contributor

@wfortin thanks for the repro. I'm going to look into this.

mbyczkowski added a commit that referenced this issue Aug 8, 2019
This should address issues seen in #93.
mbyczkowski added a commit that referenced this issue Aug 8, 2019
This should address issues seen in #93.
@mbyczkowski
Copy link
Contributor

mbyczkowski commented Aug 9, 2019

@wfortin I thought I had a fix for this in #116, but after grabbing your repro, I can still see the issue in 0.2.1. I wonder if this is packaging problem now (babel, webpack, parcel etc.) 🤔

dkowis added a commit to dkowis/js-jose that referenced this issue Aug 29, 2019
Changed the library type to UMD.
> libraryTarget: 'umd' - This exposes your library under all the module
definitions, allowing it to work with CommonJS, AMD and as global
variable.

from https://webpack.js.org/configuration/output/#outputlibrarytarget
@dkowis
Copy link
Contributor

dkowis commented Aug 29, 2019

I've submitted a pull request that allows me to import it in my Angular 8 application, satisfying the Typescript + Webpack problem. UMD library packaging should allow it to continue to be used as a var, but also an importable module.

@peitschie
Copy link

Anyone else still struggling with this, I was able to workaround the issue by directly importing the non-minified JS files. All the contained paths there were relative, so webpack was able to figure out how to wire this all together ok from that.

E.g., I've made a file called js-jose.ts in my project, with this content:

/// <reference types="jose-jwe-jws" />
(window as any).Jose = require("jose-jwe-jws/lib/jose-core.js");

Once that is done, I simply import that somewhere, and things work!

mbyczkowski pushed a commit that referenced this issue Dec 27, 2019
* Fix #93. I can use the NPM package in a Typescript/Webpack project

Changed the library type to UMD.
> libraryTarget: 'umd' - This exposes your library under all the module
definitions, allowing it to work with CommonJS, AMD and as global
variable.

from https://webpack.js.org/configuration/output/#outputlibrarytarget
@mbelge
Copy link

mbelge commented Jan 13, 2020

Indeed it works as expected with this last commit, when do you plan a new release with corresponding npm package as well ? Thx.

@mbyczkowski
Copy link
Contributor

mbyczkowski commented Jan 13, 2020

I have just released 0.2.2. @mbelge feel free to re-open the issue if you're still seeing the problem in the latest release.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants