cjs file is resolved instead of esm
smarlhens opened this issue Β· comments
Samuel MARLHENS commented
Hi there ππ» ,
Thanks for your amazing job on this library.
Describe the bug
Today I encountered an small issue related to cjs/esm export using in a Vue 3 / Vite / vitest project while trying to run my tests:
Error: require() of ES Module <path/to/project>/node_modules/d3-shape/src/index.js from <path/to/project>/node_modules/rete-connection-path-plugin/rete-connection-path-plugin.common.js not supported.
Instead change the require of index.js in <path/to/project>/node_modules/rete-connection-path-plugin/rete-connection-path-plugin.common.js to a dynamic import() which is available in all CommonJS modules.
It seems to be be related to an incorrect declaration in the package.json file, as mentioned here: vitest-dev/vitest#2742
Workaround
using Vite it's possible to resolve dependencies using an alias inside vite.config.ts
:
export default {
resolve: {
alias: [{ find: /^rete-connection-path-plugin$/, replacement: 'rete-connection-path-plugin/rete-connection-path-plugin.esm.js' }],
},
}
System info
System:
OS: Linux 5.19 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish)
CPU: (16) x64 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz
Memory: 10.07 GB / 31.07 GB
Container: Yes
Shell: 5.8.1 - /usr/bin/zsh
Binaries:
Node: 18.17.0 - /usr/bin/node
npm: 9.6.7 - /usr/bin/npm
Browsers:
Chrome: 115.0.5790.170
npmPackages:
rete: 2.0.1
rete-area-plugin: 2.0.0
rete-auto-arrange-plugin: 2.0.0
rete-connection-path-plugin: 2.0.0
rete-minimap-plugin: 2.0.0
rete-readonly-plugin: 2.0.0
rete-render-utils: 2.0.1
rete-vue-plugin: 2.0.2
Solution
I guess adding these lines to the package.json should resolve the error (seems to work in my project without the workaround):
"exports": {
".": {
"import": "./rete-connection-path-plugin.esm.js",
"require": "./rete-connection-path-plugin.common.js",
"types": "./_types/index.d.ts"
}
},
Thanks in advance for your help on this.
I wish you a great day βοΈ
Best regards,
Samuel