Tuesday 25 June 2019

How to import from project root (instead of package root)

I'm creating an npm package and I want the user to be able to set configuration files in the root of their project. An example structure might be:- UserProject/ - index.js - config.js - package.json - node_modules/ - package/ - index.js - package.json Inside UserProject/node_modules/package/index.js, I want to be able to import the file UserProject/config.js.I know I could use import config from "../../config.js" but that doesn't seem like a good way to do it.I know many other npm packages are able to do this so I'm wondering how they achieve it. A few examples that do this:Next.js is able to read ./pages/ and ./next.config.js from the project rootJest is able to read ./jest.config.js from the project rootESLint is able to read ./.eslintrc.js from the project rootPrettier is able to read ./.prettierrc.js from the project rootTypescript is able to read ./tsconfig.json from the project rootBabel is able to read ./.babelrc from the project rootI've tried creating a StackOverflow post but got no replies, I've tried googling for it but all I get are results on how to do basic es6 imports, and I've tried looking at the source codes for the above examples but I can't locate the sections on how they do it.Any tips?

Submitted June 26, 2019 at 06:38AM by stupid-sexy-jake

No comments:

Post a Comment