Skip to content
This repository has been archived by the owner on Jan 14, 2021. It is now read-only.

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

Closed
matthewmueller opened this issue Mar 11, 2020 · 6 comments
Assignees
Labels
bug/2-confirmed We have confirmed that this is a bug. kind/bug A reported bug.
Milestone

Comments

@matthewmueller
Copy link
Contributor

matthewmueller commented Mar 11, 2020

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.

@matthewmueller matthewmueller added the process/candidate Candidate for next Milestone. label Mar 11, 2020
@Jolg42 Jolg42 added bug/1-repro-available A reproduction exists and needs to be confirmed. kind/bug A reported bug. labels Mar 11, 2020
@janpio

This comment has been minimized.

@janpio
Copy link
Member

janpio commented Mar 11, 2020

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
Copy link
Member

Jolg42 commented Mar 11, 2020

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

@Jolg42 Jolg42 self-assigned this Mar 11, 2020
@matthewmueller matthewmueller added bug/2-confirmed We have confirmed that this is a bug. and removed bug/1-repro-available A reproduction exists and needs to be confirmed. labels Mar 11, 2020
@janpio janpio removed the process/candidate Candidate for next Milestone. label Mar 15, 2020
@janpio janpio added this to the Preview 25 milestone Mar 16, 2020
@janpio
Copy link
Member

janpio commented Mar 16, 2020

Possible reason: #561

Jolg42 added a commit to prisma/prisma that referenced this issue Mar 17, 2020
…e npm by default, use yarn only if there is a yarn.lock
@janpio
Copy link
Member

janpio commented Mar 17, 2020

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

@janpio janpio closed this as completed Mar 17, 2020
@Jolg42
Copy link
Member

Jolg42 commented Mar 17, 2020

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug/2-confirmed We have confirmed that this is a bug. kind/bug A reported bug.
Projects
None yet
Development

No branches or pull requests

3 participants