Subscribe on changes!

libraries that import relative path in node_modules

avatar
Apr 29th 2020

Curious how to import modules that have relative paths to was trying to import a node_module package: e.g. https://unpkg.com/browse/swrv@0.3.0/esm/index.js

[vite] Error while resolving node_modules with id "lib/cache.js":
{ Error: Cannot find module 'lib/package.json'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:668:15)
    at resolveFileName (/Users/darrenjennings/source/repos/vite-sup/node_modules/resolve-from/index.js:29:39)
    at resolveFrom (/Users/darrenjennings/source/repos/vite-sup/node_modules/resolve-from/index.js:43:9)
    at Object.module.exports [as default] (/Users/darrenjennings/source/repos/vite-sup/node_modules/resolve-from/index.js:46:47)
    at app.use (/Users/darrenjennings/source/repos/vite-sup/node_modules/vite/dist/serverPluginModules.js:143:51) code: 'MODULE_NOT_FOUND' }
[vite] Error while resolving node_modules with id "use-swrv":
{ Error: Cannot find module 'use-swrv/package.json'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:668:15)
    at resolveFileName (/Users/darrenjennings/source/repos/vite-sup/node_modules/resolve-from/index.js:29:39)
    at resolveFrom (/Users/darrenjennings/source/repos/vite-sup/node_modules/resolve-from/index.js:43:9)
    at Object.module.exports [as default] (/Users/darrenjennings/source/repos/vite-sup/node_modules/resolve-from/index.js:46:47)
    at app.use (/Users/darrenjennings/source/repos/vite-sup/node_modules/vite/dist/serverPluginModules.js:143:51) code: 'MODULE_NOT_FOUND' }```
avatar
Apr 29th 2020

I tried import like this...

import something from 'https://unpkg.com/swrv@0.3.0/esm/index.js'

got error too

[vite] Error while resolving node_modules with id "https://unpkg.com/swrv@0.3.0/esm/index.js":
{ Error: Cannot find module 'https:/package.json'

workaround for now maybe snowpack... ?

avatar
Apr 29th 2020

@ais-one to be clear, I'm not importing from unpkg. I am importing from my local node_modules after yarn add swrv to a new vite project, and then using it here: import useSWRV from 'swrv'. The unpkg url is just for reference so you can see that the file has relative path imports.

avatar
Apr 29th 2020

ok... btw, which version of vite are you using? I am on 0.5.1

avatar
Apr 29th 2020

v0.5.3

avatar
Apr 29th 2020

would 0.6.0 help?

fix resolving scoped packages and relative imports inside node_modules (#15) (78ae1b7)

avatar
Apr 29th 2020

yes @ais-one thank you! This particular library swrv needs to be patched for vue 3, but a few line changes got it working in vite for me. 🎉