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

790
Visualizações
Why is 'type: module' in package.json file?

I upgraded the node and built the existing file.

But it didn't build, and there was an error.

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:                          │
   │   ~~/nuxt.config.js                                      │
   │   require() of ES modules is not supported.                                            │
   │   require() of ~~/nuxt.config.js from                    │
   │   ~~/config.js is an ES   │
   │   module file as it is a .js file whose nearest parent package.json contains "type":   │
   │   "module" which defines all .js files in that package scope as ES modules.            │
   │   Instead rename nuxt.config.js to end in .cjs, change the requiring code to use       │
   │   import(), or remove "type": "module" from                                            │
   │   ~~/package.json.  

So I removed 'type: module' in package.json file.

Is it okay to remove it?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

When you have "type": "module" in the package.json file, your source code should use import syntax. When you do not have, you should use require syntax.

Adding "type": "module" to the package.json enables ES 6 modules. For more info, see here.

over 4 years ago · Santiago Trujillo Relatório

0

Update as of mid-2021

If you're using Node.js v14, and you should if you can since it's LTS, you only need to use type: module as explained here: https://blog.logrocket.com/es-modules-in-node-today/


If you're still stuck with a lower version of Node.js for some reasons, you can follow this blog post from Flavio: https://flaviocopes.com/how-to-enable-es-modules-nodejs/

And do the following:

  • add "type": "module" in your package.json
  • use --experimental-modules when launching your app, eg: node --experimental-modules app.js

Or you can do that instead:

  • add "type": "module" in your package.json
  • rename your file with an .mjs extension, end result will look like this node app.mjs
over 4 years ago · Santiago Trujillo Relatório

0

@AfsharMohebi's answer is excellent and covers the most useful points.

This answer is to add some color around CI/CD pipelines, where one may need to utilize adding a dynamic type parameter for executing code with node, written in ES6 JavaScript. Additionally, I am aware this is tangential to the OP's question but Google brought me here and so hopefully this is found useful by someone else.

In particular, we may use --input-type=module according to the node docs if we do not have a package.json including type: module.

For example, I use the command below to test that an npm package was uploaded successfully and is usable:

mkdir test-mypkg && cd test-mypkg 
echo "import { myFunc } from '@myname/myPkg';" > test.js 
npm i @myname/myPkg @babel/core @babel/node && cat test.js | node --input-type=module

Note: babel dependencies are included for full ES6 to ES5 transpilation and may/may not be necessary. In addition, you should probably pin the version of the package myPkg you're testing!

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