'Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/parser' is not defined by "exports" in /usr/app/node_modules/postcss/package.json

Did a docker-compose up to build my project, but there's something wrong and I am getting this error:

node:internal/errors:464
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/parser' is not defined by "exports" in /usr/app/node_modules/postcss/package.json
    at new NodeError (node:internal/errors:371:5)
    at throwExportsNotFound (node:internal/modules/esm/resolve:429:9)
    at packageExportsResolve (node:internal/modules/esm/resolve:703:3)
    at resolveExports (node:internal/modules/cjs/loader:482:36)
    at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.552 (/usr/app/node_modules/next/dist/compiled/postcss-scss/scss-syntax.js:1:11590)
    at __nccwpck_require__ (/usr/app/node_modules/next/dist/compiled/postcss-scss/scss-syntax.js:1:11735)
    at Object.270 (/usr/app/node_modules/next/dist/compiled/postcss-scss/scss-syntax.js:1:400)
    at __nccwpck_require__ (/usr/app/node_modules/next/dist/compiled/postcss-scss/scss-syntax.js:1:11735)
    at Object.327 (/usr/app/node_modules/next/dist/compiled/postcss-scss/scss-syntax.js:1:260)
    at __nccwpck_require__ (/usr/app/node_modules/next/dist/compiled/postcss-scss/scss-syntax.js:1:11735)
    at Object.845 (/usr/app/node_modules/next/dist/compiled/postcss-scss/scss-syntax.js:1:3733) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Node.js v17.4.0
Service 'nextjs' failed to build : The command '/bin/sh -c npm run build' returned a non-zero code: 1

Here's my Dockerfile

FROM node:alpine

# Set working directory
WORKDIR /usr/app

# Install PM2 globally
RUN npm install --global pm2

# Copy "package.json" and "package-lock.json" before other files
# Utilise Docker cache to save re-installing dependencies if unchanged
COPY ./package*.json ./

# Install dependencies
RUN npm install --production

# Copy all files
COPY ./ ./

# Build app
RUN npm run build

# Expose the listening port
EXPOSE 3000

# Run container as non-root (unprivileged) user
# The "node" user is provided in the Node.js Alpine base image
USER node

# Launch app with PM2
CMD [ "pm2-runtime", "start", "npm", "--", "start" ]

Here's my package.json:

{
  "name": "nextjs",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start"
  },
  "dependencies": {
    "next": "^10.0.6",
    "react": "17.0.1",
    "react-dom": "17.0.1"
  }
}

Tried deleting my node_modules folder and reinstalling, but it doesn't work. What's using the post/css package and is the error due to the node version? If so, what alpine version should I use in my Dockerfile?



Solution 1:[1]

I guess am late , but I have the solution. I faced the same issue you were facing in this line

import { useState } from "react/cjs/react.production.min";

You should try this instead...

 import {useState} from 'react' directly

Solution 2:[2]

The following changes helped me address this issue:

// package.json
{...,
  "dependencies": { ... },
  "devDependencies": {
    "eslint": "8.14.0",
    "eslint-config-next": "12.1.5"
  },
  "keywords": [],
  "description": ""
 }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Danrley Pereira
Solution 2 Boris