15

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

1 Answer 1

37

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!

Sign up to request clarification or add additional context in comments.

2 Comments

is not working on my case :/
I don't think I've ever had a worst developer experience. I don't know how it can be this complicated to import a file and use it lol

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.