Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/tokenize' is not defined by "exports" in the package.json of a module in node_modules

This is a React web app. When I run

npm start

This error occurred

> [email protected] start
> react-scripts start

node:internal/modules/cjs/loader:488
      throw e;
  ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/tokenize' is not defined by "exports" in /Users/juliantc/Desktop/ai-studio/development/frontend/node_modules/postcss-safe-parser/node_modules/postcss/package.json
    at new NodeError (node:internal/errors:371:5)
    at throwExportsNotFound (node:internal/modules/esm/resolve:416:9)
    at packageExportsResolve (node:internal/modules/esm/resolve:669: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.<anonymous> (/Users/juliantc/Desktop/ai-    studio/development/frontend/node_modules/postcss-safe-parser/lib/safe-parser.js:1:17) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Node.js v17.0.1

This error only occurs when I run this on this specific computer, which I do not have superuser access to. It works on other computers.

For reference, this is ./node_modules/postcss-safe-parser/node_modules/postcss/package.json

{
  "name": "postcss",
  "version": "8.2.6",
  "description": "Tool for transforming styles with JS plugins",
  "engines": {
    "node": "^10 || ^12 || >=14"
  },
  "exports": {
    ".": {
      "require": "./lib/postcss.js",
      "import": "./lib/postcss.mjs",
      "types": "./lib/postcss.d.ts"
    },
    "./": "./"
  },
  "main": "./lib/postcss.js",
  "types": "./lib/postcss.d.ts",
  "keywords": [
    "css",
    "postcss",
    "rework",
    "preprocessor",
    "parser",
    "source map",
    "transform",
    "manipulation",
    "transpiler"
  ],
  "funding": {
    "type": "opencollective",
    "url": "https://opencollective.com/postcss/"
  },
  "author": "Andrey Sitnik <[email protected]>",
  "license": "MIT",
  "homepage": "https://postcss.org/",
  "repository": "postcss/postcss",
  "dependencies": {
    "colorette": "^1.2.1",
    "nanoid": "^3.1.20",
    "source-map": "^0.6.1"
  },
  "browser": {
    "./lib/terminal-highlight": false,
    "colorette": false,
    "fs": false
  }
}

And this is what I get when I list the files in ./node_modules/postcss-safe-parser/node_modules/postcss/lib/

lgtd-lt-119-mbmt:frontend juliantc$ ls ./node_modules/postcss-safe-parser/node_modules/postcss/lib/

at-rule.d.ts        css-syntax-error.d.ts   input.d.ts      map-generator.js    postcss.d.ts        processor.js        rule.js         tokenize.js
at-rule.js      css-syntax-error.js input.js        node.d.ts       postcss.js      result.d.ts     stringifier.js      warn-once.js
comment.d.ts        declaration.d.ts    lazy-result.d.ts    node.js         postcss.mjs     result.js       stringify.d.ts      warning.d.ts
comment.js      declaration.js      lazy-result.js      parse.d.ts      previous-map.d.ts   root.d.ts       stringify.js        warning.js
container.d.ts      fromJSON.d.ts       list.d.ts       parse.js        previous-map.js     root.js         symbols.js
container.js        fromJSON.js     list.js         parser.js       processor.d.ts      rule.d.ts       terminal-highlight.js

Solution 1:

I am also stuck with the same problem because I installed the latest version of Node.js (v17.0.1).

Just go for node.js v14.18.1 and remove the latest version just use the stable version v14.18.1

nvm uninstall <version>

OR

nvm uninstall v17.0.1

then install the LTS one which is v14.18.1

nvm install --lts

This worked for me.

Solution 2:

Remove node_modules folder and .lock file and re-install your packages (yarn or npm). It worked for me with last 17.0.1 of nodejs, I can npm (or yarn) start my app again.

Solution 3:

Roll back to Node 16.8.0 or (works for me) stable version 16.13.0:

Step 1: install n

npm install -g n

Step 2: install/set node latest stable version

sudo n stable

or set 16.8.0

sudo n 16.8.0

Note, if you can't or don't want to use sudo for n, you can set environment variables for the location, eg;

export N_PREFIX=$HOME/.n
export PATH=$N_PREFIX/bin:$PATH

Solution 4:

FWIW, I bumped into this issue on macOS Monterey with Node 17.0.1 installed via Homebrew. Downgrading to Node 16.x worked for me.

Downgrade from Node 17.x (latest) to Node 16.x using Homebrew:

brew uninstall --ignore-dependencies node
brew install node@16
brew link node@16

Solution 5:

my working solution was with yarn:

  • removed /node_modules and yarn.lock file.
  • then reinstalled my deleted packages/file using yarn install .

it worked !!!