Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'cookie-es' | After Nuxt 3 build in Windows 10

539 Views Asked by At

The issue

The Nuxt 3 build exports module "cookie-es" as a reference, symbolic link or how it's called in Win. The reference will be empty after copying.

I propose a question

How do I tell Nuxt to export a module as a standard directory (if possible), or in the worse case, how do I copy a file in Windows droping references and making real files of them instead?

The problem showcase

how "node_modules" looks like after build (path....output\server\node_modules)
node_module cookie-es outside

how "node_modules" looks like after build (path....output\server\node_modules)
node_module cookie-es inside

how "node_modules" looks like after copied (otherPath....output\server\node_modules)
node_module cookie-es outside

how "node_modules" looks like after copied (otherPath....output\server\node_modules)
node_module cookie-es inside

where is it referencing files from? From "node_modules/.nitro"
node_module .nitro inside

In plain words only, content of a directory is lost after copying, due to a loss of a reference I quess.

Variables

OS: Windows 10
Nuxt: ^3.4.3

What I tried

I have tried disabling symlinks in vite config (inside nuxt.config)

vite: {
    resolve: {
        preserveSymlinks: false
    }
}

AND

I have tried to manualy copy the files from the "build modules" into the lost gaps in "copied modules". It works but its not a possibility to manually find the problem ones and copy them every time.

Solution

I have solved the issue by auto-copying file (inside package.json)

"scripts": {
    "build": "nuxt build",
    "postbuild": "robocopy /e /j \".output/server/node_modules\" 
        \".output/server/node_modules_copy\" & rd /s /q 
        \".output/server/node_modules\" & robocopy /e /j 
        \".output/server/node_modules_copy\" \".output/server/node_modules\" 
        & rd /s /q \".output/server/node_modules_copy\""
}

It copies modules without symbolic links into a new directory and then copies it back as node_modules.

Please feel free to post a better solution, thanks

1

There are 1 best solutions below

2
Marc On

In general there are many packages that will fail to work as they are hopelessly dependent traditional node_modules directory structure, so anything that goes beyond the basic traditional spec of NPM will break some packages because the authors didn't take a modern adaptive approach when building their lib.

You haven't specified how you instigated your project, I will assume you used one of the following modern package managers that this type of issue is typically caused by:

PNPM

If you used pnpm to instigate your project then issues around symbolic linking can arise, there are unfortunately many packages that wont work with pnpm.

You might try the folloing configutation options:

https://pnpm.io/blog/2020/10/17/node-modules-configuration-options-with-pnpm#plugnplay-the-strictest-configuration

node-linker=pnp
symlink=false

YARN 3

If you used yarn you can have a similar issue with certain packages not working because specifically because of the lack of a node_modules directory in a project root, in the case of yarn you can fix package import issues using the node linker config in a .yarnrc.yml file in your project root.

nodeLinker: node-modules