Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
54
rated 0 times [  60] [ 6]  / answers: 1 / hits: 5973  / 4 Years ago, wed, september 9, 2020, 12:00:00

I am trying to run node 14 with my package.json set as module:


"type": "module",

If I run this command on a typescript file:


 node --loader ts-node/esm.mjs --experimental-top-level-await ./src/scripts/ts-build.ts --trace-warnings --experimental-json-modules

and I have extensionless imports like this in a typescript file


import { logger } from './logger';

I get



ERR_MODULE_NOT_FOUND



But if I change it to


import { logger } from './logger.js';

It works.


Why is this?


More From » node.js

 Answers
10

If we take a look at the spec there's this section which states:



The current specifier resolution does not support all default behavior
of the CommonJS loader. One of the behavior differences is automatic
resolution of file extensions and the ability to import directories
that have an index file.



There's another section which states:



A file extension must be provided when using the import keyword.
Directory indexes (e.g. './startup/index.js') must also be fully
specified.



So it seems that the extension is actually necessary. Howvever, there's the option --experimental-specifier-resolution which you try setting to --experimental-specifier-resolution=node.


[#2717] Thursday, September 3, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
isaacvalentinn

Total Points: 325
Total Questions: 120
Total Answers: 131

Location: North Korea
Member since Tue, Jun 16, 2020
4 Years ago
isaacvalentinn questions
Mon, Jan 18, 21, 00:00, 3 Years ago
Mon, Nov 23, 20, 00:00, 4 Years ago
Wed, Sep 23, 20, 00:00, 4 Years ago
;