Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

227
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

0

did you try

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

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda