TypeStrong / grunt-ts

A grunt task to manage your complete typescript development to production workflow
https://www.npmjs.com/package/grunt-ts
MIT License
330 stars 121 forks source link

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

Open ayelsew opened 4 years ago

ayelsew commented 4 years ago
"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

> backend@1.0.0 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! backend@1.0.0 grunt: `grunt`
npm ERR! Exit status 3
npm ERR! 
npm ERR! Failed at the backend@1.0.0 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

nycdotnet commented 4 years ago

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