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!
Ben Halpern DEV.TO FOUNDER

Hey there, we see you aren't signed in.

Please consider creating an account on dev.to. It literally takes a few seconds and we'd appreciate the support so much. ❤️

Classic DEV Post from Apr 10

My Productivity Boosters — A random collection of tricks and tools — What are yours?

I love working on projects. I’ve been working on all kinds of projects for 12 y...

READ POST
Follow @fabrik42 to see more of their posts in your feed.