Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

ERR_MODULE_NOT_FOUND using es modules #1777

Closed
Ziothh opened this issue May 29, 2022 · 6 comments
Closed

ERR_MODULE_NOT_FOUND using es modules #1777

Ziothh opened this issue May 29, 2022 · 6 comments

Comments

@Ziothh
Copy link

Ziothh commented May 29, 2022

Search Terms

ts-node, ERR_MODULE_NOT_FOUND

Expected Behavior

run my typescript es module files.

Actual Behavior

I have a project that looks like this

  • package.json
  • tsconfig.json
  • src/main.ts
  • src/module.ts

I have a "start" script in my package.json that I run via pnpm start that executes
ts-node ./src/main.ts. (I have also tried this with npm and it also didn't work)

When I run this I get the error "Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.". So I do as I'm told and I add it to my package.json and run it again.

Now I get this error: "TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for [path-to-project]/src/main.ts"

I manage to resolve this error by adding "--esm" to my start script like this: ts-node --esm ./src/main.ts. If I run my start script I get a new error: "Cannot find module '[path-to-project]/src/module' imported from [path-to-project]/src/main.ts"

I can surpass this error by doing this:

// main.ts
// -- 
import { myLog } from "./module"
// --
// ++ 
// ** Add a ts-ignore because typescript doesn't like me adding the file extension. */
// @ts-ignore
import { myLog } from "./module.ts"
// ++

Now my code runs but I have some issues with this

  • I don't like adding ".ts" behind every import. This defeats the purpose of auto-imports
  • Typescript really doesn't like me adding a file extension in an import statement.

How can I make my code work with ts-node by removing the ".ts" extension in the import statement?

Minimal reproduction

// main.ts
import { myLog } from "./module"

console.log("hiii")
myLog()
// module.ts
export const myLog = () => console.log("module imported")

Specifications

  • ts-node version: 10.8.0 | 10.7.0 (tried both versions)
  • node version: 16.15.0 | 18.2.0 (switched using nvm)
  • TypeScript version: 4.7.2
  • tsconfig.json:
{
    "compilerOptions": {
        "target": "ESNext",
        "module": "ESNext",
        "skipLibCheck": true,
        "sourceMap": true,
        "outDir": "./dist",
        "moduleResolution": "node",
        "removeComments": true,
        "noImplicitAny": false,
        "strictNullChecks": true,
        "strictFunctionTypes": true,
        "noImplicitThis": true,
        "noUnusedLocals": false,
        "noUnusedParameters": false,
        "noImplicitReturns": true,
        "noFallthroughCasesInSwitch": true,
        "allowSyntheticDefaultImports": true,
        "esModuleInterop": true,
        "experimentalDecorators": true,
        "allowJs": true,
        "emitDecoratorMetadata": true,
        "strict": false,
        "resolveJsonModule": true,
        "typeRoots": ["src/types", "./node_modules/@types"],
        "baseUrl": ".",
        "paths": {
            "@entities": ["src/entities/index.ts"],
            "@entities/*": ["src/entities/*"],
            "@config/*": ["src/config/*"],
            "@helpers/*": ["src/helpers/*"],
            "@mytypes/*": ["src/types/*"],
            "@server/*": ["src/server/*"]
        }
    },
    "exclude": ["node_modules"],
    "include": ["./src/**/*.ts"]
}
  • package.json:
{
    "type": "module",
    "devDependencies": {
        "@types/node": "^17.0.36",
        "ts-node": "10.7.0",
        "typescript": "^4.7.2"
    },
    "scripts": {
        "start": "ts-node --esm ./src/main.ts"
    }
}
  • Operating system and version: MacOS
@Ziothh Ziothh changed the title ERR_MODULE_NOT_FOUND ERR_MODULE_NOT_FOUND using es modules May 29, 2022
@cspotcode
Copy link
Collaborator

https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/#type-in-package-json-and-new-extensions

That section has a detailed breakdown of how file extensions work. I recommend reading it thoroughly. Remember that this is the way that TypeScript has chosen to do things, so ts-node follows along with those rules. Even if you are not using "module": "NodeNext", those rules still apply.

Worth noting also that the --esm flag can be configured in your tsconfig.json if you prefer putting stuff in tsconfig instead of passing command-line flags. Your choice, whichever you prefer.

When I run this I get the error "Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.".

Can you copy-paste the complete terminal output that you see when getting this error? I suspect that there is additional context, perhaps some lines of output preceding or following that error. It will help to know what is giving you this error. I suspect node is giving it to you, but could also be typescript.

@Ziothh
Copy link
Author

Ziothh commented May 30, 2022

Thanks for the quick response.

I've read the docs you sent me. So Node (& Typescript also) want you to always specify the file extension, right? Even though I've specified my module type in package.json.

Visual Studio Code yells at me if I do this. It's running Typescript version 4.7.2
The error: An import path cannot end with a '.ts' extension. Consider importing './module.js' instead.ts(2691).

This error also also gets logged in the console if I run my files via ts-node
Screenshot 2022-05-30 at 12 51 15

I only manage to get my code working if I prepend my import statements with a "// @ ts-ignore"

Can you copy-paste the complete terminal output that you see when getting this error? I suspect that there is additional context, perhaps some lines of output preceding or following that error. It will help to know what is giving you this error. I suspect node is giving it to you, but could also be typescript.

Here are the errors I get before adding "type": "module"
Screenshot 2022-05-30 at 12 55 24

Worth noting also that the --esm flag can be configured in your tsconfig.json if you prefer putting stuff in tsconfig instead of passing command-line flags. Your choice, whichever you prefer.

Thanks. I'll add it to my tsconfig

@cspotcode
Copy link
Collaborator

My guess is you're still stuck putting a .ts extension in your import statements when you should put a .js extension instead. Try it even if it seems wrong; it'll work.

@Ziothh
Copy link
Author

Ziothh commented May 31, 2022

Sadly this works. Thanks for the help even though this isn't a ts-node problem but a typescript problem.

It feels really wrong to put a ".js" extension behind my ts imports... I'm also bummed out that my auto imports don't auto add the extension.

Hopefully I'll find some kind of import resolver that just adds the extension behind the scenes... For now switching to "module": "Node16 in my tsconfig does the job.

@Ziothh Ziothh closed this as completed May 31, 2022
@cspotcode
Copy link
Collaborator

Auto-imports can be configured to add the extension. There's a vscode setting for it; they also automatically mimic style of the other imports in the file.

@Ziothh
Copy link
Author

Ziothh commented May 31, 2022

Auto-imports can be configured to add the extension. There's a vscode setting for it; they also automatically mimic style of the other imports in the file.

Alright I'll have a look.

@cspotcode cspotcode mentioned this issue May 31, 2022
@TypeStrong TypeStrong locked and limited conversation to collaborators May 31, 2022
@cspotcode cspotcode converted this issue into discussion #1781 May 31, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants