最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ERR_MODULE_NOT_FOUND if no .js extension - Stack Overflow

programmeradmin0浏览0评论

I have a typescript project where I get the ERROR_MODULE_NOT_FOUND 'path/to/piled/file' when I try to run my piled app.js file with node. However, I get rid of this error if I add a .js extension to the import statement in my app.js like this import { function } from "./path/file.js"; How do I get the typescript piler to automatically add these .js extensions? Alternatively, get node to work without the .js extensions?

My tsconfig.json looks like this:

{
    "pilerOptions": {
        "module": "esnext",
        "target": "es5",
        "noImplicitReturns": true,
        "noUnusedLocals": true,
        "outDir": "dist",
        "sourceMap": true,
        "moduleResolution": "node",
        "esModuleInterop": true,
        "isolatedModules": true,
        "resolveJsonModule": true,
        "typeRoots": ["./src/types", "node_modules/@types"],
        "baseUrl": "./src",
        "paths": {
            "types": ["types"],
            "types/*": ["types/*"],
            "@data/*": ["data/*"],
            "@execute/*": ["execute/*"],
            "@indicators/*": ["indicators/*"],
            "@services/*": ["services/*"],
            "@strategies/*": ["strategies/*"],
            "*": ["node_modules/*"]
        }
    },
    "pileOnSave": true,
    "include": ["src", "test", "dist"],
    "exlude": ["src/types"]
}

My app.ts looks like this:

import { function } from "./path/file";

function();
console.log("test");

export {};

I have a typescript project where I get the ERROR_MODULE_NOT_FOUND 'path/to/piled/file' when I try to run my piled app.js file with node. However, I get rid of this error if I add a .js extension to the import statement in my app.js like this import { function } from "./path/file.js"; How do I get the typescript piler to automatically add these .js extensions? Alternatively, get node to work without the .js extensions?

My tsconfig.json looks like this:

{
    "pilerOptions": {
        "module": "esnext",
        "target": "es5",
        "noImplicitReturns": true,
        "noUnusedLocals": true,
        "outDir": "dist",
        "sourceMap": true,
        "moduleResolution": "node",
        "esModuleInterop": true,
        "isolatedModules": true,
        "resolveJsonModule": true,
        "typeRoots": ["./src/types", "node_modules/@types"],
        "baseUrl": "./src",
        "paths": {
            "types": ["types"],
            "types/*": ["types/*"],
            "@data/*": ["data/*"],
            "@execute/*": ["execute/*"],
            "@indicators/*": ["indicators/*"],
            "@services/*": ["services/*"],
            "@strategies/*": ["strategies/*"],
            "*": ["node_modules/*"]
        }
    },
    "pileOnSave": true,
    "include": ["src", "test", "dist"],
    "exlude": ["src/types"]
}

My app.ts looks like this:

import { function } from "./path/file";

function();
console.log("test");

export {};
Share Improve this question edited Apr 8, 2021 at 20:05 Victor Gunnarsson asked Apr 8, 2021 at 19:47 Victor GunnarssonVictor Gunnarsson 3202 silver badges12 bronze badges 6
  • Are there multiple files with the same base name in the directory the module is in? Are you putting .js for the modules in the typescript file? – Nico Nekoru Commented Apr 8, 2021 at 20:03
  • Thank you for your ment! I have edited my post including the app.ts code. I am not using an extension inside my typescript file. Furthermore, there are two files with the same base name in the module: api.js and api.js.map, both of them are created by the piler. – Victor Gunnarsson Commented Apr 8, 2021 at 20:08
  • The issue without the extension is probably due to ambiguity of which file to import from since there are multiple files it can do so from. I also assume that ts will pile with extension if extension is added to the ts file – Nico Nekoru Commented Apr 8, 2021 at 20:11
  • If I add a .js extension in the typescript file I get unable to resolve path, if I remove the api.js.map file it still doesn't work... – Victor Gunnarsson Commented Apr 8, 2021 at 20:16
  • Does stackoverflow./questions/55251956/… answer your question? – Nico Nekoru Commented Apr 8, 2021 at 20:21
 |  Show 1 more ment

3 Answers 3

Reset to default 3

I was having the exact same issue while trying to setup a new typescript project on node.

What solved it for me at the end was replacing esnext by monjs on the module option.

I had previously used esnext because of a top-level await, but worked that around by wrapping the await with an async function.

I cannot explain why the piled javascript imports with esnext does not have the .js extension, or how to tweak the piler to add this extension for local modules that will also be piled.

I have got the same error. The ending ".js" in the typescript project looks weird, but without it does not work when I tried to start the app like that:

"start": "node ."

If you add --experimental-specifier-resolution=node then it will work. I am not sure whether it is a good practice to add something "experimental" to the app, but you can use it as a temporary solution.

"start": "node --experimental-specifier-resolution=node ."

Also, the node v18 is warning you: ExperimentalWarning:

The Node.js specifier resolution flag is experimental. It could change or be removed at any time

When using ESNext, you need to specify full path with the extension.

发布评论

评论列表(0)

  1. 暂无评论