Error: Package exports for tslib do not define a valid '.' target

10.8k Views Asked by At

I literally made no changes to code, and suddenly I woke up to this error one day,

backend: internal/modules/cjs/loader.js:454
backend:   const e = new Error(`Package exports for '${basePath}' do not define a ` +
backend:             ^
backend: Error: Package exports for '/Users/kumarabhirup/Documents/Repositories/propagateAt/node_modules/tslib' do not define a valid '.' target
backend:     at resolveExportsTarget (internal/modules/cjs/loader.js:454:13)
backend:     at resolveExports (internal/modules/cjs/loader.js:387:16)
backend:     at Function.Module._findPath (internal/modules/cjs/loader.js:486:20)
backend:     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:781:27)
backend:     at Function.Module._load (internal/modules/cjs/loader.js:687:27)
backend:     at Module.require (internal/modules/cjs/loader.js:849:19)
backend:     at require (internal/modules/cjs/helpers.js:74:18)
backend:     at Object.<anonymous> (/Users/kumarabhirup/Documents/Repositories/propagateAt/node_modules/apollo-link/lib/index.js:3:15)
backend:     at Module._compile (internal/modules/cjs/loader.js:956:30)
backend:     at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10) {
backend:   code: 'MODULE_NOT_FOUND'
backend: }
backend: [nodemon] app crashed - waiting for file changes before starting...

This person from another StackOverflow question changed their node version and it worked for them, I tried it too (v12.x.x -> v15.x.x), didn't work for me.

I also upgraded typescript, tried installing tslib, did yarn cache clean, deleted and reinstalled node_modules, deleted yarn.lock, nothing worked.

Please help.

Here's my package.json:

{
  "name": "backend",
  "version": "1.0.0",
  "license": "ISC",
  "main": "dist/src/index.js",
  "scripts": {
    "build": "tsc -p tsconfig.release.json",
    "build:watch": "tsc --watch -p tsconfig.release.json",
    "start": "npm run build && node --experimental-modules --experimental-json-modules --es-module-specifier-resolution=node dist/src/index.js",
    "start:pm2": "NODE_ENV=production pm2 start dist/src/index.js --node-args=\"--max_old_space_size=500 --experimental-modules --experimental-json-modules --es-module-specifier-resolution=node\"",
    "dev": "npm run build && concurrently --raw \"nodemon --experimental-modules --experimental-json-modules --es-module-specifier-resolution=node dist/src/index.js\" \"npm run build:watch\"",
    "lint": "eslint . --ext js,jsx,ts,tsx",
    "lint:fix": "eslint . --fix --ext js,jsx,ts,tsx",
    "test": "NODE_ENV=test jest --passWithNoTests --watch",
    "test:ci": "NODE_ENV=test jest --passWithNoTests"
  },
  "devDependencies": {
    "@types/bull": "^3.14.0",
    "@types/mongoose": "^5.5.43",
    "@types/node": "^12.7.3",
    "@types/node-cron": "^2.0.2",
    "@types/nodemailer": "^6.4.0",
    "concurrently": "^5.1.0",
    "eslint": "^6.8.0",
    "eslint-config-airbnb-typescript-prettier": "^2.1.0",
    "nodemon": "^1.19.1",
    "pm2": "^4.4.0",
    "prettier": "^1.18.2",
    "tslib": "^2.0.3",
    "typescript": "^3.7.0",
    "typings": "^2.1.1"
  },
  "dependencies": {
    ...
    "tslib": "^2.0.3",
    ...
  },
  "engines": {
    "node": ">=12.x"
  },
  "resolutions": {
    "graphql": "14.x"
  }
}

Here's my tsconfig.json:

{
  "compilerOptions": {
    "target": "es2019",
    "module": "commonjs",
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "allowJs": true,
    "importHelpers": true,
    "alwaysStrict": true,
    "sourceMap": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitAny": false,
    "noImplicitThis": false,
    "strictNullChecks": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "resolveJsonModule": true,
    // "lib": ["es2017"],
    "types": ["jest"]
  },
  "include": ["src/**/*", "__tests__/**/*"],
  "paths": {
    "tslib" : ["node_modules/tslib/tslib.d.ts"]
  },
}

Here's my tsconfig.release.json:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "rootDir": ".",
    "outDir": "dist",
    "removeComments": true
  },
  "include": ["src/**/*"]
}

Thanks!

5

There are 5 best solutions below

2
On BEST ANSWER

I had a similar issue and I workarounded by downgrading node version (from v13.0.1 to v12.19.0). It's probably not the ideal solution, but it can help you to unblock the situation while looking for the final fix.

I used nvm for windows to manage node versions.

I leave you an screenshot of problem & resolution.

"'tslib' do not define a valid '.' target" workaround

I hope it helps to sort out your issue.

Regards!

0
On

This solved if for me:

  1. Downgraded to the latest version of Node v12.
  2. Delete the node_modules folder, and reinstall them.

Note: I'm using Angular v8, so I checked their update site to see when upgrading to Angular 8 what Node version is recommended, and it's Node v10, and since Node should be backwards compatible I just went down from Node v13 to Node v12.

0
On

I had the same issue, and ran npm audit fix like four times, sometimes with the --force flag and that fixed it for me after none of the other answers here worked.

0
On

All of the aforementioned suggestions are correct. Most of the case I run into this error it is due to inconsistent nodejs versions.

  1. I use nvm to manage nodejs versions
  2. Every project has .nvmrc which states a different node version
  3. When running the npm command on a project running a different nodejs version, that command fails with a message similar to the one posted in the question.

The fix this issue, when switching projects, the first command issued consists of switching to the right node version. nvm use will be enough on any project having .nvmrc.

StackOverflow already has answers on "How to install nvm" - and "How to use .nvmrc"

1
On

For me, these steps did the trick:

  1. Downgrading to latest NodeJS stable version: nvm install --lts
  2. Deleting /node_modules & package-lock.json files
  3. Kill the pm2 processes and add again: pm2 kill and pm2 start process.json

The trick was actually killing pm2 and adding the process again. Good luck!