Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript/Node: Error [ERR_MODULE_NOT_FOUND]: Cannot find module

I'm seeing the error I specified in my title and none of the existing solutions here seem to help, so I'm hoping someone can give me insight as to what is going on.

I am using Typescript and Node in my project. TS compiles everything just fine...I end up with the following as expected:

projectHome/
   dist/
      schema/
          schema.js
      index.js

When I run node ./dist/index.js from project home, I get the error cannot find module '/home/me/projectHome/dist/schema/schema' imported from '/home/me/projectHome/dist/index.js'

Relative imports in index.js are as follows:

    import express from 'express';
    import { ApolloServer } from 'apollo-server-express';
    import typeDefs from './schema/schema';

My schema.ts file contains:

    import { ApolloServer, gql } from 'apollo-server-express'
    import { GraphQLScalarType } from 'graphql'

        const typeDefs = gql`
           ...(edited for brevity/sanity)
        `

    export default typeDefs

and my typescript file (should this matter at this point since it is Node that is failing??) looks like this:

    {
        "compilerOptions": {
        "target": "ES6",                         
        "module": "ES6",                         
        "lib": ["ES6"],                          
        "allowJs": true,                         
        "sourceMap": true,                       
        "outDir": "./dist",                      
        "rootDir": "./src",                      
        "strict": true,                          
        "skipLibCheck": true,                    
        "forceConsistentCasingInFileNames": true 
    },

    "files": ["src/@types/graphql.d.ts"],
    "include": ["src/**/*", "serverInfo.json"],
    "exclude": ["node_modules", "**/*.spec.ts"]
    }

Note that I cannot use commonjs because some objection related code fails when I do. Is the problem actually related to using ES6 modules, or is something else wrong?

Thank in advance!

-Chris

like image 890
Chris K. Avatar asked Jan 03 '21 15:01

Chris K.


People also ask

Can not find module TS?

To solve the cannot find module 'typescript' error, make sure to install typescript globally by running the npm i -g typescript command and create a symbolic link from the globally-installed package to node_modules by running the npm link typescript command. Copied!

What does it mean to use typescript with ES modules?

That means that typescript will compile your code to use ES modules. To run nodejs with ES modules you need v12 of nodejs and run it with the --experimental-modules flag or have nodejs version 13.2.0+ to run without the --experimental-modules flag.

How to run a typescript file in node?

Node does not understand typescript files. You need to first convert typescript files to JavaScript and then use generated index.js file to run your program. Make sure your package.json should contain something like these: { "dependencies": { "mongoose": "^5.6.10", "typescript": "^3.5.3" }, "devDependencies": { "ts-node": "^8.3.0" } }

How do I run NodeJS with ES modules?

To run nodejs with ES modules you need v12 of nodejs and run it with the --experimental-modules flag or have nodejs version 13.2.0+ to run without the --experimental-modules flag. If you cannot use newer versions of the nodejs you need to change "module": "es2020" to "module": "CommonJS".

Is it possible to import data from TS to NodeJS?

Yes, NodeJS only work with JS, and TS actually has something more just than import/export module system: type-system, decorators ,... data is user created file. You cannot import it using 'data' as this will look in global or node_modules only.


Video Answer


1 Answers

You need to use fully-qualified imports when feeding ES6 modules to Node.js.

In your case, this means adding the .js extension to your schema import:

 import express from 'express';
 import { ApolloServer } from 'apollo-server-express';
-import typeDefs from './schema/schema';
+import typeDefs from './schema/schema.js';

Your confusion likely comes from the fact that this requirement has changed between the traditional, require()-style references (called "CommonJS", and detailed more here), and the more modern ECMAScript Modules — but in the interim, a lot of tools that converted between one and the other would handle this problem for you (i.e. Webpack and friends.) Now that these features are landing in a first-class fashion in Node, you're running into some additional things that older tools were Magically™ doing for you, but don't actually work that way in the spec!

like image 147
ELLIOTTCABLE Avatar answered Oct 19 '22 20:10

ELLIOTTCABLE