Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg.exports broken in typescript #466

Closed
ctavan opened this issue Jun 6, 2020 · 14 comments
Closed

pkg.exports broken in typescript #466

ctavan opened this issue Jun 6, 2020 · 14 comments

Comments

@ctavan
Copy link
Member

ctavan commented Jun 6, 2020

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 #245 (comment)

@ctavan
Copy link
Member Author

ctavan commented Jun 6, 2020

@kryz81 what typescript version and config are you using?

@kryz81
Copy link

kryz81 commented Jun 6, 2020

@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"]
}

@ryanprince
Copy link

ryanprince commented Jun 6, 2020

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.

@TrySound
Copy link
Member

TrySound commented Jun 6, 2020

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

@ryanprince
Copy link

Okay, thanks for your help!

ctavan added a commit to ctavan/uuid-example-typescript that referenced this issue Jun 8, 2020
@ctavan
Copy link
Member Author

ctavan commented Jun 8, 2020

@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

> uuid-example-typescript@1.0.0 test /***/uuid-example-typescript
> npm run build && node dist/index.js


> uuid-example-typescript@1.0.0 build /***/uuid-example-typescript
> tsc --build tsconfig.json

32cbc878-c7ae-4f05-8922-d8346550cdec

@kryz81
Copy link

kryz81 commented Jun 9, 2020

@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... 🤔

@TrySound

This comment has been minimized.

@ctavan
Copy link
Member Author

ctavan commented Jun 9, 2020

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.

@ctavan ctavan closed this as completed Jun 9, 2020
@oneserve
Copy link

oneserve commented Jun 9, 2020

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.

@ctavan
Copy link
Member Author

ctavan commented Jun 11, 2020

@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

?

@alllohaaa
Copy link

alllohaaa commented May 23, 2021

@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
uuid-example-typescript@1.0.0 D:\Projects\uuid-example-typescript-master
`-- uuid@8.3.2

PS D:\Projects\uuid-example-typescript-master> npm test   
> uuid-example-typescript@1.0.0 test D:\Projects\uuid-example-typescript-master
> npm run build && node dist/index.js


> uuid-example-typescript@1.0.0 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.```

@ctavan
Copy link
Member Author

ctavan commented May 25, 2021

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.

@alllohaaa
Copy link

Of course, I did npm install. Actually uuid@8.1.0 works too, but not 8.3.2. So I installed this version directly to the project and my problem disappeared.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants