[Solved-4 Solutions] Typescript getting error TS2304: cannot find name ' require'



Error Description:

    • When we try to get a TypeScript running, we get this error: "TS2304: Cannot find name 'require' " when we attempt to transpile a simple ts node page. The contents of this file are:
    'use strict';
    
    /// <reference path="typings/tsd.d.ts" />
    
    /*  movie.server.model.ts - definition of movie schema */
    
    var mongoose = require('mongoose'),
    Schema = mongoose.Schema;
    
    var foo = 'test';
     
    click below button to copy the code. By - nodejs tutorial - team
    • The error is thrown on the var mongoose=require('mongoose') line
    • The contents of the typings/tsd.d.ts file are:
    /// <reference path="node/node.d.ts" />
    /// <reference path="requirejs/require.d.ts" />
     
    click below button to copy the code. By - nodejs tutorial - team
    • The .d.ts file references were placed in the appropriate folders and added to typings/tsd.d.ts by the commands:
    tsd install node --save
    tsd install require --save
     
    click below button to copy the code. By - nodejs tutorial - team

    Solution 1:

      • 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
       
      click below button to copy the code. By - nodejs tutorial - team

      Solution 2:

        • For TypeScript 2.x, there are now two steps:
          • Install a package that defines require. For example:
        npm install @types/node --save-dev
         
        click below button to copy the code. By - nodejs tutorial - team
        • Tell TypeScript to include it globally in
        tsconfig.json:
        {
            "compilerOptions": {
              "types": ["node"]
            }
        }
         
        click below button to copy the code. By - nodejs tutorial - team
        • 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.

        Solution 3:

          • You can
          declare var require: any
           
          click below button to copy the code. By - nodejs tutorial - team
          • Also, instead of var mongoose = require('mongoose'), you could try the following
          import mongoose from 'mongoose' // or
          import mongoose = require('mongoose')
          
           
          click below button to copy the code. By - nodejs tutorial - team

          Solution 4:

            • Instead of:
            'use strict';
            
            /// <reference path="typings/tsd.d.ts" />
             
            click below button to copy the code. By - nodejs tutorial - team
            • Try:
            /// <reference path="typings/tsd.d.ts" />
            
            'use strict';
             
            click below button to copy the code. By - nodejs tutorial - team

            Related Searches to Typescript getting error TS2304: cannot find name ' require'