Skip to main content

Opsi konfigurasi Node-Modules dengan pnpm

· One min read

Ada banyak cara untuk membuat direktori node_modules. Your goal must be to create the most strict one but if that is not possible, there are options to make a loose node_modules as well.

Pengaturan standar

Secara standar, pnpm v5 akan membuat node_modules semi-ketat. Semi-ketat berarti aplikasi Anda hanya dapat meminta paket yang ditambahkan sebagai dependensi ke package.json (dengan beberapa pengecualian). Namun, dependensi Anda akan dapat mengakses paket apa pun.

Konfigurasi standar terlihat seperti ini:

; All packages are hoisted to node_modules/.pnpm/node_modules
hoist-pattern[]=*

; All types are hoisted to the root in order to make TypeScript happy
public-hoist-pattern[]=*types*

; All ESLint-related packages are hoisted to the root as well
public-hoist-pattern[]=*eslint*

Plug'n'Play. The strictest configuration

pnpm supports Yarn's Plug'n'Play since v5.9. With PnP, both your application and the dependencies of your application will have access only to their declared dependencies. This is even stricter then setting hoist=false because inside a monorepo, you're application will not be able to access even the dependencies of the root project.

To use Plug'n'Play, set these settings:

node-linker=pnp
symlink=false

A strict, traditional modules directory

If you are not ready to use PnP yet, you can still be strict and only allow packages to access their own dependencies by setting the hoist configuration to false:

hoist=false

However, if some of your dependencies are trying to access packages that they don't have in dependencies, you have two options:

  1. Create a pnpmfile.js and use a hook to add the missing dependency to the package's manifest.

  2. Add a pattern to the hoist-pattern setting. For instance, if the not found module is babel-core, add the following setting to .npmrc:

    hoist-pattern[]=babel-core

The worst case - hoisting to the root

Some tools might not work even with the default configuration of pnpm, which hoists everything to the root of the virtual store and some packages to the root. In this case, you can hoist either everything or a subset of dependencies to the root of the modules directory.

Hoisting everything to the the root of node_modules:

shamefully-hoist=true

Hoisting only packages that match a pattern:

public-hoist-pattern[]=babel-*