'npm package.json aliases like webpack

i am trying to alias a module however i am not sure how to do that with package.json

in webpack you would do something like this:

module.exports = {
  //...
  resolve: {
    alias: {
      'pixi.js': 'pixi.js-legacy'
    }
  }
};

But what is the equivalent without webpack?



Solution 1:[1]

There is a npm package for this: module-alias.

After installing it you can add your aliases to the package.json, like so:

"_moduleAliases": {
  "@root"      : ".", // Application's root
  "@deep"      : "src/some/very/deep/directory/or/file",
  "@my_module" : "lib/some-file.js",
  "something"  : "src/foo", // Or without @. Actually, it could be any string
}

Make sure to add this line at the top of your app's main file:

require('module-alias/register');

You should only use this in final products (and not packages you intend to publish in npm or use elsewhere) - it modifies the behavior of require.

Solution 2:[2]

Since NPM Version 6.9 it is supported without installing any additional packages:

npm i aliasName@npm:packageToInstall

???

// package.json
"dependencies": {
    "aliasName": "npm:packageToInstall@^1.6.1"
}

The idea seems to be that npm: is a URI-like scheme in a dependency version specifier.

Usage:

const alias = require( 'aliasName' );

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mat Sz
Solution 2