Get dependencies written in package.json from JavaScript code

This is just a memo about how to refer dependencies written in package.json from JavaScript code.

import pkg from './package.json';

const dependencies = Object.keys(pkg.dependencies);
const devDependencies = Object.keys(pkg.devDependencies);
const peerDependencies = Object.keys(pkg.peerDependencies);

It would be useful in case of specifying externals in rollup.config.js.

{
  external: Object.keys(pkg.dependencies)
  ...
}
Did you find this post useful? Show some love!
DISCUSSION
Classic DEV Post from Jul 22

The best book for learning backend design patterns

Hello

Masato Ohba
Software Engineer

Do you write code almost every day?

Join dev.to ❀️