Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ts-node docs tweaks #6154

Open
cspotcode opened this issue May 19, 2022 · 6 comments
Open

ts-node docs tweaks #6154

cspotcode opened this issue May 19, 2022 · 6 comments

Comments

@cspotcode
Copy link

cspotcode commented May 19, 2022

Feature to document

I noticed that the recommendations for using ts-node to load webpack.config.ts can be simplified:
https://webpack.js.org/configuration/configuration-languages/

First, TS_NODE_PROJECT environment variable is supported natively by ts-node; no need to install tsconfig-paths

Second, the recommended configuration should be updated to recommend using moduleTypes instead of compilerOptions

https://typestrong.org/ts-node/docs/module-type-overrides

moduleTypes is better because it not only modifies the emitted code to be CommonJS, it also tells node to execute the files as CommonJS. This avoids node errors about attempting to require() ESM modules.

Note that I've also merged support for CJS and CTS file extensions, to be published in our next release in a week or two. So if webpack supports webpack.config.cts, then that is an even simpler option. With a webpack.config.cts, moduleTypes is not necessary unless the config tries to import other files that are ESM TS.

Author(s)

Additional information

[x] I am willing to work on this issue and submit a pull request.

@cspotcode
Copy link
Author

I suppose one other thing worth clarifying, this is technically incorrect, though it may not be worth changing:

This occurs because ts-node does not support any module syntax other than commonjs.

We do fully support native ESM module syntax. But since webpack can't install a node --loader at runtime, the pragmatic hoice is to run as CJS instead.

@snitin315
Copy link
Member

Can you share the link where on https://webpack.js.org we have to make updates?

@cspotcode
Copy link
Author

Yes, apologies I forgot to include a link. Here it is:

https://webpack.js.org/configuration/configuration-languages/

@cspotcode
Copy link
Author

Linking to the issue and PR that add cts support to interpret:

gulpjs/interpret#89
gulpjs/interpret#90

@olyop
Copy link

olyop commented Jul 25, 2022

I ran into this problem today as well and noticed the docs we're outdated.

You can specify a loader by using the NODE_OPTIONS environment variable:

{
  ...
  "scripts": {
    ...
    "build": "cross-env NODE_OPTIONS=\"--loader ts-node/esm\" webpack ..."
  }
}

This also allows TS_NODE_PROJECT to be passed in as well to config ts-node.
In the current version of Node (18) this throws a warning but it can be hidden by passing the cli arguments: --no-warnings --experimental-specifier-resolution=node.

I agree with @cspotcode points and this section needs to be tweaked or re-written as there are better ways of using ts-node now.

@snitin315
Copy link
Member

Makes sense, currently we have this information mentioned here https://webpack.js.org/api/cli/#troubleshooting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants