简体   繁体   中英

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

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 (ie 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM