4

I am converting the code to use nodemon to leverage TypeScript.

In my package.json:

  "scripts": {
    "serve-fake-api": "nodemon fake-api/server.ts --watch 'fake-api/*.*'  ",
    "serve-vue": "vue-cli-service serve",
    "serve": "concurrently -k \"npm run serve-fake-api\" \"npm run serve-vue\"",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },

and the fake-api/server.ts file:

import { readFileSync } from 'fs';
import { create, defaults, bodyParser, rewriter, router as _router } from 'json-server';
import { join } from 'path';

const server = create();
const defaultMiddleware = defaults();

// It is recommended to use the bodyParser middleware before any other middleware in your application
server.use(bodyParser);

server.use(defaultMiddleware);

// Define custom routes (routes.json)
const routes = JSON.parse(readFileSync(join(__dirname, 'routes.json'), "utf8"));
server.use(rewriter(routes));

// Add custom middleware before JSON Server router
const customMiddleware = require(join(__dirname, 'middleware.ts'));
server.use(customMiddleware);

// This is where `json-server`'s magic happens ;)
const router = _router(join(__dirname, 'db.json'));

// Start the application by listening to port 3000,
// Although this won't print the nice starting message you see when
// running `json-server` as CLI command, it still runs the app correctly.
server.use(router);
server.listen(3000, () => {
  console.log('JSON Server is running')
});

but when running npm run serve:

[0] C:\Users\eperret\Desktop\tabulator-tests\fake-api\server.ts:1
[0] import { readFileSync } from 'fs';
[0] ^^^^^^
[0]
[0] SyntaxError: Cannot use import statement outside a module

I googled a bit and ended up here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

Is there a workaround to keep using this kind of import?

5
  • I think you need to tell nodemon to compiles the files to JS before reloading as explained here: stackoverflow.com/a/37979548/554021 . I also wonder why you're using TS but your server.ts file just looks like plain javascript Commented Nov 9, 2019 at 9:53
  • @Baruch it's a WIP from the version in JS. Commented Nov 9, 2019 at 11:18
  • @Baruch how can I use ts-code without installing it globally? Commented Nov 9, 2019 at 20:21
  • @Baruch actually I checked, it's already using ts-node... Commented Nov 12, 2019 at 8:27
  • Found out why, will post my answer Commented Nov 13, 2019 at 9:26

1 Answer 1

16

I answered to my question on the related GitHub issue thread: https://github.com/remy/nodemon/issues/1625#issuecomment-560115741

I solved my issue by changing the module type with commonjs in tsconfig.json instead of esnext:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
...
Sign up to request clarification or add additional context in comments.

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.