9

I have this schema in file data.ts

import mongoose from "mongoose";
const Schema = mongoose.Schema;

const DataSchema = new Schema(
    {
        id: Number,
        message: String,
    },
    { timestamps: true }
);

module.exports = mongoose.model('Data', DataSchema);

When I try to import this in my index.js as :

const Data = require('./data');

I keep seeing this error:

internal/modules/cjs/loader.js:584
    throw err;
    ^

Error: Cannot find module './data'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
    at Function.Module._load (internal/modules/cjs/loader.js:508:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)

Any ideas why this is.

I've tried doing:

export const data = mongoose.model('Data', DataSchema);

then importing with import { data as Data } from './data'

But that also returns the following error:

import { data as Data } from './data'
       ^

SyntaxError: Unexpected token {
    at new Script (vm.js:80:7)
    at createScript (vm.js:274:10)
    at Object.runInThisContext (vm.js:326:10)
    at Module._compile (internal/modules/cjs/loader.js:664:28)
5
  • you have data.ts typescript file which you are importing in index.js javascript file? O_o Commented Aug 21, 2019 at 3:48
  • Right - updated both to .ts however still get the same errors when I try both as in my question Commented Aug 21, 2019 at 3:54
  • how you are executing index.ts file? Commented Aug 21, 2019 at 3:55
  • With node index.ts Commented Aug 21, 2019 at 3:58
  • Typescript is a superset of JavaScript. 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. Commented Aug 21, 2019 at 4:00

3 Answers 3

5

Quick solution:

Use ts-node to run your TypeScript files directly

npm install --dev ts-node

Start a node process with this command:

node -r ts-node/register index.ts

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"
  }
}

About TypeScript:

  1. TypeScript actually is a superset (not a subset) of JavaScript. typescript

  2. .ts files must be compiled into JavaScript before running on any JavaScript environment (NodeJS, browser,...).

  3. Normally in a TypeScript project, we will have a build command in package.json, which will compile your .ts files into .js files, follow with a start command to start a node process with compiled js file:

{
  "scripts": {
    "build": "tsc index.ts",
    "start": "node index.js",
    "start:dev": "node -r ts-node/register index.ts"
  }
}
  1. Or you can run .ts files directly with ts-node.
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks - much clearer. So basically I have having issues because I'm using typescripts style of importing and exporting but not actually compiling it into JS?
Yes, NodeJS only work with JS, and TS actually has something more just than import/export module system: type-system, decorators,...
0

If you are using typescript try importing like this :

import { Data} from "./Data";

let myData= new Data();

2 Comments

data is user created file. You cannot import it using 'data' as this will look in global or node_modules only. For user created modules, you need to provide relative or absolute path of the file. e.g. './data' or '/path/to/module/data'
Edited my answer. Please check
-4

You need to export your model like this,

const Data = mongoose.model('Data', DataSchema);

export default Data;

And can import it in any file like,

import Data from "./Data";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.