Nextjs module not found only when importing a simple sum function from my own npm library

I’m stuck on this issue since a couple of days. I hope there’s a NextJS (14) compiler expert out there that could help me understanding what I’m doing wrong.

I have built a simple npm package containing some interfaces and functions that I want to use in multiple NextJS application using app router.
I build this library with tsup and a simple tsup.config.ts like

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { defineConfig } from "tsup"
export default defineConfig({
entry: ['src/index.ts'], // Entry point of your package
outDir: 'dist', // Output directory
format: ['esm', 'cjs'], // Output formats: ESM and CommonJS
dts: true, // Generate TypeScript declaration files
sourcemap: true, // Generate source maps for easier debugging
clean: true, // Clean output directory before each build
target: 'esnext', // JavaScript language target
external: ['react', 'next'], // Mark React and Next.js as external dependencies
esbuildOptions(options) {
options.banner = {
js: '"use client"',
}
},
});
</code>
<code>import { defineConfig } from "tsup" export default defineConfig({ entry: ['src/index.ts'], // Entry point of your package outDir: 'dist', // Output directory format: ['esm', 'cjs'], // Output formats: ESM and CommonJS dts: true, // Generate TypeScript declaration files sourcemap: true, // Generate source maps for easier debugging clean: true, // Clean output directory before each build target: 'esnext', // JavaScript language target external: ['react', 'next'], // Mark React and Next.js as external dependencies esbuildOptions(options) { options.banner = { js: '"use client"', } }, }); </code>
import { defineConfig } from "tsup"

export default defineConfig({
    entry: ['src/index.ts'],  // Entry point of your package
    outDir: 'dist',           // Output directory
    format: ['esm', 'cjs'],   // Output formats: ESM and CommonJS
    dts: true,                // Generate TypeScript declaration files
    sourcemap: true,          // Generate source maps for easier debugging
    clean: true,              // Clean output directory before each build
    target: 'esnext',         // JavaScript language target
    external: ['react', 'next'],  // Mark React and Next.js as external dependencies
    esbuildOptions(options) {
        options.banner = {
          js: '"use client"',
        }
    },
});

I then run

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>npm link
</code>
<code>npm link </code>
npm link

and in my NextJS app I do the same

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>npm link myPackage
</code>
<code>npm link myPackage </code>
npm link myPackage

In a server action of my NextJS app everything is fine until I only import the types.
As soon as I start importing a function, I get the “Module not found” exception when the page using the server action is built.

Even if the function is just as simple as

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>export function simpleSum(a: number, b: number): number | null {
return a+b;
}
</code>
<code>export function simpleSum(a: number, b: number): number | null { return a+b; } </code>
export function simpleSum(a: number, b: number): number | null {    
    return a+b;
}

The build doesn’t fail if I just have

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { TypeA, TypeB, TypeC } from 'myPackage';
</code>
<code>import { TypeA, TypeB, TypeC } from 'myPackage'; </code>
import { TypeA, TypeB, TypeC } from 'myPackage';

When I do

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { TypeA, TypeB, TypeC, simpleSum } from '@bpmromandie/kyc_common_types';
</code>
<code>import { TypeA, TypeB, TypeC, simpleSum } from '@bpmromandie/kyc_common_types'; </code>
import { TypeA, TypeB, TypeC, simpleSum } from '@bpmromandie/kyc_common_types';

and I don’t use simpleSum the build is still ok.

It’s when I add a call to simpleSum in my server action that the build fails with the message

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Module not found: Can't resolve 'myPackage'
</code>
<code>Module not found: Can't resolve 'myPackage' </code>
Module not found: Can't resolve 'myPackage'

Just to prevent some possible answers…
If I check under node_modules the files of myPackage are there.
The types and the function are in all these files

List of all the files under /node_modules/myPackage

I’ve marked my functions.ts file with ‘use client’ thinking that it was related to an issue with my Server Component and I’ve also added the esBuildOption you can see in my tsup.config.ts file up above (and I indeed see the ‘use client’ directive in the imported files).

I’ve also published my package in NPM and installed it with npm i myPackage to remove the possible issues due to using npm link.

I’m using NodeJS 20.11.0 and NextJS 14.2.3.

Since my library has nothing so secret inside, you could find it under @bpmromandie/kyc_common_types and see if you are able to import and use one of the available functions in your own app.

Thanks to the kind expert that could shed some light on this issue.

New contributor

Maurizio Manca is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật