无法在我的 Nuxt 项目中使用 "node" 命令导入 ES 模块
Cannot import ES modules with "node" command in my Nuxt project
- 我的 nuxt 项目使用
nuxt.config.js
中定义的 serverMiddleware
作为 serverMiddleware: ['~/api']
- 我的 api 目录中的所有文件都使用导入和导出语句,并且在我 运行
npm run dev
时可以完美运行
- 我还有一个 seeders 目录,其中包含一堆数据库种子文件,每个文件都有
import
和 export
语句
- 当我 运行 它来自
package.json
和 "seed:dev": "cross-env NODE_ENV=development node ./api/db/seeders"
,
我收到以下错误
(node:12212) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
/Users/zup/Desktop/code/ACTIVE/ch_v3_final/api/db/seeders/index.js:1
import feeds from './feeds-data'
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:915:16)
at Module._compile (internal/modules/cjs/loader.js:963:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
at internal/main/run_main_module.js:17:47
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! ch@1.0.0 seed:dev: `cross-env NODE_ENV=development node ./api/db/seeders`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the ch@1.0.0 seed:dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/zup/.npm/_logs/2021-03-21T16_38_58_766Z-debug.log
我如何运行 数据库播种器使用导入导出语句的方式 nuxt 运行我的其他文件?
你的 Node.js 版本是什么?
据此,您有多种选择,详见此处:
最简单的方法仍然是在您的 package.json
中添加一个 "type": "module"
,正如错误告诉您的那样!
最新的 LTS 是:14.16.0
,可能并非所有平台都支持它,所以要小心。
PS:如果我没记错的话v15更简单一些,但这个版本仍然是实验性的。
TLDR:与 Nuxt 没有任何关系。 ^^
- 我的 nuxt 项目使用
nuxt.config.js
中定义的serverMiddleware
作为 serverMiddleware:['~/api']
- 我的 api 目录中的所有文件都使用导入和导出语句,并且在我 运行
npm run dev
时可以完美运行
- 我还有一个 seeders 目录,其中包含一堆数据库种子文件,每个文件都有
import
和export
语句 - 当我 运行 它来自
package.json
和"seed:dev": "cross-env NODE_ENV=development node ./api/db/seeders"
,
我收到以下错误
(node:12212) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
/Users/zup/Desktop/code/ACTIVE/ch_v3_final/api/db/seeders/index.js:1
import feeds from './feeds-data'
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:915:16)
at Module._compile (internal/modules/cjs/loader.js:963:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
at internal/main/run_main_module.js:17:47
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! ch@1.0.0 seed:dev: `cross-env NODE_ENV=development node ./api/db/seeders`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the ch@1.0.0 seed:dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/zup/.npm/_logs/2021-03-21T16_38_58_766Z-debug.log
我如何运行 数据库播种器使用导入导出语句的方式 nuxt 运行我的其他文件?
你的 Node.js 版本是什么?
据此,您有多种选择,详见此处:
最简单的方法仍然是在您的 package.json
中添加一个 "type": "module"
,正如错误告诉您的那样!
最新的 LTS 是:14.16.0
,可能并非所有平台都支持它,所以要小心。
PS:如果我没记错的话v15更简单一些,但这个版本仍然是实验性的。
TLDR:与 Nuxt 没有任何关系。 ^^