prisma / prisma-client-js

Type-safe database client for TypeScript & Node.js (ORM replacement)
Apache License 2.0
1.47k stars 67 forks source link

Cannot find module '@prisma/client' after installing a new package via NPM #560

Closed matthewmueller closed 4 years ago

matthewmueller commented 4 years ago

Steps to Reproduce

0. Install packages

npm init -y
npm install prisma2 ts-node typescript

0.5 Add tsconfig.json

This is a cleaned up result of running ./node_modules/.bin/tsc --init and changing the target field.

{
  "compilerOptions": {
    "target": "es2018" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
    "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
    "strict": true /* Enable all strict type-checking options. */,
    "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
    "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
  }
}

1. Start with a working prisma schema

This step can be anything, but here's mine:

datasource db {
  provider = "postgresql"
  url      = "postgres://m@localhost:5432/prisma-dev"
}

generator client {
  provider = "prisma-client-js"
}

model posts {
  created_at DateTime?
  id         Int       @default(autoincrement()) @id
  title      String?
  user_id    users?
}

model users {
  id    Int     @default(autoincrement()) @id
  name  String
  posts posts[]
}

2. Run ./node_modules/.bin/prisma2 generate, get a working client

You should see

success Saved 0 new dependencies.
✨  Done in 0.39s.

✔ Installed the @prisma/client and prisma2 packages in your project
✔ Generated Prisma Client to ./node_modules/@prisma/client in 101ms

You can now start using Prisma Client in your code:

import { PrismaClient } from '@prisma/client'
// or const { PrismaClient } = require('@prisma/client')

const prisma = new PrismaClient()

Explore the full API: http://pris.ly/d/client

3. Write a script that uses @prisma/client

Again shouldn't matter but here's mine:

import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()

async function main() {
  await prisma.connect()
  console.log(await prisma.users.count())
}

main().catch(console.error)
$ ./node_modules/.bin/ts-node index.ts

All good.

4. Run npm install uid (or any package name)

$ npm install uid
+ uid@1.0.0
removed 8 packages, updated 124 packages and audited 173 packages in 3.894s

NPM version

$ npm version
{
  'demo-2': '1.0.0',
  npm: '6.13.6',
  ares: '1.15.0',
  brotli: '1.0.7',
  cldr: '35.1',
  http_parser: '2.9.3',
  icu: '64.2',
  llhttp: '2.0.4',
  modules: '72',
  napi: '5',
  nghttp2: '1.40.0',
  node: '12.16.1',
  openssl: '1.1.1d',
  tz: '2019c',
  unicode: '12.1',
  uv: '1.34.0',
  v8: '7.8.279.23-node.31',
  zlib: '1.2.11'
}

Node version

$ node -v
v12.16.1

5. Run your script again

$ ./node_modules/.bin/ts-node index.ts
index.ts:1:30 - error TS2307: Cannot find module '@prisma/client'.

1 import { PrismaClient } from '@prisma/client'

To fix this, you need to run ./node_modules/.bin/prisma2 generate again. This is really disruptive.

janpio commented 4 years ago

Could not reproduce this:

C:\Users\Jan\Documents\throwaway\560
λ prisma2 -v
prisma2@2.0.0-preview023, binary version: d3b0ceed5d87544b9d2decb70e08664f9047bb73

C:\Users\Jan\Documents\throwaway\560
λ prisma2 generate
✔ Created ./package.json
yarn add v1.22.0
warning ..\..\package.json: No license field
info No lockfile found.
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
success Saved 1 new dependency.
info Direct dependencies
└─ prisma2@2.0.0-preview023
info All dependencies
└─ prisma2@2.0.0-preview023
Done in 13.66s.
yarn add v1.22.0
warning ..\..\package.json: No license field
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
success Saved 1 new dependency.
info Direct dependencies
└─ @prisma/client@2.0.0-preview023
info All dependencies
└─ @prisma/client@2.0.0-preview023
Done in 1.55s.

✔ Installed the @prisma/client and prisma2 packages in your project
✔ Generated Prisma Client to .\node_modules\@prisma\client in 73ms

You can now start using Prisma Client in your code:

´´´
import { PrismaClient } from '@prisma/client'
// or const { PrismaClient } = require('@prisma/client')

const prisma = new PrismaClient()
´´´

Explore the full API: http://pris.ly/d/client

C:\Users\Jan\Documents\throwaway\560  (my-prisma-project@1.0.0)
λ ts-node index.ts

C:\ProgramData\nvm\v12.6.0\node_modules\ts-node\src\index.ts:245
    return new TSError(diagnosticText, diagnosticCodes)
           ^
TSError: ⨯ Unable to compile TypeScript:
error TS2468: Cannot find global value 'Promise'.
index.ts:4:16 - error TS2705: An async function or method in ES5/ES3 requires the 'Promise' constructor.  Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your `--lib` option.

4 async function main() {
                 ~~~~

    at createTSError (C:\ProgramData\nvm\v12.6.0\node_modules\ts-node\src\index.ts:245:12)
    at reportTSError (C:\ProgramData\nvm\v12.6.0\node_modules\ts-node\src\index.ts:249:19)
    at getOutput (C:\ProgramData\nvm\v12.6.0\node_modules\ts-node\src\index.ts:357:34)
    at Object.compile (C:\ProgramData\nvm\v12.6.0\node_modules\ts-node\src\index.ts:415:32)
    at Module.m._compile (C:\ProgramData\nvm\v12.6.0\node_modules\ts-node\src\index.ts:493:43)
    at Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Object.require.extensions.<computed> [as .ts] (C:\ProgramData\nvm\v12.6.0\node_modules\ts-node\src\index.ts:496:12)
    at Module.load (internal/modules/cjs/loader.js:643:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:839:10)

C:\Users\Jan\Documents\throwaway\560  (my-prisma-project@1.0.0)
λ node index.js
PrismaClientKnownRequestError:
Invalid `console.log(await prisma.users.count()` invocation in
C:\Users\Jan\Documents\throwaway\560\index.js:6:34

  2 const prisma = new PrismaClient()
  3
  4 async function main() {
  5   await prisma.connect()
→ 6   console.log(await prisma.users.count())

Authentication failed against database server at `localhost`, the provided database credentials for `m` are not valid.

Please make sure to provide valid database credentials for the database server at `localhost`.
    at PrismaClientFetcher.request (C:\Users\Jan\Documents\throwaway\560\node_modules\@prisma\client\index.js:87:17)
    at processTicksAndRejections (internal/process/task_queues.js:85:5) {
  code: 'P1000',
  meta: { database_user: 'm', database_host: 'localhost' }
}
^C
C:\Users\Jan\Documents\throwaway\560  (my-prisma-project@1.0.0)
λ npm install uid
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN my-prisma-project@1.0.0 No description
npm WARN my-prisma-project@1.0.0 No repository field.

+ uid@1.0.0
added 1 package from 1 contributor and audited 3 packages in 1.005s
found 0 vulnerabilities

C:\Users\Jan\Documents\throwaway\560  (my-prisma-project@1.0.0)
λ node index.js
PrismaClientKnownRequestError:
Invalid `console.log(await prisma.users.count()` invocation in
C:\Users\Jan\Documents\throwaway\560\index.js:6:34

  2 const prisma = new PrismaClient()
  3
  4 async function main() {
  5   await prisma.connect()
→ 6   console.log(await prisma.users.count())

Authentication failed against database server at `localhost`, the provided database credentials for `m` are not valid.

Please make sure to provide valid database credentials for the database server at `localhost`.
    at PrismaClientFetcher.request (C:\Users\Jan\Documents\throwaway\560\node_modules\@prisma\client\index.js:87:17)
    at processTicksAndRejections (internal/process/task_queues.js:85:5) {
  code: 'P1000',
  meta: { database_user: 'm', database_host: 'localhost' }
}
^C

As you can see I had to change the script to index.js as your reproduction was missing information.

C:\Users\Jan\Documents\throwaway\560  (my-prisma-project@1.0.0)
λ npm -v
6.9.0

C:\Users\Jan\Documents\throwaway\560  (my-prisma-project@1.0.0)
λ npm version
{
  'my-prisma-project': '1.0.0',
  npm: '6.9.0',
  ares: '1.15.0',
  brotli: '1.0.7',
  cldr: '35.1',
  http_parser: '2.8.0',
  icu: '64.2',
  llhttp: '1.1.4',
  modules: '72',
  napi: '4',
  nghttp2: '1.38.0',
  node: '12.6.0',
  openssl: '1.1.1c',
  tz: '2019a',
  unicode: '12.1',
  uv: '1.30.1',
  v8: '7.5.288.22-node.14',
  zlib: '1.2.11'
}

C:\Users\Jan\Documents\throwaway\560  (my-prisma-project@1.0.0)
λ node -v
v12.6.0
janpio commented 4 years ago

Can reproduce:

C:\Users\Jan\Documents\throwaway                                                                 
λ mkdir 560_3                                                                                    

C:\Users\Jan\Documents\throwaway                                                                 
λ cd 560_3                                                                                       

C:\Users\Jan\Documents\throwaway\560_3                                                           
λ npm init                                                                                       
This utility will walk you through creating a package.json file.                                 
It only covers the most common items, and tries to guess sensible defaults.                      

See `npm help json` for definitive documentation on these fields                                 
and exactly what they do.                                                                        

Use `npm install <pkg>` afterwards to install a package and                                      
save it as a dependency in the package.json file.                                                

Press ^C at any time to quit.                                                                    
package name: (560_3)                                                                            
version: (1.0.0)                                                                                 
description:                                                                                     
entry point: (index.js)                                                                          
test command:                                                                                    
git repository:                                                                                  
keywords:                                                                                        
author:                                                                                          
license: (ISC)                                                                                   
About to write to C:\Users\Jan\Documents\throwaway\560_3\package.json:                           

{                                                                                                
  "name": "560_3",                                                                               
  "version": "1.0.0",                                                                            
  "description": "",                                                                             
  "main": "index.js",                                                                            
  "scripts": {                                                                                   
    "test": "echo \"Error: no test specified\" && exit 1"                                        
  },                                                                                             
  "author": "",                                                                                  
  "license": "ISC"                                                                               
}                                                                                                

Is this OK? (yes)                                                                                

C:\Users\Jan\Documents\throwaway\560_3  (560_3@1.0.0)                                            
λ                                                                                                
C:\Users\Jan\Documents\throwaway\560_3  (560_3@1.0.0)                                            
λ npm install prisma2 ts-node typescript                                                         

> prisma2@2.0.0-preview023 install C:\Users\Jan\Documents\throwaway\560_3\node_modules\prisma2   
> node download-build/index.js                                                                   

npm notice created a lockfile as package-lock.json. You should commit this file.                 
npm WARN 560_3@1.0.0 No description                                                              
npm WARN 560_3@1.0.0 No repository field.                                                        

+ ts-node@8.6.2                                                                                  
+ prisma2@2.0.0-preview023                                                                       
+ typescript@3.8.3                                                                               
added 10 packages from 42 contributors and audited 10 packages in 13.072s                        
found 0 vulnerabilities                                                                          

C:\Users\Jan\Documents\throwaway\560_3  (560_3@1.0.0)                                            
λ code .                                                                                         

C:\Users\Jan\Documents\throwaway\560_3>"./node_modules/.bin/prisma2" generate
yarn add v1.22.0
warning ..\..\package.json: No license field
info No lockfile found.
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
warning "prisma2" is already in "dependencies". Please remove existing entry first before adding it to "devDependencies".   
success Saved 10 new dependencies.
info Direct dependencies
├─ prisma2@2.0.0-preview023
├─ ts-node@8.6.2
└─ typescript@3.8.3
info All dependencies
├─ arg@4.1.3
├─ buffer-from@1.1.1
├─ diff@4.0.2
├─ make-error@1.3.6
├─ prisma2@2.0.0-preview023
├─ source-map-support@0.5.16
├─ source-map@0.6.1
├─ ts-node@8.6.2
├─ typescript@3.8.3
└─ yn@3.1.1
Done in 14.26s.
yarn add v1.22.0
warning ..\..\package.json: No license field
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
success Saved 1 new dependency.
info Direct dependencies
└─ @prisma/client@2.0.0-preview023
info All dependencies
└─ @prisma/client@2.0.0-preview023
Done in 2.04s.

✔ Installed the @prisma/client and prisma2 packages in your project
✔ Generated Prisma Client to .\node_modules\@prisma\client in 90ms

You can now start using Prisma Client in your code:

´´´
import { PrismaClient } from '@prisma/client'
// or const { PrismaClient } = require('@prisma/client')

const prisma = new PrismaClient()
´´´

Explore the full API: http://pris.ly/d/client

C:\Users\Jan\Documents\throwaway\560_3>"./node_modules/.bin/ts-node" index.ts
PrismaClientKnownRequestError:
Invalid `prisma.users.count()` invocation in
C:\Users\Jan\Documents\throwaway\560_3\index.ts:7:36

Authentication failed against database server at `localhost`, the provided database credentials for `m` are not valid.      

Please make sure to provide valid database credentials for the database server at `localhost`.
    at PrismaClientFetcher.request (C:\Users\Jan\Documents\throwaway\560_3\node_modules\@prisma\client\index.js:87:17)      
    at processTicksAndRejections (internal/process/task_queues.js:85:5) {
  code: 'P1000',
  meta: { database_user: 'm', database_host: 'localhost' }
}
^CTerminate batch job (Y/N)? ^C

C:\Users\Jan\Documents\throwaway\560_3>npm install uid

> prisma2@2.0.0-preview023 install C:\Users\Jan\Documents\throwaway\560_3\node_modules\prisma2
> node download-build/index.js

npm WARN 560_3@1.0.0 No description
npm WARN 560_3@1.0.0 No repository field.

+ uid@1.0.0
added 1 package from 1 contributor, removed 1 package, updated 10 packages and audited 11 packages in 12.514s
found 0 vulnerabilities

C:\Users\Jan\Documents\throwaway\560_3>"./node_modules/.bin/ts-node" index.ts

C:\Users\Jan\Documents\throwaway\560_3\node_modules\ts-node\src\index.ts:421
    return new TSError(diagnosticText, diagnosticCodes)
           ^
TSError: ⨯ Unable to compile TypeScript:
index.ts:1:30 - error TS2307: Cannot find module '@prisma/client'.

1 import { PrismaClient } from '@prisma/client'
                               ~~~~~~~~~~~~~~~~

    at createTSError (C:\Users\Jan\Documents\throwaway\560_3\node_modules\ts-node\src\index.ts:421:12)
    at reportTSError (C:\Users\Jan\Documents\throwaway\560_3\node_modules\ts-node\src\index.ts:425:19)
    at getOutput (C:\Users\Jan\Documents\throwaway\560_3\node_modules\ts-node\src\index.ts:530:36)
    at Object.compile (C:\Users\Jan\Documents\throwaway\560_3\node_modules\ts-node\src\index.ts:735:32)
    at Module.m._compile (C:\Users\Jan\Documents\throwaway\560_3\node_modules\ts-node\src\index.ts:814:43)
    at Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Object.require.extensions.<computed> [as .ts] (C:\Users\Jan\Documents\throwaway\560_3\node_modules\ts-node\src\index.ts:817:12)
    at Module.load (internal/modules/cjs/loader.js:643:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:839:10)

C:\Users\Jan\Documents\throwaway\560_3>
Jolg42 commented 4 years ago

What is probably happening is that generate uses yarn later the npm install does not pick up and thus remove the @prisma/client from that

janpio commented 4 years ago

Possible reason: https://github.com/prisma/prisma-client-js/issues/561

janpio commented 4 years ago

I think you wanted to close this as well with your commit message @Jolg42

Jolg42 commented 4 years ago

Yes indeed I also wanted to post this: fixed in alpha 920!