PuffedRiceCrackers
PuffedRiceCrackers

Reputation: 785

How to override npm package's package.json

A npm package that I use has wrong path in its package.json. It is

  "main": "./htmldiff.js",

But it has to be this to work properly.

  "main": "./src/htmldiff.js",

I guess maybe I can commit and download un-merged version.. but is there any easier way that only touches local files?

Upvotes: 0

Views: 1391

Answers (2)

lastr2d2
lastr2d2

Reputation: 3968

You can always skip the main target and import directly from node_modules folder like this

import xx from './node_modules/htmldiff/src/htmldiff'

If you are trying to avoid a relative path like ../../../, a resolve alias from webpack or babel could help.

  const path = require('path');
  resolve: {
    alias: {
      '@': path.resolve('node_modules'),
    }
  }

And then you can do it like

import xx from '@/htmldiff/src/htmldiff'

Upvotes: 1

Emilio Grisolía
Emilio Grisolía

Reputation: 1213

You can override the file content. Just read package.json. Parse the content as an object. Change the object. Stringify it and then write it. I made this sync for simplicity. You may want to use the async version. Also, writeFile overrides the file content by default.

   'use strict';

   const 
          fs      = require('fs'),
          content = fs.readFileSync('package.json', 'utf8'),
          parsed  = JSON.parse(content);
    
    parsed.main = "./src/htmldiff.js";
    
    fs.writeFileSync('package.json', JSON.stringify(parsed));

Upvotes: 1

Related Questions