Search code examples
npmyarnpkgpackage.json

Different folders for npm and yarn


I coded a web application for which I use yarn for managing CSS and JavaScript dependencies. I decided to start using tools like parcel, sass, typescript, etc which I added as dev dependencies in my package.json file.

The package.json file is in the root folder, but my yarn-installed dependencies are in the /public/vendor/ folder, because anything outside the /public/ folder is not directly accessible to users. So I can't link the libraries in the /node_modules/ folder to my HTML.

Here is the content of my /.yarnrc file:

--modules-folder ./public/vendor --ignore-optional --production=true

The problem is that even though yarn will create its own yarn.lock file, it takes into account the contents of the package-lock.json file. So if a dependency has already been installed by npm, it won't be installed by yarn unless I explicitly declare it to be installed (meaning it works for dependencies, but not for dependency dependencies).


Solution

  • Using two different packages managers was a bad design idea.

    Instead, I edited my .htaccess file, in order to link the path /public/vendor to the /node_modules directory:

    RewriteEngine on
    
    RewriteCond %{ENV:REDIRECT_STATUS} 200
    RewriteRule .* - [L]
    
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1,E=REL_URI:/%2]
    
    RewriteCond %{ENV:REL_URI} ^/public/vendor/ [OR]
    RewriteCond %{REQUEST_URI} ^/public/vendor/
    RewriteCond %{REQUEST_URI} !.*\.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule public/vendor/(.*)$ %{ENV:BASE}/node_modules/$1 [L]
    
    RewriteCond %{ENV:REL_URI} ^/public/ [OR]
    RewriteCond %{REQUEST_URI} ^/public/
    RewriteCond %{REQUEST_URI} !.*\.php
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]
    
    RewriteRule .? %{ENV:BASE}/index.php [L]