Skip to content

Configuration Languages: TS_NODE_PROJECT not working #2733

Closed

Description

@akopchinskiy

Original docs: https://webpack.js.org/configuration/configuration-languages/#typescript

All dependencies is installed in accordance with the documentation.
But when I use this:

"scripts": {
    "build": "TS_NODE_PROJECT=\"tsconfig-for-webpack-config.json\" webpack"
  }

I receive this error:
"TS_NODE_PROJECT" unrecognized command

Activity

  1. SeanPercy commented on Jan 11, 2019

    @SeanPercy

    @akopchinskiy I faced a similar issue. The following worked for me:

    • run npm i cross-env
    • change the script to "build": "cross-env TS_NODE_PROJECT=\"tsconfig-for-webpack-config.json\" webpack"
  2. EugeneHlushko commented on Jan 14, 2019

    @EugeneHlushko
    Contributor

    @akopchinskiy can you confirm that adding cross-env like Sean suggests solves your issue?

  3. akopchinskiy commented on Jan 14, 2019

    @akopchinskiy
    Author

    @EugeneHlushko yes it's working.
    But there is another problem with it: webpack.config.json uses global environments and when I use cross-env TS_NODE_PROJECT=... one of my webpack configs conmpiled incorrectly, because I have different tsconfig.json's as well.

  4. SeanPercy commented on Jan 14, 2019

    @SeanPercy

    @akopchinskiy I guess you mean something like setting the mode to production / development, right?

    This is my folder structure:

        |-- dist
        |-- node_modules
        |-- src
        |-- tests
            |-- testProductionBuild.ts
        |-- tools
            |-- .stylelintrc
            |-- babel.config.js
            |-- nodemon.json
            |-- tslint.json
            |-- tsconfig-for-webpack-config.json
            |-- webpackConfig
                |-- common.ts
                |-- development.ts
                |-- production.ts
        |-- .babelrc
        |-- .env
        |-- .gitignore
        |-- package-lock.json
        |-- package.json
        |-- tsconfig.json
        |-- webpack.config.ts
    
     
    

    And here is an excerpt of my package.json

      "scripts": {
        "webpack-ts": "cross-env TS_NODE_PROJECT=%npm_package_config_TS_NODE_PROJECT%",
        "build": "npm run webpack-ts NODE_ENV=production webpack",
        "pretest-build": "npm run build",
        "test-build": "ts-node --project %npm_package_config_TS_NODE_PROJECT% tests/testProductionBuild.ts",
        "server": "npm run webpack-ts NODE_ENV=development webpack-dev-server",
      },
      "config": {
        "TS_NODE_PROJECT": "\"tools/tsconfig-for-webpack-config.json\""
      },
    

    The tsconfig.json file on the root level is used by my IDE and tslint for linting the entire project.
    The tools/tsconfig-for-webpack-config.json file is only used to run webpack.config.ts and test/testProductionBuild.ts

  5. hakobpogh commented on May 15, 2019

    @hakobpogh

    Hi there. I have a .ts config in two different projects one is with the following versions:

        "webpack": "^4.29.0",
        "webpack-cli": "^3.2.1"
    

    and the other is with the following versions:

        "webpack": "^4.31.0",
        "webpack-cli": "^3.3.2"
    

    But for the first one I don't need to add TS_NODE_PROJECT=\"tsconfig-for-webpack-config.json\" but for the second one, it's required. I think that it's better to make it work without this additional stuff.

  6. aryzing commented on Aug 24, 2019

    @aryzing
    Contributor

    @hakobpogh how, then, can you use different TypeScript configs for webpack and for your code?

  7. eduard-malakhov commented on May 1, 2020

    @eduard-malakhov

    @aryzing, though this is an old issue, maybe this would still be useful. I've had the exact same issue and I managed to resolve it by adding delete process.env.TS_NODE_PROJECT; at the top of my webbacp.config.ts as suggested in jonaskello/tsconfig-paths-webpack-plugin#17. This hack basically clears the TS_NODE_PROJECT variable for any code inside the config, including ts-loader which will then use the default tsconfig.json.

  8. wrightwriter commented on Aug 18, 2023

    @wrightwriter

    I solved this by modifying my tsconfig as per the docs

    {
      "compilerOptions": {
         // stuff
      }
      "ts-node": {
        "compilerOptions": {
          "module": "CommonJS",
          "target": "ESNext",
          "esModuleInterop": true,
          "moduleResolution": "node",
          "verbatimModuleSyntax": false
        }
      },
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions