Using experimental.externalDir in NextJS prevents the use of absolute imports in external libraries

In my monorepo setup with nextjs, lerna, and npm workspaces, the folder structure is as follows:

packages
    next-js-app
        pages
            index.tsx
        tsconfig.json
    ui-library
        src
            components
                 dropdown.tsx
            index.ts
            utils.ts
        tsconfig.json
package.json
lerna.json
tsconfig.json

I am trying to import the ui-library into the next-js-app like this:

// packages/next-js-app/pages/index.tsx
import { UiLibrary } from '@workspaceName/ui-library'

To achieve this, I added externalDir: true under the experimental key in the next.config.js within the next-js-app:

module.exports = {
  ...
  experimental: {
    externalDir: true
  }
  ...
}

Issue

The import works fine, but in the file

packages/ui-library/src/components/dropdown.tsx
, there is a line:

// packages/ui-library/src/components/dropdown.tsx
import { helperFunction } from 'utils'

This means I am trying to import helperFunction from packages/ui-library/src/utils.ts. When I run the next dev script from packages/next-js-app, I encounter the following error:

wait  - compiling...
error - ../ui-library/src/components/dropdown.tsx:3:0
Module not found: Can't resolve 'utils'
  1 | ...
  2 | ...
> 3 | import { helperFunction } from "utils"
  4 | 
  5 | const Dropdown = () => {
  6 |   const onClick = (event) => {

However, running the ui-library separately allows the use of absolute paths in imports without any issues.

Query

How can I resolve the issue of absolute imports in this scenario?

Answer №1

To resolve the issue in my situation, I made modifications to my webpack configuration by setting up aliases in my next.config.js file.

module.exports = {
 ...
 experimental: {
   externalDir: true
 }
 config.resolve = {
  ...config.resolve,
    alias: {
      ...config.resolve.alias,
      'utilities': path.resolve('../../packages/ui-library/src/utilities'),
      // ^ Specified all necessary paths for my external UI library here
    },
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with AngularJS directives - encountering a problem with the property 'compile' being undefined

Just starting out with AngularJS and attempting to make a basic directive. Unfortunately, the code is throwing a TypeError: Cannot read property 'compile' of undefined. Any advice or suggestions would be greatly welcomed. JS var xx = angular.mo ...

Animating a JQuery Slider using code from scratch

I'm attempting to dynamically alter/animate a JQuery slider. In this scenario, there are two arrays present: one for the values that need to be modified and another for the durations between modifications. By monitoring the console, you'll observ ...

Compelled to install npm modules manually against my will

Struggling with installing node modules for a project using npm install. It seems like my setup is not recognizing the package.json file or the dependencies of dependencies. I've had to resort to manually installing each package using npm install [fi ...

Transform the words in a string into an array

I'm scratching my head trying to find a solution on Stack Overflow, but nothing seems like the right fit. Can anyone provide some guidance? I need a regex that will enable me to use JavaScript split to break a string into an array. The test case is: ...

Guide to utilizing $refs effectively in Vue.js templates

Is there a way to display the input value automatically without using v-model and by making use of the "ref" attribute? <div class="form-group m-b-40"> <input type="text" class="form-control" id="name" ref="name" required> </div> {{sh ...

When running the command `npx create-react-app client`, an error is thrown stating "Reading properties of undefined is not possible (reading 'isServer')."

Installing packages. Please wait while the necessary packages are being installed. Currently installing react, react-dom, and react-scripts with cra-template... Encountered an error: Unable to read properties of undefined (reading 'isSer ...

How do I retrieve the value of a class that is repeated multiple times?

In the HTML code I am working with, there are values structured like this: <div class="item" onClick="getcoordinates()"> <div class="coordinate"> 0.1, 0.3 </div> </div> <div class="item" onClick="getcoordinates() ...

An error with jQuery occurred in the client's post response, resulting in a 400 POST HTTP/1.1 error

I am struggling to identify the issue with my code, especially since I'm not very familiar with jQuery. The goal is to create an HTML form for storing car data based on this template: https://i.sstatic.net/u40nG.gif The source code for the HTML form ...

Tips on changing the name of a property within an object using JavaScript

While this question may appear to be a duplicate, there is actually a distinction. I am attempting to provide a new key that does not contain any spaces. {order_id :"123" , order_name : "bags" , pkg_no : "00123#"} My goal is ...

When an iteration occurs within a for loop and a value changes, remember to incorporate a line break or equivalent element using jQuery

I am currently working on implementing a hierarchy system for users stored in a database table. At the moment, only top-level users with a hierarchy of 1 are displayed. When clicked, I use AJAX to retrieve and display all related users below them. These ...

Display a loading spinner in React Native while loading screens

I'm currently developing an application using React Native, and I've encountered a question. In my app, I have set up a navigator with multiple screens. I noticed that when the screens are simple or small in size, the navigation feels smooth and ...

Utilizing React componentDidUpdate for merging and appending data

I am working with a functional component that generates data in an object format from a state variable called scanlist: 0: brand: "AATEST" exp: "2022-08-25" gtin: "15735423000247" iname: "Arthur's Test Item" lot ...

Guide to generating a div element with its contents using JSON

On my webpage, there is a button that increases the "counter" value every time it's clicked. I am looking to achieve the following tasks: 1) How can I generate a json file for each div on my page like the example below: <div class="text1" id="1" ...

Is there a reason for the absence of the Revit category attribute in the JSON response retrieved from the GET request :urn/metadata/:guid/

After receiving the information from the endpoint regarding Revit Models uploaded to my bucket, I noticed that the JSON response contains multiple objects. These objects seem to represent Revit elements, each with all parameters except for the Revit Categ ...

Launching a React Native application on a fresh machine

I've been working on a brand new react-native app, starting with Expo from the beginning. After putting everything in source control and opening it on a Mac, I encountered some warnings when running npm install: npm WARN <a href="/cdn-cgi/l/email ...

The replaceWith() function in jQuery is able to transform PHP code into an HTML comment

How can I change a div element in a click event handler, while ensuring that PHP code inside the element remains intact and does not get moved into an HTML comment? You can find my code snippet below: $("#replaceCat1").click(function(){ $("div.boxconte ...

Tips for refreshing a TinyMCE instance after modifying its settings

Is there a method in TinyMCE to refresh the instance when the options are updated with { language: "fr" }? How can I apply this new option or reload the TinyMCE instance? I am looking to dynamically change the language using Javascript. ...

Illustrate a sphere within the canvas

I successfully declared the next square, however now I am eager to accomplish the same for a circle... Could you please provide guidance on how to achieve this? Thank you. //Create Variable var circ = new Circle(320, 380, 50); //Define the circle functi ...

Using React Native - Issue occurs when tapping a row within a ListView: Property `_pressRow` cannot be read as it is null

Currently, I am working on a small ReactNative + Redux application that utilizes a ListView. While referencing the code example from the documentation available at here, I have implemented a setup similar to the provided sample. However, I am encountering ...

What is the best way to attach information to a chart prior to the jquery dialog box appearing?

I am currently working on a web application that interacts with an Azure database. The goal is to create a graph based on user selections from a radio list and two inputted time parameters. The intention is to showcase this graph in a jQuery dialog box tha ...