nanoid: Error: No valid exports main found for 'node_modules\nanoid'

Hello!

I was trying out nanoid in a TypeScript project and came across an issue when running the code through ts-node-dev.

The setup

EDIT: Forgot to include I have Node 13.3.0 installed.

The issue occurs with nanoid version 3.0.2 when running ts-node-dev.

Compilation with tsc passes without error.

I was able to reproduce the issue with the following setup:

package.json

{
  "private": true,
  "name": "01-todo-list",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "tsc",
    "start": "ts-node-dev -r tsconfig-paths/register --respawn --no-notify --transpileOnly ./src/index.ts"
  },
  "keywords": [],
  "author": "dstrekelj <domagoj.strekelj@gmail.com>",
  "license": "IDGAF",
  "devDependencies": {
    "@types/node": "^13.11.1",
    "ts-node-dev": "^1.0.0-pre.44",
    "tsconfig-paths": "^3.9.0",
    "typescript": "^3.8.3"
  },
  "dependencies": {
    "nanoid": "^3.0.2"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es2015",
    "module": "commonjs",
    "declaration": true,
    "declarationMap": true,
    "sourceMap": true,
    "outDir": "./build",
    "strict": true,
    "moduleResolution": "node",
    "baseUrl": "./src",
    "paths": {
      "@app/*": ["*"]
    }
  },
  "exclude": ["node_modules", "build", "__tests__"]
}

src/index.ts

import { nanoid } from "nanoid";

console.log(nanoid());

Afterwards:

  1. npm install
  2. npm run start

The issue

Here is the complete transcript:

Using ts-node version 8.8.2, typescript version 3.8.3
Error: No valid exports main found for 'F:\projects\ts\challenges\reproducible\node_modules\nanoid'
    at resolveExportsTarget (internal/modules/cjs/loader.js:618:9)
    at applyExports (internal/modules/cjs/loader.js:499:14)
    at resolveExports (internal/modules/cjs/loader.js:548:12)
    at Function.Module._findPath (internal/modules/cjs/loader.js:650:22)
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:948:27)
    at Function.Module._resolveFilename (F:\projects\ts\challenges\reproducible\node_modules\tsconfig-paths\lib\register.js:75:40)
    at Function.Module._load (internal/modules/cjs/loader.js:854:27)
    at Module.require (internal/modules/cjs/loader.js:1023:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (F:\projects\ts\challenges\reproducible\src\index.ts:1:1)
[ERROR] 18:39:55 Error: No valid exports main found for 'F:\projects\ts\challenges\reproducible\node_modules\nanoid'

Other information

After downgrading to nanoid 2.1.11 everything worked fine, though I had to install @types/nanoid to get completion.

Older 3.x versions throw the same error.

I’m thinking ts-node-dev is at fault or something in my configuration, but seeing as how version 3.x is still fresh I figured it’s worth to create an issue just to be safe.

Might be related to https://github.com/ai/nanoid/issues/205?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 15 (10 by maintainers)

Most upvoted comments

I set up a project reflecting the fixture you provided. I still had the error.

Then I realised that my test results aren’t valid unless I ran the tests in the same Node environment you use.

Assuming the latest version of Node is used to run the tests, I updated from 13.3.0 to 13.12.0.

I tried…

import { nanoid } from "nanoid";

… and…

const { nanoid } = require("nanoid");

After updating to Node 13.12.0 both worked without a problem!

I will try and play around with some additional samples to see if anything else breaks. As it stands now this seemed to be caused by my older version of Node acting up.

Thank you very much for your patience and expertise in figuring this out!

I added ts-node test to dual-publish and everything works.

We use this dual-publish to create a dual ESM/CJS package for Nano ID.

But I found that you should use CommonJS in your package. So you must have:

const { lib } = require('lib')

Will moving to CommonJS solved you problem with ts-node-dev?

Until we will discover the problem, I added ts-node-dev notice to known issues of 3.0 migration guide https://github.com/ai/nanoid/releases/tag/3.0.0