question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

set "type": "module" in `package.json` breaks the build

See original GitHub issue

It’ll get this error if you set the “type”: “module” in the package.json

node -r esbuild-register scripts/test.ts
internal/process/esm_loader.js:74
    internalBinding('errors').triggerUncaughtException(
                              ^

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /scripts/test.ts
    at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:65:15)
    at Loader.getFormat (internal/modules/esm/loader.js:101:42)
    at Loader.getModuleJob (internal/modules/esm/loader.js:230:31)
    at Loader.import (internal/modules/esm/loader.js:164:17)
    at Object.loadESM (internal/process/esm_loader.js:68:5) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:24
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
egoistcommented, Nov 9, 2022
2reactions
Savidcommented, Jul 3, 2022

I’ve just been playing with ES modules recently and I’ve had some success with the suggestion from @danny-andrews;

esbuild-register-loader.mjs

const extensionsRegex = /\.ts$|\.tsx$/;

export async function load(url, context, defaultLoad) {
  if (extensionsRegex.test(url)) {
    const { source } = await defaultLoad(url, { format: 'module' });
    return {
      format: 'commonjs',
      source: source,
    };
  }
  // let Node.js handle all other URLs
  return defaultLoad(url, context, defaultLoad);
}

then run;

node --experimental-loader ./esbuild-register-loader.mjs -r esbuild-register scripts/test.ts
Read more comments on GitHub >

github_iconTop Results From Across the Web

setting "type" to "module" in package.json then not able to ...
I resolved it adding a package.json in the server folder scope with { "type": "module" } and removing that property from the main ......
Read more >
Modules: Packages | Node.js v19.3.0 Documentation
Files with a .js extension when the nearest parent package.json file contains a top-level "type" field with a value of "module" .
Read more >
Creating Node.js modules - npm Docs
Node.js modules are a type of package that can be published to npm. ... To create a package.json file, on the command line,...
Read more >
How we employed the new ECMAScript Module Support in ...
js are loaded as ES modules when the nearest parent package.json file contains a top-level field “type” with a value of “module”. If...
Read more >
Documentation - TypeScript 4.7
type in package.json and New Extensions. Node.js supports a new setting in package.json called type . "type" can be set to either "module"...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found