grunt-ts icon indicating copy to clipboard operation
grunt-ts copied to clipboard

error TS2732 ...Consider using '--resolveJsonModule' to import module with '.json' extension

Open ayelsew opened this issue 5 years ago • 1 comments
trafficstars

"grunt": "^1.3.0",
"grunt-ts": "^6.0.0-beta.22",
"typescript": "^3.9.7"

I'm trying to transpile my code typescript using grunt-ts with tsconfig.file. When I run the command npm run grunt the follow error was shown:

➜  backend git:(master) ✗ npm run grunt

> [email protected] grunt /home/leydev/MEGAsync/Projects/blog/backend
> grunt

Running "ts:default" (ts) task
Compiling...
Using tsc v3.9.7
src/routers/schemas/index.ts(1,22): error TS2732: Cannot find module './postUser.schema.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
src/routers/schemas/index.ts(2,27): error TS2732: Cannot find module './patchUserById.schema.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
src/routers/schemas/index.ts(3,22): error TS2732: Cannot find module './posts/postPost.schema.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
src/routers/schemas/index.ts(4,27): error TS2732: Cannot find module './posts/patchPostById.schema.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
src/routers/schemas/index.ts(5,25): error TS2732: Cannot find module './auth/postAuthAPI.schema.json'. Consider using '--resolveJsonModule' to import module with '.json' extension

>> 5 non-emit-preventing type warnings  
>> Error: tsc return code: 2
Warning: Task "ts:default" failed. Use --force to continue.

Aborted due to warnings.
npm ERR! code ELIFECYCLE
npm ERR! errno 3
npm ERR! [email protected] grunt: `grunt`
npm ERR! Exit status 3
npm ERR! 
npm ERR! Failed at the [email protected] grunt script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/leydev/.npm/_logs/2020-08-30T05_44_28_407Z-debug.log

In tsconfig.json already has the option resolveJsonModule, but was ignored.

{
    "compilerOptions": {
        "module": "commonjs",
        "resolveJsonModule": true,
        "esModuleInterop": true,
        "target": "ES5",
        "allowJs": true,
        "moduleResolution": "node",
        "sourceMap": true,
        "outDir": "dist",
        "rootDir": "src",
        "strict": true
    },
    "include": [
        "src/**/*",
        "src/**/*.json"
    ],
    "exclude": [
        "node_modules",
        "**/*.test.ts",
        "jest.config.js"
    ]
}

This is the gruntfile.js:

module.exports = (grunt) => {
  grunt.initConfig({
    ts: {
      default: {
        tsconfig: './tsconfig.json',
      },
    },
  });
  grunt.loadNpmTasks('grunt-ts');
  grunt.registerTask('default', ['ts']);
};

If I run only tsc, the process runs successfully

ayelsew avatar Aug 30 '20 05:08 ayelsew

in the readme there is a way to pass tsconfig as an object. What you want to do is pass it that way in your Gruntfile and use passthrough true. The docs have the exact syntax

nycdotnet avatar Aug 30 '20 13:08 nycdotnet