Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
Problem importing a module from an express app in a script outside of express

I have a vanilla Express app whose package.json looks like this:

{
  "name": "express-app",
  "version": "1.0.0",
  "main": "app.js",
  "dependencies": {
    ...
  },
  "devDependencies": {
    ...
  },
  "scripts": {
    ...
  },
}

And I want to write a script outside the express app that imports a module from the express app. The module resides in ./src/model/Example.js That module also imports various other modules.

The script is located at ./scripts/test.mjs and does:

import Example from '../src/models/Example.js';

However, I'm getting this hitting the first import from Example.js:

SyntaxError: Cannot use import statement outside a module

How do I structure this such that the script can import the module correctly?

over 4 years ago · Santiago Trujillo
4 answers
Answer question

0

It is because your project in vanilla JS, the import statement is not going to work. You can use parceljs a zero config package bundler which supports ES6 and above syntax to use it first add it as a dev dependency by running

npm install parcel-bundler --save-dev

And then add to scripts to your package

{
  "scripts": {
    "dev": "parcel <your entry file>",
    "build": "parcel build <your entry file>"
  }
}

replace the entry file with your main file in your code (example: app.js)

over 4 years ago · Santiago Trujillo Report

0

did you try

          const Example = require('../src/models/Example.js') 
over 4 years ago · Santiago Trujillo Report

0

The modern way to do that is on yow package.json Add a property call type and set the value of module

{
“name”;”project name”,
…
“type”;”module”,
…
“dependencies”:”….”,
}

by default nodejs treads yow code as type: commons It is why it says you cannot use import out side a module, bcuz yow package.json implicitly marks yow codes as type commonjs. So after you set type module then you need to change the extension of them JavaScript files, from .js to .mjs. Yes you guessed right the m is from module. You can do this or configure Babel, webpack, while you find the holy grail and what ever else is need it

over 4 years ago · Santiago Trujillo Report

0

Did you try to use a monorepo approach using something like Lerna.

https://lerna.js.org/

So that you could create modules and dependencies directly inside your repository.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!