rygo18 rygo18 - 5 months ago 49
Node.js Question

Node local folder import

I have a react native project which has a "common" folder that contains a number of folders and files.
I'm importing these in multiple places in my base code. However, what I currently have to do is individually import them e.g.

import comp1 from '../../Common/field/comp1';
import comp2 from '../../Common/field/comp2';


What I'd like to be able to do is the leave the "common" folder as is and simply export them all from a central file, preferably with an easy to read alias. Hopefully so that my "common" folder would be seamless with the standard external node dependancy imports e.g.

import React, { Component } from 'react';
import { comp1, comp2 } from 'common';


I'm aware of npm private node modules and some other solutions for individual files but I can't seem to find any recommended way of doing this.
Perhaps I'm not searching for the right feature or I'm over simplifying my issue but it would seem to me that this should be a very common thing for developers to want without having to go through npm.

Any help would be appreciated.

Answer Source

So what I've done is just created my own folder and added it as a node module with the standard package.json, index.js and so on. This allows me to do what I want for the time being. Not sure how maintainable it is going forward so might need to add it as a private repo on bitbucket or something.