kontent-ai / delivery-sdk-js

Kontent Delivery SDK for Javascript
https://kontent.ai
MIT License
50 stars 34 forks source link

`Module not found: Can't resolve 'tslib'` when importing `transformImageUrl` #347

Closed tommarshall closed 2 years ago

tommarshall commented 2 years ago

Brief bug description

Module not found: Can't resolve 'tslib' in './node_modules/@kentico/kontent-core/dist/es5/helpers'

When importing transformImageUrl from @kentico/kontent-delivery to build image transformation URLs, e.g. within a custom loader for Next/Image.

Repro steps

  1. Create a new Next.js project using cms-kontent example from the Next.js repo.
yarn create next-app --example https://github.com/vercel/next.js/tree/82a9d21/examples/cms-kontent tslib-import-error-demo
  1. Set up the project as per the Next.js example README instructions.
  2. Run yarn build to build the project.

Expected behaviour

The build should succeed.

Actual behaviour

The build fails with the following error:

$ next build
info  - Loaded env from /Users/tom/projects/tmp/.env.local
info  - Checking validity of types
warn  - No ESLint configuration detected. Run next lint to begin setup
info  - Creating an optimized production build
Failed to compile.

./node_modules/@kentico/kontent-core/dist/es5/helpers/enum.helper.js
Module not found: Can't resolve 'tslib' in '/Users/tom/projects/next-kontent-on-demand-isr-demo/node_modules/@kentico/kontent-core/dist/es5/helpers'

Import trace for requested module:
./node_modules/@kentico/kontent-core/dist/es5/helpers/index.js
./node_modules/@kentico/kontent-core/dist/es5/index.js
./node_modules/@kentico/kontent-delivery/dist/es6/client/delivery-client.js
./node_modules/@kentico/kontent-delivery/dist/es6/client/index.js
./node_modules/@kentico/kontent-delivery/dist/es6/index.js
./components/image.js
./components/avatar.js
./components/hero-post.js
./pages/index.js
...

Test environment

Additional context

Adding tslib as a dependency to the Next project (e.g. yarn add tslib) fixes the error.

tslib is present in the @kentico/kontent-delivery package-lock.json, but it's listed as a dev dependency.

Assuming transformImageUrl is part of the public API, which it appears to be as it's documented, then I think tslib needs to be moved from a dev dependency to a standard dependency.

That's a straightforward fix that I can open a PR for, but given the error is occurring within a module of kontent-core, I'm not sure whether the right place to do that is in @kentico/kontent-delivery or downstream in @kentico/kontent-core. Guidance would be appreciated here...

To date I think this has gone unnoticed as the majority of Kontent projects are likely importing other packages that depend on tslib, e.g. Kontent Starter Corporate - Next.js, which includes tslib as a dependency of eslint-config-next (via @typescript-eslint/parser -> @typescript-eslint/typescript-estree -> tsutils -> tslib)

Enngage commented 2 years ago

Hi @tommarshall,

Thank you for the details and opening the issue here.

As far as I understand the tslib is just a runtime library for typescript and as such should not be part of the public API or be listed as a dependency of the project. I would assume that you would get similar error if you used something other than transformImageUrl, is that correct?

tslib is (or is not) needed depending on how your build process is set up. In your case the proper solution would just be to include it in your own devDependencies (again not dependencies because it's only needed for building your app) as you have already done ;)