TypeScript getting error TS2304: cannot find name ' require'

后端 未结 23 2542
Happy的楠姐
Happy的楠姐 2020-11-22 06:00

I am trying to get my first TypeScript and DefinitelyTyped Node.js application up and running, and running into some errors.

I am getting the error \"TS2304: Cannot

相关标签:
23条回答
  • 2020-11-22 06:33

    You can

    declare var require: any
    

    Or, for more comprehensive support, use DefinitelyTyped's require.d.ts

    Also, instead of var mongoose = require('mongoose'), you could try the following

    import mongoose from 'mongoose' // or
    import mongoose = require('mongoose')
    
    0 讨论(0)
  • 2020-11-22 06:33
    1. Did you specify what module to use to transpile the code?
      tsc --target es5 --module commonjs script.ts
      You must do that to let the transpiler know that you're compiling NodeJS code. Docs.

    2. You must install mongoose definitions as well
      tsd install mongoose --save

    3. Do not use var to declare variables (unless necessary, which is a very rare case), use let instead. Learn more about that

    0 讨论(0)
  • 2020-11-22 06:34

    Quick and Dirty

    If you just have one file using require, or you're doing this for demo purposes you can define require at the top of your TypeScript file.

    declare var require: any
    

    TypeScript 2.x

    If you are using TypeScript 2.x you no longer need to have Typings or Definitely Typed installed. Simply install the following package.

    npm install @types/node --save-dev
    

    The Future of Declaration Files (6/15/2016)

    Tools like Typings and tsd will continue to work, and we’ll be working alongside those communities to ensure a smooth transition.

    Verify or Edit your src/tsconfig.app.json so that it contains the following:

    ...
    "types": [ "node" ],
    "typeRoots": [ "../node_modules/@types" ]
    ...
    

    Make sure is the file in the src folder and no the one on the root app folder.

    By default, any package under @types is already included in your build unless you've specified either of these options. Read more

    TypeScript 1.x

    Using typings (DefinitelyTyped's replacement) you can specify a definition directly from a GitHub repository.

    Install typings

    npm install typings -g --save-dev
    

    Install the requireJS type definition from DefinitelyType's repo

    typings install dt~node --save --global
    

    Webpack

    If you are using Webpack as your build tool you can include the Webpack types.

    npm install --save-dev @types/webpack-env
    

    Update your tsconfig.json with the following under compilerOptions:

    "types": [
          "webpack-env"
        ]
    

    This allows you to do require.ensure and other Webpack specific functions.

    Angular CLI

    With CLI you can follow the Webpack step above and add the "types" block to your tsconfig.app.json.

    Alternatively, you could use the preinstalled node types. Keep in mind this will include additional types to your client-side code that are not really available.

    "compilerOptions": {
        // other options
        "types": [
          "node"
        ]
      }
    
    0 讨论(0)
  • 2020-11-22 06:36

    Sometime missing "jasmine" from tsconfig.json may cause this error. (TypeScript 2.X)

    So add

    "types": [
      "node",
      "jasmine"
    ]
    

    to your tsconfig.json file.

    0 讨论(0)
  • 2020-11-22 06:39

    For TypeScript 2.x, there are now two steps:

    1. Install a package that defines require. For example:

      npm install @types/node --save-dev
      
    2. Tell TypeScript to include it globally in tsconfig.json:

      {
          "compilerOptions": {
              "types": ["node"]
          }
      }
      

    The second step is only important if you need access to globally available functions such as require. For most packages, you should just use the import package from 'package' pattern. There's no need to include every package in the tsconfig.json types array above.

    0 讨论(0)
  • 2020-11-22 06:39

    I've been struggling from this issue as well. I believe that this works for all release candidates aka rc but I didn't test is though. For @angular rc.2 it works well.

    1. Add core-js as npm dependency in package.json
    2. run typings install core-js --save
    3. remove all "es6-shim" occurances in your package.json. You don't need it anymore.

    Cheers!

    0 讨论(0)
提交回复
热议问题