Uuid: pkg.exports broken in typescript

Created on 6 Jun 2020  Â·  14Comments  Â·  Source: uuidjs/uuid

This compiled to typescript doesn't work:

import { v4 } from 'uuid';
v4();

Error:
Error: Package exports for '........ /node_modules/uuid' do not define a valid '.' target

I had to downgrade to v7

_Originally posted by @kryz81 in https://github.com/uuidjs/uuid/issues/245#issuecomment-640027851_

Most helpful comment

This library only officially supports LTS releases of Node.js (v8, v12, v14).

At this point I would _not_ suggest to stick to Node.js v13.x because it already reached end-of-life. Instead upgrade to v14. Please check https://nodejs.org/en/about/releases/ for details on Node.js versioning semantics.

All 14 comments

@kryz81 what typescript version and config are you using?

@ctavan

Typescript 3.8.3

Config:

{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true
},
"exclude": ["node_modules", "dist"]
}

I may be running into this issue as well. I'm using uuid as a CommonJS module with Node.js, and I installed it using npm install --save uuid. I'm seeing this error when I run my project.

ryanprince@Ryans-MBP *** % node index.js            
internal/modules/cjs/loader.js:621
  throw e;
  ^

Error: No valid exports main found for '/Users/ryanprince/Documents/***/***/node_modules/uuid'
    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._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> (/Users/ryanprince/Documents/***/***/index.js:4:24)
    at Module._compile (internal/modules/cjs/loader.js:1128:30) {
  code: 'MODULE_NOT_FOUND'
}

The uuid version is 8.1.0, and the Node.js version is 13.3.0.

@ryanprince Node 13 is unstable version and not supported. But technically you have very old one. The latest v13.14.0 works as expected.

Okay, thanks for your help!

@kryz81 this is strange, I cannot reproduce the issue. Would you mind checking out https://github.com/ctavan/uuid-example-typescript and comparing that to your code? I copy & pasted your tsconfig.json.

Works well for meâ„¢:

$ node --version
v14.4.0

$ npm test

> [email protected] test /***/uuid-example-typescript
> npm run build && node dist/index.js


> [email protected] build /***/uuid-example-typescript
> tsc --build tsconfig.json

32cbc878-c7ae-4f05-8922-d8346550cdec

@ctavan

With your repo I got the same error using Node 13.x. But when I switched to Node 14.x the error disappeared!

With Node 12.x it works too... 🤔

I guess it's the same problem with very old node 13. Check node v13.14 please.

This library only officially supports LTS releases of Node.js (v8, v12, v14).

At this point I would _not_ suggest to stick to Node.js v13.x because it already reached end-of-life. Instead upgrade to v14. Please check https://nodejs.org/en/about/releases/ for details on Node.js versioning semantics.

We have experienced similar issues within our angular/ionic application.
We have updated node to v14.4.0, and this is still not working.

The config we use is

"module": "esnext",
"moduleResolution": "node",
"target": "es5"

Project was building fine, but then the package was generating nan-undefined values.
Downgrade to v7 fixes the issue.

I was trying to replicate this on example repo, but with no luck.

@oneserve what's the exact error message you are seeing?

I've pushed an update to https://github.com/ctavan/uuid-example-typescript with the config you are mentioning and it all works just fine on my machine (Node.js v14.4.0).

What happens if you clone that repo and run:

node --version
npm install
npm test

?

@ctavan
I have this issue on Node v14 and v16. But only on my main PC. Other machines work fine.
The test succeeds with uuid v7. The biggest problem for me that I do not use this lib directly but rather with googleapis package and cant set the version.

```
PS D:\Projects\uuid-example-typescript-master> node --version
v14.17.0

PS D:\Projects\uuid-example-typescript-master> npm ls uuid
[email protected] D:\Projects\uuid-example-typescript-master
`-- [email protected]

PS D:\Projects\uuid-example-typescript-master> npm test

[email protected] test D:\Projects\uuid-example-typescript-master
npm run build && node dist/index.js

[email protected] build D:\Projects\uuid-example-typescript-master
tsc --build tsconfig.json

Debugger listening on ws://127.0.0.1:36598/acf52596-36f6-4aad-be8a-1972eb18f81d
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
Waiting for the debugger to disconnect...
file:///D:/Projects/uuid-example-typescript-master/dist/index.js:1
import { v4 } from 'uuid';
^^
SyntaxError: Named export 'v4' not found. The requested module 'uuid' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'uuid';
const { v4 } = pkg;

at ModuleJob._instantiate (internal/modules/esm/module_job.js:104:21)
at async ModuleJob.run (internal/modules/esm/module_job.js:149:5)
at async Loader.import (internal/modules/esm/loader.js:177:24)
at async Object.loadESM (internal/process/esm_loader.js:68:5)

npm ERR! Test failed. See above for more details.```

If this is an issue that you can only reproduce in a single environment/machine my best guess is that something is off with that particular environment/machine. Maybe something with globally installed dependencies?! Did you run npm install in the reproduction repo? Apart from that it’s hard to further diagnose this issue.

Of course, I did npm install. Actually [email protected] works too, but not 8.3.2. So I installed this version directly to the project and my problem disappeared.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ctavan picture ctavan  Â·  7Comments

ctavan picture ctavan  Â·  5Comments

danactive picture danactive  Â·  3Comments

dstaley picture dstaley  Â·  9Comments

nemoload picture nemoload  Â·  3Comments